// g13fg Example Program Text // C# version, NAG Copyright 2008 using System; using NagLibrary; using System.Globalization; namespace NagDotNetExamples { public class G13FGE { const int ipmax = 5; const int iqmax = 5; const int num = 1500; const int mseed = 1; const int nregmx = 10; const int mnmx = 1; const double zero = 0.00e0; static void Main(String[] args) { StartExample(); } public static void StartExample() { try { double fac1, hp, lgf, mean, tol, xterm; int d, df, genid, i, ip, iq, j, k, maxit, mn, npar, npar2, nreg, nt, subid; bool copts = false, fcall = false; string dist = ""; double[] bx = new double[nregmx]; double[] etm = new double[num]; double[] ht = new double[num]; double[] htm = new double[num]; double[] param = new double[2*iqmax+ipmax+1]; double[] rvec = new double[2*(ipmax + 2*iqmax +2)]; double[,] x = new double[num, Math.Max(nregmx+mnmx, 1)]; double[] yt = new double[num]; int[] seed = new int[mseed]; int ifail; Console.WriteLine("g13fg Example Program Results"); Console.WriteLine(""); df = 0; hp = 0.0; // Initialise the seed seed[0] = 111; // genid and subid identify the base generator genid = 1; subid = 1; // Initialise the generator to a repeatable sequence G05.G05State g05State = new G05.G05State(genid, subid, seed, out ifail); // if (ifail != 0) { Console.WriteLine("** Generator initialisation failed with ifail = {0,5}", ifail); goto L200; } // // Initialise the time dependent, exogenous, variables bx[0] = 1.50e0; bx[1] = 2.50e0; bx[2] = 3.00e0; for (i = 1; i <= num; i++) { fac1 = (double)i * 0.010e0; x[i - 1, 1] = 0.010e0 + 0.70e0 * Math.Sin(fac1); x[i - 1, 0] = 0.50e0 + fac1 * 0.10e0; x[i - 1, 2] = 1.00e0; } // for (d = 1; d <= 2; d++) { if (d == 1) { // Errors distributed as a normal distribution Console.WriteLine(""); Console.WriteLine(" {0}", "Normal distribution"); Console.WriteLine(""); dist = "N"; // Initialise the series parameters required // for generation of some test data mean = 3.00e0; mn = 1; nreg = 2; ip = 1; iq = 1; param[0] = 0.10e0; param[1] = -0.30e0; param[2] = 0.10e0; param[3] = 0.90e0; } else { // Errors distributed as a students t distribution Console.WriteLine(""); Console.WriteLine(" {0}", "Students T distribution"); Console.WriteLine(""); dist = "T"; // Initialise the series parameters required // for generation of some test data mean = 3.00e0; df = 5; mn = 1; nreg = 2; ip = 1; iq = 2; param[0] = 0.10e0; param[1] = -0.30e0; param[2] = -0.10e0; param[3] = 0.10e0; param[4] = 0.30e0; param[5] = 0.70e0; } // // Start of data generation // Generate the errors fcall = true; G05.g05pg(dist, num, ip, iq, param, df, ht, yt, fcall, rvec, g05State, out ifail); // Calculate the observed data for (i = 1; i <= num; i++) { xterm = zero; for (k = 1; k <= nreg; k++) { xterm = xterm + x[i - 1, k - 1] * bx[k - 1]; } if (mn == 1) { yt[i - 1] = mean + xterm + yt[i - 1]; } else { yt[i - 1] = xterm + yt[i - 1]; } } // End of data generation // // Calculate the number of parameters npar = 1 + 2 * iq + ip; npar2 = npar + mn + nreg; if (dist == "T") { npar2 = npar2 + 1; } double[,] covr = new double[npar2, npar2]; double[] sc = new double[npar2]; double[] se = new double[npar2]; double[] theta = new double[npar2]; // // Set the model fitting options copts = true; maxit = 50; tol = 1.00e-5; // // Generate initial values for the alpha_i and beta_j parameters for (i = 1; i <= npar; i++) { theta[i - 1] = param[i - 1] * 0.50e0; } // Generate initial values for df estimate j = npar + 1; if (dist == "T") { theta[j - 1] = df * 0.650e0; j = j + 1; } // Generate the initial values for b_0 if (mn == 1) { theta[j - 1] = mean * 0.50e0; j = j + 1; } // Generate the initial values for the linear regression // coefficients b_i, if required if (!(copts)) { for (i = 1; i <= nreg; i++) { theta[i + j - 1 - 1] = bx[i - 1] * 0.50e0; } } // // Call the analysis method G13.g13fg(dist, yt, x, num, ip, iq, nreg, mn, npar2, theta, se, sc, covr, ref hp, etm, htm, out lgf, copts, maxit, tol, out ifail); // if (ifail == 0) { // Output the results Console.WriteLine(" {0}", " Parameter Standard Correct"); Console.WriteLine(" {0}", " estimates errors values"); // Output the coefficient alpha_0 Console.WriteLine(" {0,16:f3}{1,16:f3}{2,16:f3}", theta[0], se[0], param[0]); // Output the coefficients alpha_i for (i = 2; i <= iq; i++) { Console.WriteLine(" {0,16:f3}{1,16:f3}{2,16:f3}", theta[i - 1], se[i - 1], param[i - 1]); } // Output the coefficients beta_j for (i = iq + 1; i <= npar; i++) { Console.WriteLine(" {0,16:f3}{1,16:f3}{2,16:f3}", theta[i - 1], se[i - 1], param[i - 1]); } j = npar + 1; // Output the estimated degrees of freedom, df if (dist == "T") { Console.WriteLine(" {0,16:f3}{1,16:f3}{2,16:f3}", theta[j - 1], se[j - 1], (double)df); j = j + 1; } // Output the estimated mean term, b_0 if (mn == 1) { Console.WriteLine(" {0,16:f3}{1,16:f3}{2,16:f3}", theta[j - 1], se[j - 1], mean); j = j + 1; } // Output the estimated linear regression coefficients, b_i for (i = 1; i <= nreg; i++) { Console.WriteLine(" {0,16:f3}{1,16:f3}{2,16:f3}", theta[i + j - 1 - 1], se[i + j - 1 - 1], bx[i - 1]); } // // Calculate the volatility forecast nt = 4; double[] cvar = new double[nt]; G13.g13fh(num, nt, ip, iq, theta, cvar, htm, etm, out ifail); // Display the volatility forecast Console.WriteLine(""); Console.WriteLine(" {0}{1,12:f3}", "Volatility forecast = ", cvar[nt - 1]); Console.WriteLine(""); } } // L200: ; // } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine("Exception Raised"); } } } }