예제 #1
0
        /// <summary>
        /// Computes correlated deviates from Cholesky decomposition.
        /// </summary>
        /// <param name="R">correlation matrix</param>
        /// <param name="dt">step size</param>
        /// <param name="z">correlated deviates array to be returned.</param>
        /// <returns>array of correlated normal deviates</returns>
        /// <remarks>
        /// OkashTODO: do we need the parameter double[] z?? Dont think so. 
        ///            Same question goes to the method GenerateCorrelatedDeviates above.
        /// </remarks>
        public double[] GenerateCorrelatedDeviatesCholesky(SymmetricMatrix R, double dt, double[] z)
        {
            int m = R.GetNumberOfRows();
            int n = R.GetNumberOfColumns();
            StatUtility util = new StatUtility();
            // standard normal deviate
            double deviate = 0.0;
            // stores deviate * sqrt(dt)
            double[] dw = new double[4] { 0.0, 0.0, 0.0, 0.0 };
            double sum = 0.0;
            // lower-banded (lb) matrix
            Matrix lb = R.Cholesky();

            Random randomNumberGenerator = new Random();
            long seed = randomNumberGenerator.Next() % 100;

            // generate uncorrelated deviates
            for (int i = 0; i < m; i++)
            {
                deviate = util.gasdev(ref seed);
                dw[i] = deviate * Math.Sqrt(dt);
            }
            // generate correlated deviates
            for (int i = 0; i < m; i++)
            {
                sum = 0;
                for (int j = 0; j < n; j++)
                {
                    sum += lb.GetElement(i, j) * dw[j];
                }
                z[i] = sum;
            }
            return z;
        }