//this method is called when the user clicks the Forecast Run button //depending on the user input it a) creates the forecasts b) creates the simulations c) calls the plot1() plot2() and plot(3) methods private void ForecastRun_Click(object sender, EventArgs e) { System.Windows.Forms.Cursor old = this.Cursor; this.Cursor = Cursors.AppStarting; //getting all the information we need about the model int mposition = 0; mposition = Mdgv.SelectedRows[0].Index; int nar = 0; int nma = 0; int nd = 0; int narchp = 0; int narchq = 0; bool subtmean = false; string transform; double shape; List <double> armaestimates = new List <double>(); List <double> garchestimates = new List <double>(); double v = 0; nar = Modellist[mposition].P; nma = Modellist[mposition].Q; nd = Modellist[mposition].D; subtmean = Modellist[mposition].Subtractmean; v = Modellist[mposition].Variance; narchp = Modellist[mposition].archP; narchq = Modellist[mposition].archQ; for (int i = 0; i < nar + nma; i++) { armaestimates.Add(Modellist[mposition].Parameters[i]); } for (int i = 0; i < narchp + narchq + 1; i++) { garchestimates.Add(Modellist[mposition].archParameters[i]); } transform = Modellist[mposition].Transformtype; shape = Modellist[mposition].Transformshape; //temporary lists used in plot2 List <double> p2listf = new List <double>(); List <double> p2lista = new List <double>(); List <List <double> > p3simulations = new List <List <double> >(); List <List <double> > p3lists = new List <List <double> >(); int simulations = 100; //Remember: //TSeries will contain the data - if the model transform is either Iterative or Weibull it will be transformed, otherwise it will be the unadjusted data //Data will contain the info for the first graph i.e. the object on which the models were calibrated. This could be the original data, transformed data, transformed data - mean, differenced data //setup the variables for the forecast int nsteps = (int)this.numericUpDown1.Value; int point = (int)this.numericUpDown2.Value; //now do the forecast for the first graph List <double> forecast = P.predictnstepsahead(data, armaestimates, nar, nma, nsteps, point); List <double> actual = new List <double>(); int counter = 0; while ((point - 1 + counter < data.Count()) && (counter <= nsteps)) { actual.Add(this.Data[point - 1 + counter]); counter++; } //now draw the first plot plot1(forecast, actual, nsteps, point); //if the time series is differenced then we need to calculate the forecast and actual values for the original series //and then undo the transform List <double> forecastD = new List <double>(); List <double> actualD = new List <double>(); if (nd > 0) { //Note if we assume d = 1, then we need to start with "point+1" for the forecasts in plot 1 and plot 2 to commence at the exact //same time point. This is due to differencing and the inclusion of a 0 in the predictnstepsaheadDD function for convention. //Hence, if d = 2 or larger, then the time points on the two graphs may not coincide exactly. //creating the forecast if we have an ARIMA model List <double> temp = P.predictnstepsaheadDD(Tseries, armaestimates, nar, nma, nsteps, point + 1, nd); foreach (double dd in temp) { forecastD.Add(dd); } //creating a list to hold the actual data if we have an ARIMA model counter = 0; while ((point + counter < Tseries.Count()) && (counter <= nsteps)) { actualD.Add(this.Tseries[point + counter]); counter++; } } if ((transform == "Iterative") || (transform == "Weibull") || (subtmean == true)) { //mean double m = 0; if (subtmean == true) { m = P.avg(Tseries); } //if i in the ARIMA model is >0 then we want to work with forecastD and actualD lists if (nd > 0) { List <double> tempA = P.unwind(forecastD, transform, shape, m); List <double> tempB = P.unwind(actualD, transform, shape, m); foreach (double dd in tempA) { p2listf.Add(dd); } foreach (double dd in tempB) { p2lista.Add(dd); } } else { List <double> tempA = P.unwind(forecast, transform, shape, m); List <double> tempB = P.unwind(actual, transform, shape, m); foreach (double dd in tempA) { p2listf.Add(dd); } foreach (double dd in tempB) { p2lista.Add(dd); } } plot2(p2listf, p2lista, nsteps, point); } //ARMAsimulations with nd = 0 if ((nd == 0) && (narchq == 0)) { for (int i = 0; i < simulations; i++) { List <double> tempC = P.ARMAsimulatenstepsahead(data, armaestimates, nar, nma, nsteps, point, v); p3simulations.Add(tempC); } } //ARCHsimulations with nd = 0 if ((nd == 0) && (narchq > 0) && (narchp == 0)) { for (int i = 0; i < simulations; i++) { List <double> tempC = P.ARCHsimulatenstepsahead(data, armaestimates, nar, nma, nsteps, point, v, narchq, garchestimates); p3simulations.Add(tempC); } } //GARCHsimulations with nd = 0 if ((nd == 0) && (narchq > 0) && (narchp > 0)) { for (int i = 0; i < simulations; i++) { List <double> tempC = P.GARCHsimulatenstepsahead(data, armaestimates, nar, nma, nsteps, point, v, narchq, narchp, garchestimates); p3simulations.Add(tempC); } } //ARMAsimulations with nd > 0 if ((nd > 0) && (narchq == 0)) { for (int i = 0; i < simulations; i++) { List <double> tempC = P.ARMAsimulatenstepsaheadDD(Tseries, armaestimates, nar, nma, nsteps, point, v, nd); p3simulations.Add(tempC); } } //ARCHsimulations with nd > 0 if ((nd > 0) && (narchq > 0) && (narchp == 0)) { for (int i = 0; i < simulations; i++) { List <double> tempC = P.ARCHsimulatenstepsaheadDD(Tseries, armaestimates, nar, nma, nsteps, point, v, nd, narchq, garchestimates); p3simulations.Add(tempC); } } //GARCHsimulations with nd > 0 if ((nd > 0) && (narchq > 0) && (narchp > 0)) { for (int i = 0; i < simulations; i++) { List <double> tempC = P.GARCHsimulatenstepsaheadDD(Tseries, armaestimates, nar, nma, nsteps, point, v, nd, narchq, narchp, garchestimates); p3simulations.Add(tempC); } } //now we need to check if we need to do any unwinding if ((transform == "Iterative") || (transform == "Weibull") || (subtmean == true)) { //mean double m = 0; if (subtmean == true) { m = P.avg(Tseries); } for (int i = 0; i < simulations; i++) { List <double> tempD = P.unwind(p3simulations[i], transform, shape, m); p3lists.Add(tempD); } plot3(p3lists, nsteps, point); } this.Cursor = old; }
//on selecting one of the values from the listbox, this method is called. //It a)populates the Tseries list b)calls the loadmodels() method c) calls the populatedatagridview() method private void ARCHlb_SelectedValueChanged(object sender, EventArgs e) { System.Windows.Forms.Cursor old = this.Cursor; this.Cursor = Cursors.AppStarting; Tseries.Clear(); int position = ARCHlb.SelectedIndex; if (position == -1) { this.Cursor = old; return; } string curItem = Tab[position]; string df = Flocations[position]; string f = ";Extended Properties=\"Excel 12.0;HDR=NO\""; string c = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + df + f; string interim1 = Fnames[position].Replace(".xlsx", ""); string interim2 = Tab[position].Replace("$", ""); string interim3 = Flocations[position].Replace(fnames[position], interim1 + interim2 + ".csv"); int counter = 0; string line1; string line2; string line3; double t = 0; double tscale = 0; try { StreamReader sr = new StreamReader(interim3); while (counter < 1) { line1 = sr.ReadLine(); line2 = sr.ReadLine(); line3 = sr.ReadLine(); bool tf = Double.TryParse(line2, out t); bool tfscale = Double.TryParse(line3, out tscale); if (line1 == "Iterative") { this.Type = "Iterative"; this.Transform = t; this.Transformscale = 0; } if (line1 == "Weibull") { this.Type = "Weibull"; this.Transform = t; this.Transformscale = tscale; } counter++; } } catch { } DataSet data = new DataSet(); OleDbConnection con = new OleDbConnection(c); DataTable dataTable = new DataTable(); string query = string.Format("SELECT * FROM [{0}]", curItem); OleDbDataAdapter adapter = new OleDbDataAdapter(query, con); try { con.Open(); adapter.Fill(dataTable); data.Tables.Add(dataTable); int m = 0, n = 0; m = data.Tables[0].Rows.Count; n = dataTable.Columns.Count; this.R = m; this.C = n; int q = 0, r = 0; try { for (q = 0; q < m; q++) { for (r = 0; r < n; r++) { double d = (double)data.Tables[0].Rows[q].ItemArray[r]; Tseries.Add(d); } } } catch (Exception x) { MessageBox.Show("No data in the underlying tab. Error:" + x.Message); } } catch (Exception ex) { MessageBox.Show("The listbox selection did not work. Error: " + ex.Message); } finally { con.Close(); Modellist.Clear(); this.loadmodels(position); if (Modellist.Count() == 0) { hide(); string message = "No ARIMA models relating to this data set have been found.\nPlease go back to ARIMA section, run the models and save output."; string caption = "ARIMA Models"; MessageBoxButtons buttons = MessageBoxButtons.OK; DialogResult result; result = MessageBox.Show(message, caption, buttons); } else { populatedatagridview(); this.P = new Statistics(Tseries); } this.Cursor = old; } }
//this method is called once the user chooses a tab from the listbox //it (a) loads the time series data (b) calls the Hookup method which will produce a number of graphs and summary statistics c) saves a copy of the underlying data to a CSV file private void explrlb_SelectedValueChanged(object sender, EventArgs e) { System.Windows.Forms.Cursor old = this.Cursor; this.Cursor = Cursors.AppStarting; //clear any data that may already be in the Tseries object Tseries.Clear(); int position = explrlb.SelectedIndex; //protects in the event that the user selectes a blank line if (position == -1) { this.Cursor = old; return; } //otherwise proceed as normal string curItem = Tab[position]; string df = Flocations[position]; string f = ";Extended Properties=\"Excel 12.0;HDR=NO\""; string c = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + df + f; DataSet data = new DataSet(); OleDbConnection con = new OleDbConnection(c); //var DataTable dataTable = new DataTable(); string query = string.Format("SELECT * FROM [{0}]", curItem); OleDbDataAdapter adapter = new OleDbDataAdapter(query, con); try { con.Open(); adapter.Fill(dataTable); data.Tables.Add(dataTable); explrdgv1.AutoGenerateColumns = true; explrdgv1.DataSource = data.Tables[0]; string s = data.Tables[0].TableName; explrdgv1.AllowUserToAddRows = false; explrdgv1.MultiSelect = false; explrdgv1.ReadOnly = true; //now we populate Tseries int m = 0, n = 0; m = data.Tables[0].Rows.Count; n = dataTable.Columns.Count; this.R = m; this.C = n; int q = 0, r = 0; try { for (q = 0; q < m; q++) { for (r = 0; r < n; r++) { double d = (double)data.Tables[0].Rows[q].ItemArray[r]; Tseries.Add(d); } } //call the HookUpData method which will assist with the production of charts etc HookUpData(); } catch (Exception x) { explrForm_hide(); MessageBox.Show("No data in the underlying tab. Error:" + x.Message); } } catch (Exception ex) { MessageBox.Show("The listbox selection did not work. Error: " + ex.Message); } finally { con.Close(); this.Cursor = old; //save down a CSV copy of the underlying data in case the user wants it curItem = curItem.Replace("'", ""); curItem = curItem.Replace("$", ""); string p = curItem + ".csv"; string q = df.Replace(".xlsx", p); StreamWriter sw = new StreamWriter(q, false); sw.WriteLine("Data"); foreach (double d in Tseries) { sw.WriteLine(d); } sw.Close(); } }