// g13fc Example Program Text // C# version, NAG Copyright 2008 using System; using NagLibrary; namespace NagDotNetExamples { public class G13FCE { const int ipmax = 3; const int iqmax = 3; const int nregmx = 10; const int mnmx = 1; const int num = 1500; const int mseed = 1; const double zero = 0.00e0; static void Main(String[] args) { StartExample(); } public static void StartExample() { try { double fac1, gamma, hp, lgf, mean, tol, xterm; int d, df, genid, i, ip, iq, j, k, lseed, lstate, maxit, mn, npar, npar2, nreg, nt, subid; bool fcall = false; string dist = ""; double[] bx = new double[nregmx]; double[] param = new double[ipmax+iqmax+1]; double[,] x = new double[num, num]; double[] yt = new double[num]; int[] seed = new int[mseed]; bool[] copts = new bool[2]; double[] etm = new double[num]; double[] ht = new double[num]; double[] htm = new double[num]; double[] rvec = new double[2*(ipmax+iqmax+1)]; int ifail; Console.WriteLine("g13fc 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); // // 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, 0] = 0.010e0 + 0.70e0 * Math.Sin(fac1); x[i - 1, 1] = 0.50e0 + fac1 * 0.10e0; x[i - 1, 2] = 1.00e0; } // // Show two examples, normally distributed errors, // and student t errors 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; gamma = -0.40e0; ip = 1; iq = 1; param[0] = 0.080e0; param[1] = 0.20e0; param[2] = 0.70e0; } 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 df = 4; mean = 3.00e0; mn = 1; nreg = 2; gamma = -0.40e0; ip = 1; iq = 1; param[0] = 0.10e0; param[1] = 0.10e0; param[2] = 0.80e0; } // // Start of data generation // Generate the errors fcall = true; G05.g05pe(dist, num, ip, iq, param, gamma, 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 = iq + ip + 1; npar2 = npar + mn + nreg + 1; if (dist == "T") { npar2 = npar2 + 1; } double[] theta = new double[npar2]; double[,] covr = new double[npar2, npar2]; double[] sc = new double[npar2]; double[] se = new double[npar2]; // // Set the model fitting options copts[0] = true; copts[1] = 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 the asymmetry parameter, gamma theta[npar + 1 - 1] = gamma * 0.50e0; // Generate initial values for df estimate j = npar + 2; 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[1])) { for (i = 1; i <= nreg; i++) { theta[i + j - 1 - 1] = bx[i - 1] * 0.50e0; } } // // Call the analysis method G13.g13fc(dist, yt, x, num, ip, iq, nreg, mn, npar2, theta, se, sc, covr, ref hp, etm, htm, out lgf, copts, maxit, tol, out ifail); // // Output the results Console.WriteLine(" {0} {1}", " Parameter Standard", " Correct"); Console.WriteLine(" {0} {1}", " estimates errors ", " values"); // Output the coefficient alpha_0 Console.WriteLine(" {0,16:f4}{1,16:f4}{2,16:f4}", theta[0], se[0], param[0]); // Output the coefficients alpha_i for (i = 2; i <= iq; i++) { Console.WriteLine(" {0,16:f4}{1,16:f4}{2,16:f4}", 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:f4}{1,16:f4}{2,16:f4}", theta[i - 1], se[i - 1], param[i - 1]); } // Output the estimated asymmetry parameter, gamma Console.WriteLine(" {0,16:f4}{1,16:f4}{2,16:f4}", theta[npar + 1 - 1], se[npar + 1 - 1], gamma); j = npar + 2; // Output the estimated degrees of freedom, df if (dist == "T") { Console.WriteLine(" {0,16:f4}{1,16:f4}{2,16:f4}", 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:f4}{1,16:f4}{2,16:f4}", 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:f4}{1,16:f4}{2,16:f4}", 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.g13fd(num, nt, ip, iq, theta, gamma, cvar, htm, etm, out ifail); // Display the volatility forecast Console.WriteLine(""); Console.WriteLine(" {0}{1,12:f4}", "Volatility forecast = ", cvar[nt - 1]); Console.WriteLine(""); } // } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine("Exception Raised"); } } } }