Exemplo n.º 1
0
    public static CorrelationResult correlation_damped_sine(FullertonLib.BesselData globaldata, FullertonLib.r8BESJ0Data data, int n, double[] rho, double rho0)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    CORRELATION_DAMPED_SINE evaluates the damped sine correlation function.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    11 November 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Reference:
    //
    //    Petter Abrahamsen,
    //    A Review of Gaussian Random Fields and Correlation Functions,
    //    Norwegian Computing Center, 1997.
    //
    //  Parameters:
    //
    //    Input, int N, the number of arguments.
    //
    //    Input, double RHO[N], the arguments.
    //
    //    Input, double RHO0, the correlation length.
    //
    //    Output, double C[N], the correlations.
    //
    {
        int i;

        double[] c = new double[n];

        for (i = 0; i < n; i++)
        {
            switch (rho[i])
            {
            case 0.0:
                c[i] = 1.0;
                break;

            default:
                double rhohat = Math.Abs(rho[i]) / rho0;
                c[i] = Math.Sin(rhohat) / rhohat;
                break;
            }
        }

        return(new CorrelationResult {
            result = c, data = globaldata, j0data = data
        });
    }
Exemplo n.º 2
0
    public static CorrelationResult correlation_power(FullertonLib.BesselData globaldata, FullertonLib.r8BESK1Data data, int n, double[] rho, double rho0)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    CORRELATION_POWER evaluates the power correlation function.
    //
    //  Discussion:
    //
    //    In order to be able to call this routine under a dummy name, I had
    //    to drop E from the argument list.
    //
    //    The power correlation is
    //
    //      C(rho) = ( 1 - |rho| )^e  if 0 <= |rho| <= 1
    //             = 0                otherwise
    //
    //      The constraint on the exponent is 2 <= e.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    11 November 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int N, the number of arguments.
    //
    //    Input, double RHO[N], the arguments.
    //    0.0 <= RHO.
    //
    //    Input, double RHO0, the correlation length.
    //    0.0 < RHO0.
    //
    //    Input, double E, the exponent.
    //    E has a default value of 2.0;
    //    2.0 <= E.
    //
    //    Output, double C[N], the correlations.
    //
    {
        int i;

        const double e = 2.0;

        double[] c = new double[n];

        for (i = 0; i < n; i++)
        {
            double rhohat = Math.Abs(rho[i]) / rho0;
            c[i] = rhohat switch
            {
Exemplo n.º 3
0
    public static CorrelationResult correlation_pentaspherical(FullertonLib.BesselData globaldata, FullertonLib.r8BESK1Data data, int n, double[] rho, double rho0)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    CORRELATION_PENTASPHERICAL evaluates the pentaspherical correlation function.
    //
    //  Discussion:
    //
    //    This correlation is based on the volume of overlap of two spheres
    //    of radius RHO0 and separation RHO.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    11 November 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Reference:
    //
    //    Petter Abrahamsen,
    //    A Review of Gaussian Random Fields and Correlation Functions,
    //    Norwegian Computing Center, 1997.
    //
    //  Parameters:
    //
    //    Input, int N, the number of arguments.
    //
    //    Input, double RHO[N], the arguments.
    //
    //    Input, double RHO0, the correlation length.
    //
    //    Output, double C[N], the correlations.
    //
    {
        int i;

        double[] c = new double[n];

        for (i = 0; i < n; i++)
        {
            double rhohat = Math.Min(Math.Abs(rho[i]) / rho0, 1.0);

            c[i] = 1.0 - 1.875 * rhohat + 1.25 * Math.Pow(rhohat, 3)
                   - 0.375 * Math.Pow(rhohat, 5);
        }

        return(new CorrelationResult {
            result = c, data = globaldata, k1data = data
        });
    }
Exemplo n.º 4
0
    public static CorrelationResult correlation_cubic(FullertonLib.BesselData globaldata, FullertonLib.r8BESK1Data data, int n, double[] rho, double rho0)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    CORRELATION_CUBIC evaluates the cubic correlation function.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    11 November 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Reference:
    //
    //    Petter Abrahamsen,
    //    A Review of Gaussian Random Fields and Correlation Functions,
    //    Norwegian Computing Center, 1997.
    //
    //  Parameters:
    //
    //    Input, int N, the number of arguments.
    //
    //    Input, double RHO[N], the arguments.
    //
    //    Input, double RHO0, the correlation length.
    //
    //    Output, double C[N], the correlations.
    //
    {
        int i;

        double[] c = new double[n];

        for (i = 0; i < n; i++)
        {
            double rhohat = Math.Min(Math.Abs(rho[i]) / rho0, 1.0);

            c[i] = 1.0
                   - 7.0 * Math.Pow(rhohat, 2)
                   + 8.75 * Math.Pow(rhohat, 3)
                   - 3.5 * Math.Pow(rhohat, 5)
                   + 0.75 * Math.Pow(rhohat, 7);
        }

        return(new CorrelationResult {
            result = c, data = globaldata, k1data = data
        });
    }
Exemplo n.º 5
0
    public static CorrelationResult correlation_linear(FullertonLib.BesselData globaldata, FullertonLib.r8BESK1Data data, int n, double[] rho, double rho0)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    CORRELATION_LINEAR evaluates the linear correlation function.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    11 November 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Reference:
    //
    //    Petter Abrahamsen,
    //    A Review of Gaussian Random Fields and Correlation Functions,
    //    Norwegian Computing Center, 1997.
    //
    //  Parameters:
    //
    //    Input, int N, the number of arguments.
    //
    //    Input, double RHO[N], the arguments.
    //
    //    Input, double RHO0, the correlation length.
    //
    //    Output, double C[N], the correlations.
    //
    {
        int i;

        double[] c = new double[n];

        for (i = 0; i < n; i++)
        {
            if (rho0 < Math.Abs(rho[i]))
            {
                c[i] = 0.0;
            }
            else
            {
                c[i] = (rho0 - Math.Abs(rho[i])) / rho0;
            }
        }
        return(new CorrelationResult {
            result = c, data = globaldata, k1data = data
        });
    }
Exemplo n.º 6
0
    public static CorrelationResult correlation_white_noise(FullertonLib.BesselData globaldata, FullertonLib.r8BESK1Data data, int n, double[] rho, double rho0)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    CORRELATION_WHITE_NOISE evaluates the white noise correlation function.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    03 November 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Reference:
    //
    //    Petter Abrahamsen,
    //    A Review of Gaussian Random Fields and Correlation Functions,
    //    Norwegian Computing Center, 1997.
    //
    //  Parameters:
    //
    //    Input, int N, the number of arguments.
    //
    //    Input, double RHO[N], the arguments.
    //
    //    Input, double RHO0, the correlation length.
    //
    //    Output, double C[N], the correlations.
    //
    {
        int i;

        double[] c = new double[n];

        for (i = 0; i < n; i++)
        {
            c[i] = rho[i] switch
            {
                0.0 => 1.0,
                _ => 0.0
            };
        }

        return(new CorrelationResult {
            result = c, data = globaldata, k1data = data
        });
    }
}
Exemplo n.º 7
0
    public static CorrelationResult correlation_besselj(FullertonLib.BesselData globaldata, FullertonLib.r8BESJ0Data data, int n, double[] rho, double rho0)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    CORRELATION_BESSELJ evaluates the Bessel J correlation function.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    11 November 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Reference:
    //
    //    Petter Abrahamsen,
    //    A Review of Gaussian Random Fields and Correlation Functions,
    //    Norwegian Computing Center, 1997.
    //
    //  Parameters:
    //
    //    Input, int N, the number of arguments.
    //
    //    Input, double RHO[N], the arguments.
    //
    //    Input, double RHO0, the correlation length.
    //
    //    Output, double C[N], the correlations.
    //
    {
        int i;

        double[] c = new double[n];

        for (i = 0; i < n; i++)
        {
            double rhohat = Math.Abs(rho[i]) / rho0;
            c[i] = FullertonLib.r8_besj0(ref globaldata, ref data, rhohat);
        }

        CorrelationResult result = new() { result = c, data = globaldata, j0data = data };

        return(result);
    }
Exemplo n.º 8
0
    public static CorrelationResult correlation_rational_quadratic(FullertonLib.BesselData globaldata, FullertonLib.r8BESJ0Data data, int n, double[] rho, double rho0)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    CORRELATION_RATIONAL_QUADRATIC: rational quadratic correlation function.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    11 November 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Reference:
    //
    //    Petter Abrahamsen,
    //    A Review of Gaussian Random Fields and Correlation Functions,
    //    Norwegian Computing Center, 1997.
    //
    //  Parameters:
    //
    //    Input, int N, the number of arguments.
    //
    //    Input, double RHO[N], the arguments.
    //
    //    Input, double RHO0, the correlation length.
    //
    //    Output, double C[N], the correlations.
    //
    {
        int i;

        double[] c = new double[n];

        for (i = 0; i < n; i++)
        {
            c[i] = 1.0 / (1.0 + Math.Pow(rho[i] / rho0, 2));
        }

        return(new CorrelationResult {
            result = c, data = globaldata, j0data = data
        });
    }
Exemplo n.º 9
0
    public static CorrelationResult correlation_hole(FullertonLib.BesselData globaldata, FullertonLib.r8BESK1Data data, int n, double[] rho, double rho0)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    CORRELATION_HOLE evaluates the hole correlation function.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    11 November 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int N, the number of arguments.
    //
    //    Input, double RHO[N], the arguments.
    //
    //    Input, double RHO0, the correlation length.
    //
    //    Output, double C[N], the correlations.
    //
    {
        int i;

        double[] c = new double[n];

        for (i = 0; i < n; i++)
        {
            c[i] = (1.0 - Math.Abs(rho[i]) / rho0)
                   * Math.Exp(-Math.Abs(rho[i]) / rho0);
        }
        return(new CorrelationResult {
            result = c, data = globaldata, k1data = data
        });
    }
Exemplo n.º 10
0
    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);
    }
