public static ITimeSeries <double> RandomWalk(double drift, double stepsigma, int numsteps = 100, DataFrequency freq = DataFrequency.Daily) { var norm = new Normal(drift, stepsigma); norm.RandomSource = new Random(RandomSeed); var data = norm.Samples(); var walk = new List <double>(); var last = 1.0; walk.Add(last); int count = 0; // Use TakeWhile ?? foreach (var d in data) { last = last + d; walk.Add(last); count++; if (count >= numsteps) { break; } } return(TimeSeriesFactory <double> .Create(walk, integrationOrder : 1, freq : freq)); }
public static ITimeSeries <double> Gaussian(double mu, double sigma, int numperiods = 100, DataFrequency freq = DataFrequency.Daily) { var norm = new Normal(mu, sigma); norm.RandomSource = new Random(RandomSeed); var data = norm.Samples(); return(TimeSeriesFactory <double> .Create(data, 0, numperiods, freq)); }
public static ITimeSeries <double> FirstDifference(this ITimeSeries <double> timeseries, ReturnMethod method = ReturnMethod.Geometric) { if (method == ReturnMethod.Arithmetic) { var res = from cur in timeseries.Skip(1) select new TSDataPoint <double>(cur.Date, cur.Value / timeseries[(from prev in timeseries where prev.Date < cur.Date select prev.Date).Max()] - 1); return(TimeSeriesFactory <double> .Create(res, timeseries.Name, timeseries.IntegrationOrder - 1, timeseries.Frequency)); } else { var res = from cur in timeseries.Skip(1) select new TSDataPoint <double>(cur.Date, Math.Log(cur.Value / timeseries[(from prev in timeseries where prev.Date < cur.Date select prev.Date).Max()])); return(TimeSeriesFactory <double> .Create(res, timeseries.Name, timeseries.IntegrationOrder - 1, timeseries.Frequency)); } }