public static void InitState(State state, ulong randSeed) { var random = new LocalRandom(true, randSeed); foreach (var rate in state.Rates) { rate.Init(random, (int)state.StartYear, (int)state.NumYears); } }
public void Init(LocalRandom random, int startYear, int numYears) { this.startYear = startYear; values = new double[numYears]; for (var i = 0; i < numYears; ++i) { values[i] = pdf.Sample(random); } }
public static double SampleSeries(int startYear, int endYear, LocalRandom random, double[] series) { if (startYear < 1928 || 2016 < startYear || endYear < 1928 || 2016 < endYear || endYear < startYear) { throw new ArgumentException("Start year and end year must be in 1928-2016"); } var len = endYear - startYear + 1; var index = random.Next(len) + (startYear - 1928); return(series[index] / 100.0); }
/// <summary> /// Run a test on the rand generators, return true on success /// </summary> /// <param type="outputAction"></param> /// <returns></returns> public static bool Test64(Action <string> outputAction) { TestRand01(outputAction); // local testing helper Func <Action <string>, bool, ulong[], bool> RunTest = (printf, use128Bit, vals) => { var rand = new LocalRandom(use128Bit, 0, 0); rand.SetSeed(42, 0); // seed from paper var success1 = true; foreach (var correctValue in vals) { var randValue = rand.Next64(); printf($"0x{randValue:X16}=0x{correctValue:X16}, {randValue == correctValue} "); success1 &= randValue == correctValue; } return(success1); }; var success = true; outputAction("Testing rand 64"); success &= RunTest(outputAction, false, new ulong[] { 0x27a53829edf003a9, 0xdf28458e5c04c31c, 0x2756dc550bc36037, 0xa10325553eb09ee9, 0x40a0fccb8d9df09f, 0x5c2047cfefb5e9ca }); outputAction("Testing rand 128"); success &= RunTest(outputAction, true, new ulong[] { 0x287472e87ff5705a, 0xbbd190b04ed0b545, 0xb6cee3580db14880, 0xbf5f7d7e4c3d1864, 0x734eedbe7e50bbc5, 0xa5b6b5f867691c77 }); outputAction(success ? "Rand test succeeded." : "Rand test failed."); return(success); }
public double Sample(LocalRandom random) { const double epsilon = Double.Epsilon; generate = !generate; if (!generate) { return(z1 * Parameter + Mean); } double u1, u2; do { u1 = random.NextDouble(); u2 = random.NextDouble(); }while (u1 <= epsilon); var z0 = Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Cos(2 * Math.PI * u2); z1 = Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2 * Math.PI * u2); return(z0 * Parameter + Mean); }
/// <summary> /// return logger that has data for monte carlo sim /// todo - count, parallel /// </summary> /// <param name="state"></param> /// <param name="count"></param> /// <returns></returns> public static async Task <Tuple <GDataTable, double[]> > RunSimulations(State state, int count, ulong randomSeed, double [] percentiles) { var logs = new ConcurrentQueue <DDataTable>(); await Task.Run(() => { var rand1 = new LocalRandom(true, randomSeed); var seeds = new ulong[count]; for (var i = 0; i < count; ++i) { seeds[i] = rand1.Next64(); } Parallel.For(0, count, n => { var log = new DDataTable(); var tempState = DeepClone(state); Simulation.Simulate(tempState, seeds[n], log); logs.Enqueue(log); }); }); var finalLog = new GDataTable(); if (count == 1) { // create log of proper type var log = logs.First(); foreach (var row in Enumerable.Range(0, log.RowCount)) { finalLog.StartRow(); foreach (var col in Enumerable.Range(0, log.ColCount)) { finalLog.AddData(new double[] { log[row, col] }, log.ColumnNames[col]); } } return(new Tuple <GDataTable, double[]>(finalLog, new double[] { 0.50 })); } // sort by sum of total worths in item var sortedLogs = new List <Tuple <DDataTable, double> >(); foreach (var log in logs) { var maxCol = log.ColumnNames.Count; var lastRow = log.RowCount - 1; var value = 0.0; for (var i = 0; i < maxCol; ++i) { if (log.ColumnNames[i].ToString().Contains(Actor.FinalValueText)) { value += log[lastRow, i]; } } sortedLogs.Add(new Tuple <DDataTable, double>(log, value)); } sortedLogs.Sort((a, b) => a.Item2.CompareTo(b.Item2)); var rowMax = sortedLogs.Max(r => r.Item1.RowCount); var columnNames = sortedLogs[0].Item1.ColumnNames; foreach (var row in Enumerable.Range(0, rowMax + 1)) { finalLog.StartRow(); foreach (var name in columnNames) { var value = new List <double>(); foreach (var entry in percentiles) { var log = sortedLogs[(int)(sortedLogs.Count * entry)].Item1; var column = log.GetColumn(name); if (column.Count > row) { value.Add(column[row]); } } finalLog.AddData(value.ToArray(), name); } } return(new Tuple <GDataTable, double[]>(finalLog, percentiles)); }
public double Sample(LocalRandom random) { var u = random.NextDouble() - 0.5; return(Mean - Parameter * Math.Sign(u) * Math.Log(1 - 2 * Math.Abs(u))); }
public double Sample(LocalRandom random) { return(InvestmentTables.SampleSeries(1928, 2016, random, InvestmentTables.sp500Returns1928To2016)); }
public double Sample(LocalRandom random) { var len = annualInflation1957To2016.Length; return(annualInflation1957To2016[random.Next(len)] / 100.0); }