Exemplo n.º 11
0
    public static CorrelationResult correlation_matern(FullertonLib.BesselData globaldata, FullertonLib.r8BESKData data, int n, double[] rho, double rho0)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    CORRELATION_MATERN evaluates the Matern correlation function.
    //
    //  Discussion:
    //
    //    In order to call this routine under a dummy name, I had to drop NU from
    //    the parameter list.
    //
    //    The Matern correlation is
    //
    //      rho1 = 2 * sqrt ( nu ) * rho / rho0
    //
    //      c(rho) = ( rho1 )^nu * BesselK ( nu, rho1 )
    //               / gamma ( nu ) / 2 ^ ( nu - 1 )
    //
    //    The Matern covariance has the form:
    //
    //      K(rho) = sigma^2 * c(rho)
    //
    //    A Gaussian process with Matern covariance has sample paths that are
    //    differentiable (nu - 1) times.
    //
    //    When nu = 0.5, the Matern covariance is the exponential covariance.
    //
    //    As nu goes to +oo, the correlation converges to exp ( - (rho/rho0)^2 ).
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    03 November 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int N, the number of arguments.
    //
    //    Input, double RHO[N], the arguments.
    //    0.0 <= RHO.
    //
    //    Input, double RHO0, the correlation length.
    //    0.0 < RHO0.
    //
    //    Output, double C[N], the correlations.
    //
    {
        int i;

        const double nu = 2.5;

        double[] c = new double[n];

        for (i = 0; i < n; i++)
        {
            double rho1 = 2.0 * Math.Sqrt(nu) * Math.Abs(rho[i]) / rho0;

            c[i] = rho1 switch
            {
                0.0 => 1.0,
                _ => Math.Pow(rho1, nu) * FullertonLib.r8_besk(ref globaldata, ref data, nu, rho1) / r8_gamma(nu) /
                Math.Pow(2.0, nu - 1.0)
            };
        }
        return(new CorrelationResult {
            result = c, data = globaldata, kdata = data
        });
    }
}
    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);
    }