private static void correlation_test01() //****************************************************************************80 // // Purpose: // // CORRELATION_TEST01 plots the correlation functions. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 11 November 2012 // // Author: // // John Burkardt // { Console.WriteLine(""); Console.WriteLine("CORRELATION_TEST01"); Console.WriteLine(" Make plots of correlation functions."); Console.WriteLine(""); const int n = 101; FullertonLib.BesselData globaldata = new(); // // besselj // double rho0 = 1.0; FullertonLib.r8BESJ0Data j0data = new(); double[] rho = typeMethods.r8vec_linspace_new(n, -8.0, 8.0); Correlation.CorrelationResult tr = Correlation.correlation_besselj(globaldata, j0data, n, rho, rho0); double[] c = tr.result; globaldata = tr.data; j0data = tr.j0data; Plot.correlation_plot(n, rho, c, "besselj", "Bessel J correlation"); // // besselk // rho0 = 1.0; FullertonLib.r8BESK1Data k1data = new(); rho = typeMethods.r8vec_linspace_new(n, -4.0, 4.0); tr = Correlation.correlation_besselk(globaldata, k1data, n, rho, rho0); c = tr.result; globaldata = tr.data; k1data = tr.k1data; Plot.correlation_plot(n, rho, c, "besselk", "Bessel K correlation"); // // circular // rho0 = 1.0; rho = typeMethods.r8vec_linspace_new(n, -2.0, 2.0); tr = Correlation.correlation_circular(globaldata, k1data, n, rho, rho0); c = tr.result; globaldata = tr.data; Plot.correlation_plot(n, rho, c, "circular", "Circular correlation"); // // constant // rho0 = 1.0; rho = typeMethods.r8vec_linspace_new(n, -2.0, 2.0); tr = Correlation.correlation_constant(globaldata, k1data, n, rho, rho0); c = tr.result; globaldata = tr.data; Plot.correlation_plot(n, rho, c, "constant", "Constant correlation"); // // cubic // rho0 = 1.0; rho = typeMethods.r8vec_linspace_new(n, -2.0, 2.0); tr = Correlation.correlation_cubic(globaldata, k1data, n, rho, rho0); c = tr.result; globaldata = tr.data; Plot.correlation_plot(n, rho, c, "cubic", "Cubic correlation"); // // damped_cosine // rho0 = 1.0; rho = typeMethods.r8vec_linspace_new(n, -6.0, 6.0); tr = Correlation.correlation_damped_cosine(globaldata, k1data, n, rho, rho0); c = tr.result; globaldata = tr.data; Plot.correlation_plot(n, rho, c, "damped_cosine", "Damped cosine correlation"); // // damped_sine // rho0 = 1.0; rho = typeMethods.r8vec_linspace_new(n, -12.0, 12.0); tr = Correlation.correlation_damped_sine(globaldata, j0data, n, rho, rho0); c = tr.result; globaldata = tr.data; Plot.correlation_plot(n, rho, c, "damped_sine", "Damped sine correlation"); // // exponential // rho0 = 1.0; rho = typeMethods.r8vec_linspace_new(n, -2.0, 2.0); tr = Correlation.correlation_exponential(globaldata, k1data, n, rho, rho0); c = tr.result; globaldata = tr.data; Plot.correlation_plot(n, rho, c, "exponential", "Exponential correlation"); // // gaussian // rho0 = 1.0; rho = typeMethods.r8vec_linspace_new(n, -2.0, 2.0); tr = Correlation.correlation_gaussian(globaldata, j0data, n, rho, rho0); c = tr.result; globaldata = tr.data; Plot.correlation_plot(n, rho, c, "gaussian", "Gaussian correlation"); // // hole // rho0 = 1.0; rho = typeMethods.r8vec_linspace_new(n, -6.0, 6.0); tr = Correlation.correlation_hole(globaldata, k1data, n, rho, rho0); c = tr.result; globaldata = tr.data; Plot.correlation_plot(n, rho, c, "hole", "Hole correlation"); // // linear // rho0 = 1.0; rho = typeMethods.r8vec_linspace_new(n, -2.0, 2.0); tr = Correlation.correlation_linear(globaldata, k1data, n, rho, rho0); c = tr.result; globaldata = tr.data; Plot.correlation_plot(n, rho, c, "linear", "Linear correlation"); // // matern, nu = 2.5 // rho0 = 1.0; FullertonLib.r8BESKData kdata = new(); rho = typeMethods.r8vec_linspace_new(n, -2.0, 2.0); tr = Correlation.correlation_matern(globaldata, kdata, n, rho, rho0); c = tr.result; globaldata = tr.data; kdata = tr.kdata; Plot.correlation_plot(n, rho, c, "matern", "Matern correlation (NU = 2.5)"); // // pentaspherical // rho0 = 1.0; rho = typeMethods.r8vec_linspace_new(n, -2.0, 2.0); tr = Correlation.correlation_pentaspherical(globaldata, k1data, n, rho, rho0); c = tr.result; globaldata = tr.data; k1data = tr.k1data; Plot.correlation_plot(n, rho, c, "pentaspherical", "Pentaspherical correlation"); // // power, e = 2.0 // rho0 = 1.0; rho = typeMethods.r8vec_linspace_new(n, -2.0, 2.0); tr = Correlation.correlation_power(globaldata, k1data, n, rho, rho0); c = tr.result; globaldata = tr.data; k1data = tr.k1data; Plot.correlation_plot(n, rho, c, "power", "Power correlation"); // // rational_quadratic // rho0 = 1.0; rho = typeMethods.r8vec_linspace_new(n, -4.0, 4.0); tr = Correlation.correlation_rational_quadratic(globaldata, j0data, n, rho, rho0); c = tr.result; globaldata = tr.data; j0data = tr.j0data; Plot.correlation_plot(n, rho, c, "rational_quadratic", "Rational quadratic correlation"); // // spherical // rho0 = 1.0; rho = typeMethods.r8vec_linspace_new(n, -2.0, 2.0); tr = Correlation.correlation_spherical(globaldata, k1data, n, rho, rho0); c = tr.result; globaldata = tr.data; k1data = tr.k1data; Plot.correlation_plot(n, rho, c, "spherical", "Spherical correlation"); // // white_noise // rho0 = 1.0; rho = typeMethods.r8vec_linspace_new(n, -2.0, 2.0); tr = Correlation.correlation_white_noise(globaldata, k1data, n, rho, rho0); c = tr.result; globaldata = tr.data; k1data = tr.k1data; Plot.correlation_plot(n, rho, c, "white_noise", "White noise correlation"); }
public static Correlation.CorrelationResult sample_paths_cholesky(FullertonLib.BesselData globaldata, FullertonLib.r8BESKData kdata, int n, int n2, double rhomax, double rho0, Func <FullertonLib.BesselData, FullertonLib.r8BESKData, int, double[], double, Correlation.CorrelationResult> correlation, ref typeMethods.r8vecNormalData data, ref int seed) //****************************************************************************80 // // Purpose: // // SAMPLE_PATHS_CHOLESKY: sample paths for stationary correlation functions. // // Discussion: // // This method uses the Cholesky factorization of the correlation matrix. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 11 November 2012 // // Author: // // John Burkardt // // Parameters: // // Input, int N, the number of points on each path. // // Input, int N2, the number of paths. // // Input, double RHOMAX, the maximum value of RHO. // // Input, double RHO0, the correlation length. // // Input, double *CORRELATION ( int n, double rho_vec[], double rho0), // the name of the function which evaluates the correlation. // // Input/output, int &SEED, a seed for the random number // generator. // // Output, double X[N*N2], the sample paths. // { int flag = 0; int j; // // Choose N equally spaced sample points from 0 to RHOMAX. // const double rhomin = 0.0; double[] rho_vec = typeMethods.r8vec_linspace_new(n, rhomin, rhomax); // // Evaluate the correlation function. // Correlation.CorrelationResult tr = correlation(globaldata, kdata, n, rho_vec, rho0); double[] cor_vec = tr.result; globaldata = tr.data; // // Construct the correlation matrix; // // From the vector // [ C(0), C(1), C(2), ... C(N-1) ] // construct the vector // [ C(N-1), ..., C(2), C(1), C(0), C(1), C(2), ... C(N-1) ] // Every row of the correlation matrix can be constructed by a subvector // of this vector. // double[] cor = new double[n * n]; for (j = 0; j < n; j++) { int i; for (i = 0; i < n; i++) { int k = typeMethods.i4_wrap(j - i, 0, n - 1); cor[i + j * n] = cor_vec[k]; } } // // Get the Cholesky factorization of COR: // // COR = L * L'. // double[] l = typeMethods.r8mat_cholesky_factor(n, cor, ref flag); switch (flag) { // // The matrix might not be nonnegative definite. // case 2: Console.WriteLine(""); Console.WriteLine("SAMPLE_PATHS_CHOLESKY - Fatal error!"); Console.WriteLine(" The correlation matrix is not"); Console.WriteLine(" symmetric nonnegative definite."); return(null); } // // Compute a matrix of N by N2 normally distributed values. // double[] r = typeMethods.r8mat_normal_01_new(n, n2, ref data, ref seed); // // Compute the sample path. // double[] x = typeMethods.r8mat_mm_new(n, n, n2, l, r); Correlation.CorrelationResult res = new() { result = x, data = globaldata }; return(res); }
private static void correlation_test02() //****************************************************************************80 // // Purpose: // // CORRELATION_TEST02 plots sample paths with SAMPLE_PATHS_CHOLESKY. // // Discussion: // // Most paths will be blue, but make the LAST one red so that there will // always be one distinguished path that is easy to follow. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 11 November 2012 // // Author: // // John Burkardt // { const double rho0 = 1.0; const double rhomax = 10.0; const double rhomin = 0.0; Console.WriteLine(""); Console.WriteLine("CORRELATION_TEST02"); Console.WriteLine(" SAMPLE_PATHS_CHOLESKY generates sample paths from the"); Console.WriteLine(" correlation matrix, factored using the Cholesky factor."); Console.WriteLine(" It requires that the correlation matrix is nonnegative definite."); Console.WriteLine(""); Console.WriteLine(" SAMPLE_PATHS_EIGEN generates sample paths from the"); Console.WriteLine(" correlation matrix, factored using the eigen factorization."); Console.WriteLine(" If the correlation matrix is not nonnegative definite,"); Console.WriteLine(" we simply suppress negative eigenvalues."); Console.WriteLine(""); FullertonLib.BesselData globaldata = new(); const int n = 101; const int n2 = 3; double[] rho = typeMethods.r8vec_linspace_new(n, rhomin, rhomax); // // besselj // Use EIGEN, because CHOLESKY fails. // int seed = 123456789; typeMethods.r8vecNormalData data = new(); FullertonLib.r8BESJ0Data jdata = new(); Correlation.CorrelationResult tr = SamplePaths.sample_paths_eigen(globaldata, jdata, n, n2, rhomax, rho0, Correlation.correlation_besselj, ref data, ref seed); globaldata = tr.data; double[] x = tr.result; jdata = tr.j0data; Paths.paths_plot(n, n2, rho, x, "besselj", "Bessel J correlation"); // // besselk // seed = 123456789; FullertonLib.r8BESK1Data k1data = new(); tr = SamplePaths.sample_paths_cholesky(globaldata, k1data, n, n2, rhomax, rho0, Correlation.correlation_besselk, ref data, ref seed); globaldata = tr.data; x = tr.result; k1data = tr.k1data; Paths.paths_plot(n, n2, rho, x, "besselk", "Bessel K correlation"); // // circular // seed = 123456789; tr = SamplePaths.sample_paths_cholesky(globaldata, k1data, n, n2, rhomax, rho0, Correlation.correlation_circular, ref data, ref seed); globaldata = tr.data; x = tr.result; k1data = tr.k1data; Paths.paths_plot(n, n2, rho, x, "circular", "Circular correlation"); // // constant // seed = 123456789; tr = SamplePaths.sample_paths_cholesky(globaldata, k1data, n, n2, rhomax, rho0, Correlation.correlation_constant, ref data, ref seed); globaldata = tr.data; x = tr.result; k1data = tr.k1data; Paths.paths_plot(n, n2, rho, x, "constant", "Constant correlation"); // // cubic // seed = 123456789; tr = SamplePaths.sample_paths_cholesky(globaldata, k1data, n, n2, rhomax, rho0, Correlation.correlation_cubic, ref data, ref seed); globaldata = tr.data; x = tr.result; k1data = tr.k1data; Paths.paths_plot(n, n2, rho, x, "cubic", "Cubic correlation"); // // damped_cosine // seed = 123456789; tr = SamplePaths.sample_paths_cholesky(globaldata, k1data, n, n2, rhomax, rho0, Correlation.correlation_damped_cosine, ref data, ref seed); globaldata = tr.data; x = tr.result; k1data = tr.k1data; Paths.paths_plot(n, n2, rho, x, "damped_cosine", "Damped cosine correlation"); // // damped_sine // Use EIGEN, because CHOLESKY fails. // seed = 123456789; tr = SamplePaths.sample_paths_eigen(globaldata, jdata, n, n2, rhomax, rho0, Correlation.correlation_damped_sine, ref data, ref seed); globaldata = tr.data; x = tr.result; jdata = tr.j0data; Paths.paths_plot(n, n2, rho, x, "damped_sine", "Damped sine correlation"); // // exponential // seed = 123456789; tr = SamplePaths.sample_paths_cholesky(globaldata, k1data, n, n2, rhomax, rho0, Correlation.correlation_exponential, ref data, ref seed); globaldata = tr.data; x = tr.result; k1data = tr.k1data; Paths.paths_plot(n, n2, rho, x, "exponential", "Exponential correlation"); // // gaussian // Use EIGEN, because CHOLESKY fails. // seed = 123456789; tr = SamplePaths.sample_paths_eigen(globaldata, jdata, n, n2, rhomax, rho0, Correlation.correlation_gaussian, ref data, ref seed); globaldata = tr.data; x = tr.result; jdata = tr.j0data; Paths.paths_plot(n, n2, rho, x, "gaussian", "Gaussian correlation"); // // hole // seed = 123456789; tr = SamplePaths.sample_paths_cholesky(globaldata, k1data, n, n2, rhomax, rho0, Correlation.correlation_hole, ref data, ref seed); globaldata = tr.data; x = tr.result; k1data = tr.k1data; Paths.paths_plot(n, n2, rho, x, "hole", "Hole correlation"); // // linear // seed = 123456789; tr = SamplePaths.sample_paths_cholesky(globaldata, k1data, n, n2, rhomax, rho0, Correlation.correlation_linear, ref data, ref seed); globaldata = tr.data; x = tr.result; k1data = tr.k1data; Paths.paths_plot(n, n2, rho, x, "linear", "Linear correlation"); // // matern ( nu = 2.5 ) // seed = 123456789; FullertonLib.r8BESKData kdata = new(); tr = SamplePaths.sample_paths_cholesky(globaldata, kdata, n, n2, rhomax, rho0, Correlation.correlation_matern, ref data, ref seed); globaldata = tr.data; x = tr.result; kdata = tr.kdata; Paths.paths_plot(n, n2, rho, x, "matern", "Matern correlation (nu=2.5)"); // // pentaspherical // seed = 123456789; tr = SamplePaths.sample_paths_cholesky(globaldata, k1data, n, n2, rhomax, rho0, Correlation.correlation_pentaspherical, ref data, ref seed); globaldata = tr.data; x = tr.result; k1data = tr.k1data; Paths.paths_plot(n, n2, rho, x, "pentaspherical", "Pentaspherical correlation"); // // power ( e = 2.0 ) // seed = 123456789; tr = SamplePaths.sample_paths_cholesky(globaldata, k1data, n, n2, rhomax, rho0, Correlation.correlation_power, ref data, ref seed); globaldata = tr.data; x = tr.result; k1data = tr.k1data; Paths.paths_plot(n, n2, rho, x, "power", "Power correlation (e=2.0)"); // // rational_quadratic // Use EIGEN, because CHOLESKY fails. // seed = 123456789; tr = SamplePaths.sample_paths_eigen(globaldata, jdata, n, n2, rhomax, rho0, Correlation.correlation_rational_quadratic, ref data, ref seed); globaldata = tr.data; x = tr.result; jdata = tr.j0data; Paths.paths_plot(n, n2, rho, x, "rational_quadratic", "Rational quadratic correlation"); // // spherical // seed = 123456789; tr = SamplePaths.sample_paths_cholesky(globaldata, k1data, n, n2, rhomax, rho0, Correlation.correlation_spherical, ref data, ref seed); globaldata = tr.data; x = tr.result; k1data = tr.k1data; Paths.paths_plot(n, n2, rho, x, "spherical", "Spherical correlation"); // // white_noise // seed = 123456789; tr = SamplePaths.sample_paths_cholesky(globaldata, k1data, n, n2, rhomax, rho0, Correlation.correlation_white_noise, ref data, ref seed); globaldata = tr.data; x = tr.result; k1data = tr.k1data; Paths.paths_plot(n, n2, rho, x, "white_noise", "White noise correlation"); }
public static Correlation.CorrelationResult sample_paths_eigen(FullertonLib.BesselData globaldata, FullertonLib.r8BESJ0Data jdata, int n, int n2, double rhomax, double rho0, Func <FullertonLib.BesselData, FullertonLib.r8BESJ0Data, int, double[], double, Correlation.CorrelationResult> correlation, ref typeMethods.r8vecNormalData data, ref int seed) //****************************************************************************80 // // Purpose: // // SAMPLE_PATHS_EIGEN: sample paths for stationary correlation functions. // // Discussion: // // This method uses the eigen-decomposition of the correlation matrix. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 12 November 2012 // // Author: // // John Burkardt // // Parameters: // // Input, int N, the number of points on each path. // // Input, int N2, the number of paths. // // Input, double RHOMAX, the maximum value of RHO. // // Input, double RHO0, the correlation length. // // Input, double *CORRELATION ( int n, double rho_vec[], double rho0), // the name of the function which evaluates the correlation. // // Input/output, int &SEED, a seed for the random number // generator. // // Output, double X[N*N2], the sample paths. // { int i; int j; int k; // // Choose N equally spaced sample points from 0 to RHOMAX. // const double rhomin = 0.0; double[] rho_vec = typeMethods.r8vec_linspace_new(n, rhomin, rhomax); // // Evaluate the correlation function. // Correlation.CorrelationResult tr = correlation(globaldata, jdata, n, rho_vec, rho0); double[] cor_vec = tr.result; globaldata = tr.data; jdata = tr.j0data; // // Construct the correlation matrix; // // From the vector // [ C(0), C(1), C(2), ... C(N-1) ] // construct the vector // [ C(N-1), ..., C(2), C(1), C(0), C(1), C(2), ... C(N-1) ] // Every row of the correlation matrix can be constructed by a subvector // of this vector. // double[] cor = new double[n * n]; for (j = 0; j < n; j++) { for (i = 0; i < n; i++) { k = typeMethods.i4_wrap(Math.Abs(i - j), 0, n - 1); cor[i + j * n] = cor_vec[k]; } } // // Get the eigendecomposition of COR: // // COR = V * D * V'. // // Because COR is symmetric, V is orthogonal. // double[] d = new double[n]; double[] w = new double[n]; double[] v = new double[n * n]; TRED2.tred2(n, cor, ref d, ref w, ref v); TQL2.tql2(n, ref d, ref w, v); // // We assume COR is non-negative definite, and hence that there // are no negative eigenvalues. // double dmin = typeMethods.r8vec_min(n, d); if (dmin < -Math.Sqrt(typeMethods.r8_epsilon())) { Console.WriteLine(""); Console.WriteLine("SAMPLE_PATHS_EIGEN - Warning!"); Console.WriteLine(" Negative eigenvalues observed as low as " + dmin + ""); } for (i = 0; i < n; i++) { d[i] = Math.Max(d[i], 0.0); } // // Compute the eigenvalues of the factor C. // for (i = 0; i < n; i++) { d[i] = Math.Sqrt(d[i]); } // // Compute C, such that C' * C = COR. // double[] c = new double[n * n]; for (j = 0; j < n; j++) { for (i = 0; i < n; i++) { c[i + j * n] = 0.0; for (k = 0; k < n; k++) { c[i + j * n] += d[k] * v[i + k * n] * v[j + k * n]; } } } // // Compute N by N2 independent random normal values. // double[] r = typeMethods.r8mat_normal_01_new(n, n2, ref data, ref seed); // // Multiply to get the variables X which have correlation COR. // double[] x = typeMethods.r8mat_mm_new(n, n, n2, c, r); Correlation.CorrelationResult result = new() { result = x, data = globaldata }; return(result); }