private static void PrintEndInfo(ProgramOptions options, List<StockEstimate> paths) { var avg = paths.Select(x => x.RealizedVol).Average(); var avgPrice = paths.Select(x => x.FinalStockPrice).Average(); Console.WriteLine("Experimental average vol: {0} and relative error is {1}", avg, Math.Abs(avg - options.Vol) / Math.Abs(options.Vol)); Console.WriteLine("Average final stock price: {0} which gives {1:P2} return", avgPrice, avgPrice / options.Price); Console.ReadKey(); }
private static void RunSimulations(ProgramOptions options) { TimeSpan ts = new TimeSpan(365 * options.Years, 0, 0, 0); ts = new TimeSpan(ts.Ticks / options.Steps); // note that ts.TotalDays / 365.0 <=> t / n Random rnd = new Random(options.Seed); List<StockEstimate> paths = new List<StockEstimate>(); for (int i = 0; i < options.Count; ++i) { PathSimulate(options, ts, rnd, paths); } File.WriteAllText("output.txt", paths.ToCsv()); PrintEndInfo(options, paths); }
public static ProgramOptions ConstructFrom(string[] args) { ProgramOptions options = null; if (args.Length == 14) { options = new ProgramOptions(); IList<PropertyInfo> propertyInfos = typeof(ProgramOptions).GetProperties(); for (int i = 1; i < args.Length; i += 2) { foreach (var p in propertyInfos) { if (args[i - 1] == String.Format("/{0}", p.Name.ToLower())) { p.SetValue(options, Convert.ChangeType(args[i], p.PropertyType)); } } } } return options; }
private static void PathSimulate(ProgramOptions options, TimeSpan ts, Random rnd, List<StockEstimate> paths) { GeometricBrownianMotionMarket market = new GeometricBrownianMotionMarket(rnd, options.Price, options.Drift, options.Vol); List<double> logreturn = new List<double>(); double lastPrice = market.StockPrice; for (int j = 0; j < options.Steps; ++j) { market.Evolve(ts); logreturn.Add(Math.Log(market.StockPrice / lastPrice)); lastPrice = market.StockPrice; } double avg = logreturn.Average(); double realizedVolSquared = 365.0 / (ts.TotalDays * (options.Steps - 1)) * logreturn.Select(x => (x - avg) * (x - avg)).Sum(); paths.Add(new StockEstimate() { FinalStockPrice = market.StockPrice, RealizedVol = Math.Sqrt(realizedVolSquared) }); }