public override TimeSeries BuildForecasts(TimeSeries simulatedData, List <DateTime> futureTimes) { // fit model first, using maximum likelihood estimation var model = new ARMAModel(mAROrder, mMAOrder); // create the model object model.TheData = simulatedData; // this is the data we want to fit the model to model.FitByMLE(mNumberIterLDS, mNumberIterOpt, 0, null); // first param is # low discrepancy sequence iterations, should be at least about 200 // second param is # standard optimizer iterations, should be at least about 100 // now do some forecasting beyond the end of the data var forecaster = new ForecastTransform(); forecaster.FutureTimes = futureTimes.ToArray(); // these are future times at which we want forecasts // for now, ARMA models do not use the times in any meaningful way when forecasting, // they just assume that these are the timestamps for the next sequential points // after the end of the existing time series forecaster.SetInput(0, model, null); // the ARMA model used for forecasting forecaster.SetInput(1, simulatedData, null); // the original data // normally you would call the Recompute() method of a transform, but there is no need // here; it is automatically called as soon as all inputs are set to valid values (in the 2 statements above) var predictors = forecaster.GetOutput(0) as TimeSeries; return(predictors); }
public void ArmaFromExtData() { var arma = new ARMAModel(1, 1, _extData); Assert.IsTrue(arma.CanUseMLE()); arma.FitByMLE(200, 100, 0, null); Write($"Sample mean: {_extData.SampleMean()}, From model: {arma.Mu} \n"); Write($"Desc: {arma.Description}"); }
public void ArmaSimulates() { var _arma = new ARMAModel(1, 1); var simulateData = _arma.SimulateData(_dates, 22); Assert.NotNull(simulateData); Assert.IsTrue(simulateData.Count > 0); Write($"Sample mean: {simulateData.SampleMean()}"); _arma.TheData = simulateData; // Feed back in _arma.FitByMLE(200, 100, 0, null); Write(_arma.Description); }
public EstimationResult Estimate(IEnumerable <IDateValue> dateValues) { var series = new TimeSeries(); dateValues.ForEach(x => series.Add(x.Date, x.Value, true)); armaModel.SetInput(0, series, null); armaModel.FitByMLE(200, 100, 0, null); armaModel.ComputeResidualsAndOutputs(); var result = armaModel.GetOutput(3) as TimeSeries; return(EstimationResult.Create(result[0], this)); }
protected override TimeSeries _BuildOutput(TimeSeries simulatedData, object userState = null) { ARMAModel model = new ARMAModel(mAROrder, mMAOrder); model.SetInput(0, simulatedData, null); //Maximum Likelihood Estimation model.FitByMLE(mNumberIterLDS, mNumberIterOpt, 0, null); // first param is # low discrepancy sequence iterations, should be at least about 200 // second param is # standard optimizer iterations, should be at least about 100 //Compute the residuals model.ComputeResidualsAndOutputs(); //model1.GetOutputName(3); //model1.Description; //Get the predicted values return(model.GetOutput(3) as TimeSeries); }
// this method fits an ARMA model to the data and returns it, in the process testing for non-stationarity and calculating the orders of the // MA and AR terms of the model public ARMAModel buildARMAModel(DataObject[] series, double siglevel, int len = 50, bool print = true, bool log = false) { bool nonstationarity = testStationarity(series, siglevel); if (print) { Console.WriteLine("non-stationarity: " + nonstationarity.ToString()); } int d = 0; if (nonstationarity) { d = 1; } var tSeries = series; //Difference the series if (d != 0) { double[] dcol = new double[tSeries.Length]; for (int i = 0; i < dcol.Length; i++) { dcol[i] = (tSeries[i].Value); } double[] xdiff = ArrayManipulation.diff(dcol); for (int i = dcol.Length - xdiff.Length; i < xdiff.Length; i++) { tSeries[i] = new DataObject(tSeries[i].Date, xdiff[i]); } } var ts = new TimeSeries { Title = "Time Series", Description = "TS Description" }; for (int i = 0; i < series.GetLength(0); i++) { ts.Add(tSeries[i].Date, (tSeries[i].Value), false); //datetime, value, false } if (log) { //log transform the time series var logTransform = new LogTransform(); logTransform.SetInput(0, ts, null); logTransform.Recompute(); ts = logTransform.GetOutput(0) as TimeSeries; } //Use ACF and PACF to find ARMA parameters var highInterval = 1.96 / Math.Sqrt(series.Length); var acf = ts.ComputeACF(20, false); int p = 1; var low = acf[0]; for (int i = 0; i < acf.Count; i++) { if (Math.Min(low, acf[i]) <= highInterval && highInterval < Math.Max(acf[i], low)) { p = i; if (p != 0) { p--; } if (p == 0) { p = 1; } break; } low = acf[i]; } var pacf = TimeSeries.GetPACFFrom(acf); int q = 1; low = pacf[0]; for (int i = 0; i < pacf.Count; i++) { if (Math.Min(low, pacf[i]) <= highInterval && highInterval < Math.Max(pacf[i], low)) { q = i; if (q != 0) { q--; } if (q == 0) { q = 1; } break; } low = pacf[i]; } if (p > 4) { p = 4; } if (q > 4) { q = 4; } if (print) { Console.WriteLine("p: " + p.ToString() + " q: " + q.ToString()); Console.WriteLine("Fitting the model..."); } var model = new ARMAModel(p, q); model.TheData = ts; Stopwatch watch = new Stopwatch(); watch.Start(); model.FitByMLE(200, 100, 0, null); watch.Stop(); if (print) { Console.WriteLine("Model took " + watch.Elapsed.TotalSeconds.ToString() + " seconds to fit parameters to the data."); Console.WriteLine("Model has been fitted."); } return(model); }