/// <summary> /// the whole process to calculate /// </summary> /// <param name="S">Initial Stock Price</param> /// <param name="r">Risk Free Rate</param> /// <param name="T">Maturity</param> /// <param name="nv">parameter in model</param> /// <param name="k">parameter in model</param> /// <param name="theta">parameter in model</param> /// <param name="rho">parameter in model</param> /// <param name="sigma">parameter in model</param> /// <param name="N">Number Of Time Steps</param> /// <param name="n">Number Of Trials</param> /// <returns>the price of look back option</returns> public double CalculateLookbackCallOptionPrice(double S, double r, double T, double nv, double k, double theta, double rho, double sigma, int N, int n) { if (S <= 0 || T <= 0) { throw new System.ArgumentException("Need S, K, T > 0"); } if (2.0 * k * theta <= sigma * sigma) { throw new System.ArgumentException("need to meet Feller condition"); } else { double Priceitself = 0.0; double[] callprice = new double[n]; for (int i = 0; i < n; i++) { callprice[i] = Math.Exp(-r * T) * (GeneratePath.Generateallpath(S, r, T, nv, k, theta, rho, sigma /*, tau*/, N, n)[N] - GeneratePath.Generateallpath(S, r, T, nv, k, theta, rho, sigma, N, n).Min()); } for (int j = 0; j < n; j++) { Priceitself = Priceitself + callprice[j]; } return(Priceitself / n); } }
/// <summary> /// For how to calculate the European option price by MC /// </summary> /// <param name="S">Initial Stock Price</param> /// <param name="r">Risk Free Rate</param> /// <param name="T">Maturity</param> /// <param name="nv">parameter in model</param> /// <param name="k">parameter in model</param> /// <param name="theta">parameter in model</param> /// <param name="rho">parameter in model</param> /// <param name="sigma">parameter in model</param> /// <param name="N">Number Of Time Steps</param> /// <param name="n">Number Of Trials</param> /// <returns>the price we generating from Monte Carlo method</returns> public double CalculateEuropeanCallOptionPrice(double S, double K, double r, double T, double nv, double k, double theta, double rho, double sigma, int N, int n) { if (S <= 0 || K <= 0 || T <= 0) { throw new System.ArgumentException("Need S, K, T > 0"); } if (2 * k * theta <= sigma * sigma) { throw new System.ArgumentException("need to meet Feller condition"); } else { double[] callprice = new double[n]; for (int i = 0; i < n; i++) { callprice[i] = GeneratePath.Generateallpath(S, r, T, nv, k, theta, rho, sigma, N, n)[N]; } double Price = 0.0; int j = 0; double[] aaa = new double[n]; for (j = 0; j < n; j++) { Price = Price + Math.Exp(-r * T) * Math.Max(callprice[j] - K, 0); aaa[j] = Math.Max(callprice[j] - K, 0); } return(Price / n); } }
public override void OnInspectorGUI() { GeneratePath pathGen = (GeneratePath)target; DrawDefaultInspector(); if (GUILayout.Button("Generate")) { pathGen.Generate(); } }
/// <summary> /// Handles the EditValueChanged event of the TxtBluray control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> private void TxtBluray_EditValueChanged(object sender, EventArgs e) { this.txtBluraySeriesNFOPreview.Text = GeneratePath.TvSeries( null, Get.InOutCollection.CurrentTvSaveSettings.BluraySeriesNfoTemplate, this.txtBlurayTestPath.Text); this.txtBluraySeriesPosterPreview.Text = GeneratePath.TvSeries( null, Get.InOutCollection.CurrentTvSaveSettings.BluraySeriesPosterTemplate, this.txtBlurayTestPath.Text) + ".jpg"; this.txtBluraySeriesBannerPreview.Text = GeneratePath.TvSeries( null, Get.InOutCollection.CurrentTvSaveSettings.BluraySeriesBannerTemplate, this.txtBlurayTestPath.Text) + ".jpg"; this.txtBluraySeriesFanartPreview.Text = GeneratePath.TvSeries( null, Get.InOutCollection.CurrentTvSaveSettings.BluraySeriesFanartTemplate, this.txtBlurayTestPath.Text) + ".jpg"; this.txtBluraySeasonPosterPreview.Text = GeneratePath.TvSeason( null, Get.InOutCollection.CurrentTvSaveSettings.BluraySeasonPosterTemplate, this.txtBlurayTestPath.Text) + ".jpg"; this.txtBluraySeasonFanartPreview.Text = GeneratePath.TvSeason( null, Get.InOutCollection.CurrentTvSaveSettings.BluraySeasonFanartTemplate, this.txtBlurayTestPath.Text) + ".jpg"; this.txtBluraySeasonBannerPreview.Text = GeneratePath.TvSeason( null, Get.InOutCollection.CurrentTvSaveSettings.BluraySeasonBannerTemplate, this.txtBlurayTestPath.Text) + ".jpg"; this.txtBlurayEpisodeNFOPreview.Text = GeneratePath.TvEpisode( null, Get.InOutCollection.CurrentTvSaveSettings.BlurayEpisodeNFOTemplate, this.txtBlurayTestPath.Text); this.txtBlurayEpisodeScreenshotPreview.Text = GeneratePath.TvEpisode( null, Get.InOutCollection.CurrentTvSaveSettings.BlurayEpisodeScreenshotTemplate, this.txtBlurayTestPath.Text) + ".jpg"; }
public double CalculateAsianPutOptionPrice(double S, double K, double r, IEnumerable <double> T, double exerciseT, double nv, double k, double theta, double rho, double sigma, int N, int n) { if (S <= 0 || K <= 0 || exerciseT <= 0) { throw new System.ArgumentException("Need S, K, T > 0"); } CheckAsianOptionInputs(T, exerciseT); if (2.0 * k * theta <= sigma * sigma) { throw new System.ArgumentException("need to meet Feller condition"); } else { int M = T.Count(); double tau = exerciseT / N; double[] givenprice = new double[M]; double[] callprice = new double[n]; double[] path = new double[N]; int number; double priceitself; double price; double Price; for (int u = 0; u < n; u++) { path = GeneratePath.Generateallpath(S, r, exerciseT, nv, k, theta, rho, sigma /*, tau*/, N, n); for (int i = 0; i < M; i++) { number = (int)(T.ElementAt(i) / tau); givenprice[i] = path[number]; } priceitself = givenprice.Sum(); price = Math.Exp(-r * exerciseT) * Math.Max(K - priceitself / M, 0); callprice[u] = price; } Price = callprice.Sum(); return(Price / n); } }
/// <summary> /// how to calculate the price by using the Monte Carlo and payoff formula /// </summary> /// <param name="S">Initial Stock Price</param> /// <param name="r">Risk Free Rate</param> /// <param name="T">Maturity</param> /// <param name="K">Strike Price</param> /// <param name="nv">parameter in model</param> /// <param name="k">parameter in model</param> /// <param name="theta">parameter in model</param> /// <param name="rho">parameter in model</param> /// <param name="sigma">parameter in model</param> /// <param name="N">Number Of Time Steps</param> /// <param name="n">Number Of Trials</param> /// <returns>calculate the price from the payoff formula</returns> public double CalculateRainbowOptionPrice(double S, double K, double r, double T, double nv, double k, double theta, double rho, double sigma, int N, int n) { if (S <= 0 || T <= 0) { throw new System.ArgumentException("Need S, K, T > 0"); } if (2.0 * k * theta <= sigma * sigma) { throw new System.ArgumentException("need to meet Feller condition"); } else { double priceitself; double[] price = new double[n]; for (int j = 0; j < n; j++) { price[j] = Math.Exp(-r * T) * Math.Max(GeneratePath.Generateallpath(S, r, T, nv, k, theta, rho, sigma, N, n).Max(), K); } priceitself = price.Sum(); return(priceitself / n); } }
/// <summary> /// Saves the series. /// </summary> /// <param name="series">The series.</param> /// <param name="type">The SeriesIOType type.</param> public void SaveSeries(Series series, SeriesIOType type) { string path = series.GetSeriesPath(); if (string.IsNullOrEmpty(path)) { return; } string nfoTemplate; string posterTemplate; string fanartTemplate; string bannerTemplate; string firstEpisodePath = series.GetFirstEpisode(); TvRenamerFactory.RenameSeries(series); if (MovieNaming.IsBluRay(firstEpisodePath)) { nfoTemplate = Get.InOutCollection.CurrentTvSaveSettings.BluraySeriesNfoTemplate; posterTemplate = Get.InOutCollection.CurrentTvSaveSettings.BluraySeriesPosterTemplate; fanartTemplate = Get.InOutCollection.CurrentTvSaveSettings.BluraySeriesFanartTemplate; bannerTemplate = Get.InOutCollection.CurrentTvSaveSettings.BluraySeriesBannerTemplate; } else if (MovieNaming.IsDVD(firstEpisodePath)) { nfoTemplate = Get.InOutCollection.CurrentTvSaveSettings.DVDSeriesNfoTemplate; posterTemplate = Get.InOutCollection.CurrentTvSaveSettings.DVDSeriesPosterTemplate; fanartTemplate = Get.InOutCollection.CurrentTvSaveSettings.DVDSeriesFanartTemplate; bannerTemplate = Get.InOutCollection.CurrentTvSaveSettings.DVDSeriesBannerTemplate; } else { nfoTemplate = Get.InOutCollection.CurrentTvSaveSettings.SeriesNfoTemplate; posterTemplate = Get.InOutCollection.CurrentTvSaveSettings.SeriesPosterTemplate; fanartTemplate = Get.InOutCollection.CurrentTvSaveSettings.SeriesFanartTemplate; bannerTemplate = Get.InOutCollection.CurrentTvSaveSettings.SeriesBannerTemplate; } if (type == SeriesIOType.All || type == SeriesIOType.Nfo) { // Nfo string nfoPathTo = GeneratePath.TvSeries(series, nfoTemplate); this.WriteNFO(this.GenerateSeriesOutput(series), nfoPathTo); series.ChangedText = false; } // Poster if (type == SeriesIOType.All || type == SeriesIOType.Images || type == SeriesIOType.Poster) { if (!string.IsNullOrEmpty(series.PosterUrl)) { string posterPathFrom; if (!string.IsNullOrEmpty(series.PosterPath) && File.Exists(series.PosterPath)) { posterPathFrom = series.PosterPath; } else { posterPathFrom = this.TvPathImageGet(series.PosterUrl); } string posterPathTo = GeneratePath.TvSeries(series, posterTemplate); this.CopyFile(posterPathFrom, posterPathTo + ".jpg"); series.ChangedPoster = false; } } // Fanart if (type == SeriesIOType.All || type == SeriesIOType.Images || type == SeriesIOType.Fanart) { if (!string.IsNullOrEmpty(series.FanartUrl)) { string fanartPathFrom; if (!string.IsNullOrEmpty(series.FanartPath) && File.Exists(series.FanartPath)) { fanartPathFrom = series.FanartPath; } else { fanartPathFrom = this.TvPathImageGet(series.FanartUrl); } string fanartPathTo = GeneratePath.TvSeries(series, fanartTemplate); this.CopyFile(fanartPathFrom, fanartPathTo + ".jpg"); series.ChangedFanart = false; } } // Banner if (type == SeriesIOType.All || type == SeriesIOType.Images || type == SeriesIOType.Banner) { if (!string.IsNullOrEmpty(series.SeriesBannerUrl)) { string bannerPathFrom; if (!string.IsNullOrEmpty(series.SeriesBannerPath) && File.Exists(series.SeriesBannerPath)) { bannerPathFrom = series.SeriesBannerPath; } else { bannerPathFrom = this.TvPathImageGet(series.SeriesBannerUrl); } string bannerPathTo = GeneratePath.TvSeries(series, bannerTemplate); this.CopyFile(bannerPathFrom, bannerPathTo + ".jpg"); series.ChangedBanner = false; } } }
/// <summary> /// Saves the season. /// </summary> /// <param name="season">The season.</param> /// <param name="type">The SeasonIOType type.</param> public void SaveSeason(Season season, SeasonIOType type) { if (season.HasEpisodeWithPath()) { string posterTemplate; string fanartTemplate; string bannerTemplate; string firstEpisodePath = season.GetFirstEpisode(); if (MovieNaming.IsBluRay(firstEpisodePath)) { posterTemplate = Get.InOutCollection.CurrentTvSaveSettings.BluraySeasonPosterTemplate; fanartTemplate = Get.InOutCollection.CurrentTvSaveSettings.BluraySeasonFanartTemplate; bannerTemplate = Get.InOutCollection.CurrentTvSaveSettings.BluraySeasonBannerTemplate; } else if (MovieNaming.IsDVD(firstEpisodePath)) { posterTemplate = Get.InOutCollection.CurrentTvSaveSettings.DVDSeasonPosterTemplate; fanartTemplate = Get.InOutCollection.CurrentTvSaveSettings.DVDSeasonFanartTemplate; bannerTemplate = Get.InOutCollection.CurrentTvSaveSettings.DVDSeasonBannerTemplate; } else { posterTemplate = Get.InOutCollection.CurrentTvSaveSettings.SeasonPosterTemplate; fanartTemplate = Get.InOutCollection.CurrentTvSaveSettings.SeasonFanartTemplate; bannerTemplate = Get.InOutCollection.CurrentTvSaveSettings.SeasonBannerTemplate; } // Poster if (type == SeasonIOType.All || type == SeasonIOType.Poster) { if (!string.IsNullOrEmpty(season.PosterUrl)) { string posterPathFrom; if (!string.IsNullOrEmpty(season.PosterPath) && File.Exists(season.PosterPath)) { posterPathFrom = season.PosterPath; } else { posterPathFrom = this.TvPathImageGet(season.PosterUrl); } string posterPathTo = GeneratePath.TvSeason(season, posterTemplate); this.CopyFile(posterPathFrom, posterPathTo + ".jpg"); season.ChangedPoster = false; } } // Fanart if (type == SeasonIOType.All || type == SeasonIOType.Fanart) { if (!string.IsNullOrEmpty(season.FanartUrl)) { string fanartPathFrom; if (!string.IsNullOrEmpty(season.PosterPath) && File.Exists(season.PosterPath)) { fanartPathFrom = season.FanartPath; } else { fanartPathFrom = this.TvPathImageGet(season.FanartUrl); } string fanartPathTo = GeneratePath.TvSeason(season, fanartTemplate); this.CopyFile(fanartPathFrom, fanartPathTo + ".jpg"); season.ChangedFanart = false; } } // Banner if (type == SeasonIOType.All || type == SeasonIOType.Banner) { if (!string.IsNullOrEmpty(season.BannerUrl)) { string bannerPathFrom; if (!string.IsNullOrEmpty(season.BannerPath) && File.Exists(season.BannerPath)) { bannerPathFrom = season.BannerPath; } else { bannerPathFrom = this.TvPathImageGet(season.BannerUrl); } string bannerPathTo = GeneratePath.TvSeason(season, bannerTemplate); this.CopyFile(bannerPathFrom, bannerPathTo + ".jpg"); season.ChangedBanner = false; } } } }
/// <summary> /// Saves the episode. /// </summary> /// <param name="episode">The episode.</param> /// <param name="type">The EpisodeIOType type.</param> public void SaveEpisode(Episode episode, EpisodeIOType type) { if (episode.Secondary) { return; } if (string.IsNullOrEmpty(episode.FilePath.FileNameAndPath)) { return; } string nfoTemplate; string screenshotTemplate; if (MovieNaming.IsDVD(episode.FilePath.FileNameAndPath)) { nfoTemplate = Get.InOutCollection.CurrentTvSaveSettings.DVDEpisodeNFOTemplate; screenshotTemplate = Get.InOutCollection.CurrentTvSaveSettings.DVDEpisodeScreenshotTemplate; } else if (MovieNaming.IsBluRay(episode.FilePath.FileNameAndPath)) { nfoTemplate = Get.InOutCollection.CurrentTvSaveSettings.BlurayEpisodeNFOTemplate; screenshotTemplate = Get.InOutCollection.CurrentTvSaveSettings.BlurayEpisodeScreenshotTemplate; } else { nfoTemplate = Get.InOutCollection.CurrentTvSaveSettings.EpisodeNFOTemplate; screenshotTemplate = Get.InOutCollection.CurrentTvSaveSettings.EpisodeScreenshotTemplate; } // Nfo if (type == EpisodeIOType.All || type == EpisodeIOType.Nfo) { string nfoPathTo = GeneratePath.TvEpisode(episode, nfoTemplate); this.WriteNFO(this.GenerateSingleEpisodeOutput(episode, true), nfoPathTo); episode.ChangedText = false; } // Screenshot if (type == EpisodeIOType.Screenshot || type == EpisodeIOType.All) { if (!string.IsNullOrEmpty(episode.FilePath.FileNameAndPath)) { string screenshotPathFrom; if (!string.IsNullOrEmpty(episode.EpisodeScreenshotPath) && File.Exists(episode.EpisodeScreenshotPath)) { screenshotPathFrom = episode.EpisodeScreenshotPath; } else { screenshotPathFrom = this.TvPathImageGet(episode.EpisodeScreenshotUrl); } string screenshotPathTo = GeneratePath.TvEpisode(episode, screenshotTemplate); this.CopyFile(screenshotPathFrom, screenshotPathTo + ".jpg"); episode.ChangedScreenshot = false; } } }
public void Execute() { if ((DateTime.Now - LastLastPositionUpdate > TimeSpan.FromMilliseconds(1000) && LastPosition.GetDistance(GetPosition.Invoke()) > 24) || TryCount > 2) { Reset(); return; } if (CurrentPath.Count == 0) { TryCount = 0; List <Vector3> nodes = GeneratePath.Invoke(GetPosition.Invoke(), TargetPosition); if (nodes.Count == 0) { // pathfinding was unsuccessful return; } foreach (Vector3 node in nodes) { CurrentPath.Enqueue(node); } CurrentPathTargetPosition = TargetPosition; } List <Vector3> forces = new List <Vector3>(); Vector3 currentPosition = GetPosition.Invoke(); Vector3 targetPosition = CurrentPath.Peek(); double distanceToTargetPosition = currentPosition.GetDistance(targetPosition); if (distanceToTargetPosition > 128) { Reset(); return; } else if (distanceToTargetPosition < MovementSettings.WaypointCheckThreshold) { if (CurrentPath.Count > 0) { targetPosition = CurrentPath.Dequeue(); } else if (CurrentPath.Count == 0) { return; } } Vector3 positionToGoTo = BotUtils.MoveAhead(GetRotation.Invoke(), targetPosition, 2); bool updateForces = true; switch (State) { case MovementEngineState.Moving: forces.Add(PlayerVehicle.Seek(positionToGoTo, 1)); break; case MovementEngineState.DirectMoving: PlayerVehicle.MoveToPosition(targetPosition); updateForces = false; break; case MovementEngineState.Following: forces.Add(PlayerVehicle.Seek(positionToGoTo, 1)); forces.Add(PlayerVehicle.Seperate(1)); break; case MovementEngineState.Chasing: forces.Add(PlayerVehicle.Seek(positionToGoTo, 1)); break; case MovementEngineState.Fleeing: forces.Add(PlayerVehicle.Flee(positionToGoTo, 1)); break; case MovementEngineState.Evading: forces.Add(PlayerVehicle.Evade(positionToGoTo, 1, TargetRotation)); break; case MovementEngineState.Wandering: forces.Add(PlayerVehicle.Wander(1)); break; case MovementEngineState.Stuck: forces.Add(PlayerVehicle.Unstuck(1)); break; default: return; } if (updateForces) { PlayerVehicle.Update(forces); } if (DateTime.Now - LastJumpCheck > TimeSpan.FromMilliseconds(250)) { double distanceTraveled = LastPosition.GetDistance(GetPosition.Invoke()); if ((LastPosition.X == 0 && LastPosition.Y == 0 && LastPosition.Z == 0) || distanceTraveled < 0.3) { Jump.Invoke(); TryCount++; } LastPosition = GetPosition.Invoke(); LastLastPositionUpdate = DateTime.Now; LastJumpCheck = DateTime.Now; } LastTargetPosition = GetPosition.Invoke(); HasMoved = true; }
public void Execute() { if (CurrentPath.Count == 0 || CurrentPathTargetPosition.GetDistance(TargetPosition) > 1) { List <Vector3> nodes = GeneratePath.Invoke(GetPosition.Invoke(), TargetPosition); if (nodes.Count == 0) { // pathfinding was unsuccessful return; } foreach (Vector3 node in nodes) { CurrentPath.Enqueue(node); } CurrentPathTargetPosition = TargetPosition; } List <Vector3> forces = new List <Vector3>(); Vector3 currentPosition = GetPosition.Invoke(); Vector3 targetPosition = CurrentPath.Peek(); double distanceToTargetPosition = currentPosition.GetDistance2D(targetPosition); if (distanceToTargetPosition < MovementSettings.WaypointCheckThreshold) { if (CurrentPath.Count > 0) { targetPosition = CurrentPath.Dequeue(); } else if (CurrentPath.Count == 0) { return; } } Vector3 positionToGoTo = MoveAhead(targetPosition, 1.5); switch (State) { case MovementEngineState.Moving: forces.Add(PlayerVehicle.Seek(positionToGoTo, 1)); break; case MovementEngineState.Following: forces.Add(PlayerVehicle.Seek(positionToGoTo, 1)); forces.Add(PlayerVehicle.Seperate(1)); break; case MovementEngineState.Chasing: forces.Add(PlayerVehicle.Pursuit(positionToGoTo, 1, TargetRotation)); break; case MovementEngineState.Fleeing: forces.Add(PlayerVehicle.Flee(positionToGoTo, 1)); break; case MovementEngineState.Evading: forces.Add(PlayerVehicle.Evade(positionToGoTo, 1, TargetRotation)); break; case MovementEngineState.Wandering: forces.Add(PlayerVehicle.Wander(1)); break; case MovementEngineState.Stuck: forces.Add(PlayerVehicle.Unstuck(1)); break; default: return; } // move PlayerVehicle.Update(forces); if (DateTime.Now - LastJumpCheck > TimeSpan.FromMilliseconds(500)) { double distanceTraveled = LastPosition.GetDistance(GetPosition.Invoke()); if ((LastPosition.X == 0 && LastPosition.Y == 0 && LastPosition.Z == 0) || distanceTraveled < 0.5) { Jump.Invoke(); } LastPosition = GetPosition.Invoke(); LastJumpCheck = DateTime.Now; } HasMoved = true; }