// g02qg Example Program Text // C# version, NAG Copyright 2012 using System; using NagLibrary; using System.Globalization; namespace NagDotNetExamples { public class G02QGE { static string datafile = "ExampleData/g02qge.d"; // Specify data file as a command line argument. // It defaults to the named file specified below otherwise. static void Main(String[] args) { if (args.Length == 1) { datafile = args[0]; } StartExample(); } public static void StartExample() { try { DataReader sr = new DataReader(datafile); int lseed = 1; double df; int genid = 0, i = 0, ifail = 0, ip = 0, j = 0, l = 0, ldbl = 0, lddat = 0, ldres = 0, lstate = 0, lwt = 0, m = 0, n = 0, ntau = 0, sorder = 0, subid = 0, tdch = 0; int c1 = 0; string weight = ""; string cvalue = "", semeth = ""; double[,] b; double[,] bl; double[,] bu; double[, ,] ch; double[,] dat; double[,] res; double[] tau; double[] wt; double[] y; int[] info; int[] isx; int[] state; int[] seed = new int[lseed]; Console.WriteLine(" {0}", "G02QGF Example Program Results"); Console.WriteLine(""); // Skip heading in data file sr.Reset(); // Read in the problem size sr.Reset(); sorder = int.Parse(sr.Next()); c1 = int.Parse(sr.Next()); weight = sr.Next(); n = int.Parse(sr.Next()); m = int.Parse(sr.Next()); ntau = int.Parse(sr.Next()); // Read in the data if ((weight == "W") || (weight == "w")) { lwt = (int)n; } else { lwt = 0; } y = new double[n]; isx = new int[m]; tau = new double[ntau]; wt = new double[lwt]; // allocate if (sorder == 1) { // DAT(N,M) lddat = (int)n; dat = new double[n, m]; // allocate if (lwt == 0) { sr.Reset(); for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { dat[i - 1, j - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); y[i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } } } else { sr.Reset(); for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { dat[i - 1, j - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); y[i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); wt[i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } } } } else { // DAT(M,N) lddat = (int)m; dat = new double[m, n]; // allocate if (lwt == 0) { sr.Reset(); for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { dat[j - 1, i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); y[i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } } } else { sr.Reset(); for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { dat[j - 1, i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); y[i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); wt[i - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } } } } // Read in variable inclusion flags sr.Reset(); for (j = 1; j <= m; j++) { isx[j - 1] = int.Parse(sr.Next()); } // Calculate IP ip = 0; for (j = 1; j <= m; j++) { if (isx[j - 1] == 1) { ip++; } } if (c1 == 1) { ip++; } // Read in the quantiles required sr.Reset(); for (j = 1; j <= ntau; j++) { tau[j - 1] = double.Parse(sr.Next(), CultureInfo.InvariantCulture); } // allocate // Initialize the optional argument array ifail = 0; G02.g02qgOptions options = new G02.g02qgOptions(); /* // Begin commented out block while (true) { // Read in any optional arguments. Reads in to the end of // the input data, or until a blank line is reached string opstr; try{ sr.Reset(); optstr = sr.Next(); // Set the supplied option options.Set(opstr); } catch { break; } } // end commented out block */ // options.Set(sr); options.Set("Return Residuals = Yes"); options.Set("Matrix Returned = Covariance"); options.Set("Interval Method = IID"); // Assume that no intervals or output matrices are required // unless the optional argument state differently ldbl = 0; tdch = 0; ldres = 0; lstate = 0; G05.G05State g05State; // Query the optional arguments to see what output is required options.Get("INTERVAL METHOD", ref semeth); String matrix_returned = ""; options.Get("MATRIX RETURNED", ref matrix_returned); Console.WriteLine("SEMETH {0}", semeth); Console.WriteLine("**************"); if (semeth != "NONE") { // Require the intervals to be output ldbl = (int)ip; if (semeth == "BOOTSTRAP XY") { // Need to find the length of the state array for the random // number generator // Read in the generator ID and a seed sr.Reset(); genid = int.Parse(sr.Next()); subid = int.Parse(sr.Next()); seed[0] = int.Parse(sr.Next()); // Initialise the generator to a repeatable sequence g05State = new G05.G05State(genid, subid, seed, out ifail); } ifail = 0; if (matrix_returned == "COVARIANCE") { tdch = (int)ntau; } else if (matrix_returned == "H INVERSE") { if ((semeth == "BOOTSTRAP XY") || (semeth == "IID")) { // NB: If we are using bootstrap or IID errors then any request for // H INVERSE is ignored tdch = 0; } else { tdch = ntau + 1; } } ifail = 0; options.Get("RETURN RESIDUALS", ref cvalue); if (cvalue == "YES") { ldres = (int)n; } } // Allocate memory for output arrays // allocate if (lstate > 0) { // Doing bootstrap, so initialise the RNG ifail = 0; g05State = new G05.G05State(genid, subid, seed, out ifail); } else { // initializing state as a fallback case, is not used seed[0] = 1; g05State = new G05.G05State(1, subid, seed, out ifail); } // Call the model fitting routine ifail = -1; b = new double[ntau, ip]; info = new int[ntau]; bl = new double[ntau, ldbl]; bu = new double[ntau, ldbl]; ch = new double[tdch, ldbl, ldbl]; state = new int[lstate]; res = new double[ldres, ntau]; G02.g02qg(sorder, c1, n, m, dat, isx, ip, y, wt, ntau, tau, out df, b, bl, bu, ch, res, options, g05State, info, out ifail); if (ifail != 0) { if (ifail == 231) { for (j = 1; j <= ntau; j++) { Console.WriteLine(" {0} {1}", "Additional error information (INFO): ", info[j - 1]); } } else { goto L100; } } // Display the parameter estimates for (l = 1; l <= ntau; l++) { Console.WriteLine(" {0}{1,6:f3}", "Quantile: ", tau[l - 1]); Console.WriteLine(""); if (ldbl > 0) { Console.WriteLine(" {0}", " Lower Parameter Upper"); Console.WriteLine(" {0}", " Limit Estimate Limit"); } else { Console.WriteLine(" {0}", " Parameter"); Console.WriteLine(" {0}", " Estimate"); } for (j = 1; j <= ip; j++) { if (ldbl > 0) { Console.WriteLine(" {0,3} {1,7:f3} {2,7:f3} {3,7:f3}", j, bl[l - 1, j - 1], b[l - 1, j - 1], bu[l - 1, j - 1]); } else { Console.WriteLine(" {0,3} {1,7:f3} ", j, b[l - 1, j - 1]); } } Console.WriteLine(""); if (tdch == ntau) { Console.WriteLine(" {0}", "Covariance matrix"); for (i = 1; i <= ip; i++) { Console.WriteLine(""); for (j = 1; j <= i; j++) { Console.Write(" {0,10:e3} ", ch[l - 1, i - 1, j - 1]); } } Console.WriteLine(""); } else if (tdch == ntau + 1) { Console.WriteLine(" {0}", "J"); for (i = 1; i <= ip; i++) { for (j = 1; j <= i; j++) { Console.WriteLine(" {0,10:e3} ", ch[j - 1, i - 1, 0]); } } Console.WriteLine(""); Console.WriteLine(" {0}", "H inverse"); for (i = 1; i <= ip; i++) { for (j = 1; j <= i; j++) { Console.WriteLine(" {0,10:e3} ", ch[j - 1, i - 1, l + 1 - 1]); } } Console.WriteLine(""); } Console.WriteLine(""); } if (ldres > 0) { Console.WriteLine(" {0}", "First 10 Residuals"); Console.Write(" {0}\r\n ", "Obs Quantile\r\n"); for (j = 1; j <= ntau; j++) { Console.Write(" {0,6:f3} ", tau[j - 1]); } Console.WriteLine(""); for (i = 1; i <= Math.Min(n, 10); i++) { Console.Write("\r\n{0,3}",i); for (j = 1; j <= ntau; j++) { Console.Write(" {0,10:f5} ", res[i - 1, j - 1]); } } } else { Console.WriteLine(" {0}", "Residuals not returned"); } Console.WriteLine(""); L100: ; } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine("Exception Raised"); } } } }