public double DHQuadExpSim(DHParam param, double S0, double Strike, double Mat, double r, double q, int T, int N, string PutCall)
        {
            RandomNumber RN = new RandomNumber();
            NormInverse  NI = new NormInverse();

            double kappa1 = param.kappa1;
            double theta1 = param.theta1;
            double sigma1 = param.sigma1;
            double v01    = param.v01;
            double rho1   = param.rho1;
            double kappa2 = param.kappa2;
            double theta2 = param.theta2;
            double sigma2 = param.sigma2;
            double v02    = param.v02;
            double rho2   = param.rho2;

            // Time increment
            double dt = Mat / T;

            // Required quantities
            double K01 = -rho1 * kappa1 * theta1 * dt / sigma1;
            double K11 = dt / 2.0 * (kappa1 * rho1 / sigma1 - 0.5) - rho1 / sigma1;
            double K21 = dt / 2.0 * (kappa1 * rho1 / sigma1 - 0.5) + rho1 / sigma1;
            double K31 = dt / 2.0 * (1.0 - rho1 * rho1);

            double K02 = -rho2 * kappa2 * theta2 * dt / sigma2;
            double K12 = dt / 2.0 * (kappa2 * rho2 / sigma2 - 0.5) - rho2 / sigma2;
            double K22 = dt / 2.0 * (kappa2 * rho2 / sigma2 - 0.5) + rho2 / sigma2;
            double K32 = dt / 2.0 * (1.0 - rho2 * rho2);

            // Initialize the variance and stock processes
            double[,] V1 = new double[T, N];
            double[,] V2 = new double[T, N];
            double[,] S  = new double[T, N];

            // Starting values for the variance and stock processes
            for (int k = 0; k <= N - 1; k++)
            {
                S[0, k]  = S0;      // Spot price
                V1[0, k] = v01;     // Heston v0 initial variance
                V2[0, k] = v02;     // Heston v0 initial variance
            }

            // Generate the stock and volatility paths
            double m1, s1, phi1, p1, U1, b1, a1, Zv1;
            double m2, s2, phi2, p2, U2, b2, a2, Zv2;
            double beta1, beta2, B1, B2, logS;

            for (int k = 0; k <= N - 1; k++)
            {
                for (int t = 1; t <= T - 1; t++)
                {
                    m1 = theta1 + (V1[t - 1, k] - theta1) * Math.Exp(-kappa1 * dt);
                    s1 = V1[t - 1, k] * sigma1 *sigma1 *Math.Exp(-kappa1 *dt) / kappa1 * (1.0 - Math.Exp(-kappa1 * dt))
                         + theta1 * sigma1 * sigma1 / 2.0 / kappa1 * Math.Pow(1.0 - Math.Exp(-kappa1 * dt), 2.0);

                    phi1 = s1 / (m1 * m1);
                    p1   = (phi1 - 1.0) / (phi1 + 1.0);
                    U1   = RN.RandomNum(0.0, 1.0);
                    if (phi1 < 0.5)
                    {
                        b1       = Math.Sqrt(2.0 / phi1 - 1.0 + Math.Sqrt(2.0 / phi1 * (2.0 / phi1 - 1.0)));
                        a1       = m1 / (1.0 + b1 * b1);
                        Zv1      = NI.normICDF(U1);
                        V1[t, k] = a1 * (b1 + Zv1) * (b1 + Zv1);
                    }
                    else if (phi1 >= 0.5)
                    {
                        if (U1 <= p1)
                        {
                            V1[t, k] = 0.0;
                        }
                        else if (U1 > p1)
                        {
                            beta1    = (1.0 - p1) / m1;
                            V1[t, k] = Math.Log((1.0 - p1) / (1 - U1)) / beta1;
                        }
                    }
                    m2 = theta2 + (V2[t - 1, k] - theta2) * Math.Exp(-kappa2 * dt);
                    s2 = V2[t - 1, k] * sigma2 *sigma2 *Math.Exp(-kappa2 *dt) / kappa2 * (1.0 - Math.Exp(-kappa2 * dt))
                         + theta2 * sigma2 * sigma2 / 2.0 / kappa2 * Math.Pow(1.0 - Math.Exp(-kappa2 * dt), 2.0);

                    phi2 = s2 / (m2 * m2);
                    p2   = (phi2 - 1.0) / (phi2 + 1.0);
                    U2   = RN.RandomNum(0.0, 1.0);
                    if (phi2 < 0.5)
                    {
                        b2       = Math.Sqrt(2.0 / phi2 - 1.0 + Math.Sqrt(2.0 / phi2 * (2.0 / phi2 - 1.0)));
                        a2       = m2 / (1.0 + b2 * b2);
                        Zv2      = NI.normICDF(U2);
                        V2[t, k] = a2 * (b2 + Zv2) * (b2 + Zv2);
                    }
                    else if (phi2 >= 0.5)
                    {
                        if (U2 <= p2)
                        {
                            V2[t, k] = 0.0;
                        }
                        else if (U2 > p2)
                        {
                            beta2    = (1.0 - p2) / m2;
                            V2[t, k] = Math.Log((1.0 - p2) / (1.0 - U2)) / beta2;
                        }
                    }
                    // Predictor-Corrector for the stock price
                    B1   = RN.RandomNorm();
                    B2   = RN.RandomNorm();
                    logS = Math.Log(Math.Exp(-r * Convert.ToDouble(t) * dt) * S[t - 1, k])
                           + K01 + K11 * V1[t - 1, k] + K21 * V1[t, k] + Math.Sqrt(K31 * (V1[t, k] + V1[t - 1, k])) * B1
                           + K02 + K12 * V2[t - 1, k] + K22 * V2[t, k] + Math.Sqrt(K32 * (V2[t, k] + V2[t - 1, k])) * B2;
                    S[t, k] = Math.Exp(logS) * Math.Exp(r * Convert.ToDouble(t + 1) * dt);
                }
            }
            // Terminal stock prices
            double[] ST = new double[N];
            for (int k = 0; k <= N - 1; k++)
            {
                ST[k] = S[T - 1, k];
            }

            // Payoff vectors
            double[] Payoff = new double[N];
            for (int k = 0; k <= N - 1; k++)
            {
                if (PutCall == "C")
                {
                    Payoff[k] = Math.Max(ST[k] - Strike, 0.0);
                }
                else if (PutCall == "P")
                {
                    Payoff[k] = Math.Max(Strike - ST[k], 0.0);
                }
            }

            // Simulated price
            EulerAlfonsiSimulation EA = new EulerAlfonsiSimulation();
            double SimPrice           = Math.Exp(-r * Mat) * EA.VMean(Payoff);

            return(SimPrice);
        }
Esempio n. 2
0
        // Alfonsi function
        public double AlfonsiV(HParam param, double vt, double dt)
        {
            RandomNumber RN    = new RandomNumber();
            double       kappa = param.kappa;
            double       theta = param.theta;
            double       sigma = param.sigma;
            double       v0    = param.v0;
            double       rho   = param.rho;

            double phi = (1.0 - Math.Exp(-kappa * dt / 2.0)) / kappa;
            double S   = (sigma * sigma / 4.0 - theta * kappa);
            double E   = Math.Exp(kappa * dt / 2.0);

            double K2   = 0.0;
            double U    = 0.0;
            double Y    = 0.0;
            double newV = 0.0;

            if (sigma * sigma > 4.0 * kappa * theta)
            {
                K2 = E * (S * phi + Math.Pow((Math.Sqrt(E * S * phi) + sigma / 2.0 * Math.Sqrt(3.0 * dt)), 2));
            }
            else
            {
                K2 = 0;
            }
            if (vt >= K2)
            {
                U = RN.RandomNum(0.0, 1.0);
                if (U <= 1.0 / 6.0)
                {
                    Y = Math.Sqrt(3.0);
                }
                else if (U <= 1.0 / 3.0)
                {
                    Y = -Math.Sqrt(3.0);
                }
                else
                {
                    Y = 0.0;
                }
                phi  = (1.0 - Math.Exp(-kappa * dt / 2.0)) / kappa;
                S    = (theta * kappa - sigma * sigma / 4.0);
                E    = Math.Exp(-kappa * dt / 2.0);
                newV = E * Math.Pow((Math.Sqrt(S * phi + E * vt) + sigma / 2.0 * Math.Sqrt(dt) * Y), 2) + S * phi;
            }
            else
            {
                double[] u  = CIRmoments(param, vt, dt);
                double   u1 = u[0];
                double   u2 = u[1];
                double   Pi = 0.5 - 0.5 * Math.Sqrt(1 - u1 * u1 / u2);
                U = RN.RandomNum(0.0, 1.0);
                if (U <= Pi)
                {
                    newV = u1 / 2.0 / Pi;
                }
                else if (U > Pi)
                {
                    newV = u1 / 2.0 / (1.0 - Pi);
                }
            }
            return(newV);
        }
Esempio n. 3
0
        public double DHEulerAlfonsiSim(string scheme, DHParam param, double S0, double Strike, double Mat, double r, double q, int T, int N, string PutCall)
        {
            RandomNumber RN = new RandomNumber();

            double kappa1 = param.kappa1;
            double theta1 = param.theta1;
            double sigma1 = param.sigma1;
            double v01    = param.v01;
            double rho1   = param.rho1;
            double kappa2 = param.kappa2;
            double theta2 = param.theta2;
            double sigma2 = param.sigma2;
            double v02    = param.v02;
            double rho2   = param.rho2;

            HParam param1;

            param1.kappa = kappa1;
            param1.theta = theta1;
            param1.sigma = sigma1;
            param1.v0    = v01;
            param1.rho   = rho1;

            HParam param2;

            param2.kappa = kappa2;
            param2.theta = theta2;
            param2.sigma = sigma2;
            param2.v0    = v02;
            param2.rho   = rho2;

            // Time increment
            double dt = Mat / T;

            // Required quantities
            double K01 = -rho1 * kappa1 * theta1 * dt / sigma1;
            double K11 = dt / 2.0 * (kappa1 * rho1 / sigma1 - 0.5) - rho1 / sigma1;
            double K21 = dt / 2.0 * (kappa1 * rho1 / sigma1 - 0.5) + rho1 / sigma1;
            double K31 = dt / 2.0 * (1.0 - rho1 * rho1);

            double K02 = -rho2 * kappa2 * theta2 * dt / sigma2;
            double K12 = dt / 2.0 * (kappa2 * rho2 / sigma2 - 0.5) - rho2 / sigma2;
            double K22 = dt / 2.0 * (kappa2 * rho2 / sigma2 - 0.5) + rho2 / sigma2;
            double K32 = dt / 2.0 * (1.0 - rho2 * rho2);

            // Initialize the variance and stock processes
            double[,] V1 = new double[T, N];
            double[,] V2 = new double[T, N];
            double[,] S  = new double[T, N];

            // Starting values for the variance and stock processes
            for (int k = 0; k <= N - 1; k++)
            {
                S[0, k]  = S0;      // Spot price
                V1[0, k] = v01;     // Heston v0 initial variance
                V2[0, k] = v02;     // Heston v0 initial variance
            }
            double G1, G2, B1, B2, logS;

            // Generate the stock and volatility paths
            for (int k = 0; k <= N - 1; k++)
            {
                for (int t = 1; t <= T - 1; t++)
                {
                    if (scheme == "Euler")
                    {
                        // Generate two in dependent N(0,1) variables
                        G1 = RN.RandomNorm();
                        G2 = RN.RandomNorm();
                        // Euler discretization with full truncation for the variances
                        V1[t, k] = V1[t - 1, k] + kappa1 * (theta1 - V1[t - 1, k]) * dt + sigma1 * Math.Sqrt(V1[t - 1, k] * dt) * G1;
                        V2[t, k] = V2[t - 1, k] + kappa2 * (theta2 - V2[t - 1, k]) * dt + sigma2 * Math.Sqrt(V2[t - 1, k] * dt) * G2;
                        V1[t, k] = Math.Max(0, V1[t, k]);
                        V2[t, k] = Math.Max(0, V2[t, k]);
                    }
                    else if (scheme == "Alfonsi")
                    {
                        // Alfonsi discretization
                        V1[t, k] = AlfonsiV(param1, V1[t - 1, k], dt);
                        V2[t, k] = AlfonsiV(param2, V2[t - 1, k], dt);
                    }
                    // Predictor-Corrector for the stock price
                    B1   = RN.RandomNorm();
                    B2   = RN.RandomNorm();
                    logS = Math.Log(Math.Exp(-r * Convert.ToDouble(t) * dt) * S[t - 1, k])
                           + K01 + K11 * V1[t - 1, k] + K21 * V1[t, k] + Math.Sqrt(K31 * (V1[t, k] + V1[t - 1, k])) * B1
                           + K02 + K12 * V2[t - 1, k] + K22 * V2[t, k] + Math.Sqrt(K32 * (V2[t, k] + V2[t - 1, k])) * B2;
                    S[t, k] = Math.Exp(logS) * Math.Exp(r * Convert.ToDouble(t + 1) * dt);
                }
            }

            // Terminal stock prices
            double[] ST = new double[N];
            for (int k = 0; k <= N - 1; k++)
            {
                ST[k] = S[T - 1, k];
            }

            // Payoff vectors
            double[] Payoff = new double[N];
            for (int k = 0; k <= N - 1; k++)
            {
                if (PutCall == "C")
                {
                    Payoff[k] = Math.Max(ST[k] - Strike, 0.0);
                }
                else if (PutCall == "P")
                {
                    Payoff[k] = Math.Max(Strike - ST[k], 0.0);
                }
            }

            // Simulated price
            double SimPrice = Math.Exp(-r * Mat) * VMean(Payoff);

            return(SimPrice);
        }
Esempio n. 4
0
        public double DHTransVolSim(string scheme, DHParam param, double S0, double Strike, double Mat, double r, double q, int T, int N, string PutCall)
        {
            RandomNumber RN = new RandomNumber();

            // Heston parameters
            double kappa1 = param.kappa1;
            double theta1 = param.theta1;
            double sigma1 = param.sigma1;
            double v01    = param.v01;
            double rho1   = param.rho1;
            double kappa2 = param.kappa2;
            double theta2 = param.theta2;
            double sigma2 = param.sigma2;
            double v02    = param.v02;
            double rho2   = param.rho2;

            // Time increment
            double dt = Mat / T;

            // Required quantities
            double K01 = -rho1 * kappa1 * theta1 * dt / sigma1;
            double K11 = dt / 2.0 * (kappa1 * rho1 / sigma1 - 0.5) - rho1 / sigma1;
            double K21 = dt / 2.0 * (kappa1 * rho1 / sigma1 - 0.5) + rho1 / sigma1;
            double K31 = dt / 2.0 * (1.0 - rho1 * rho1);

            double K02 = -rho2 * kappa2 * theta2 * dt / sigma2;
            double K12 = dt / 2.0 * (kappa2 * rho2 / sigma2 - 0.5) - rho2 / sigma2;
            double K22 = dt / 2.0 * (kappa2 * rho2 / sigma2 - 0.5) + rho2 / sigma2;
            double K32 = dt / 2.0 * (1.0 - rho2 * rho2);

            // Initialize the volatility and stock processes
            double[,] w1 = new double[T, N];
            double[,] w2 = new double[T, N];
            double[,] S  = new double[T, N];

            // Starting values for the variance and stock processes
            for (int k = 0; k <= N - 1; k++)
            {
                S[0, k]  = S0;                  // Spot price
                w1[0, k] = Math.Sqrt(v01);      // Heston initial volatility
                w2[0, k] = Math.Sqrt(v02);
            }
            // Generate the stock and volatility paths
            double Zv1, m11, m12, beta1, thetav1;
            double Zv2, m21, m22, beta2, thetav2;
            double B1, B2, logS;

            for (int k = 0; k <= N - 1; k++)
            {
                for (int t = 1; t <= T - 1; t++)
                {
                    Zv1 = RN.RandomNorm();
                    Zv2 = RN.RandomNorm();
                    if (scheme == "ZhuEuler")
                    {
                        // Transformed variance scheme
                        w1[t, k] = w1[t - 1, k] + 0.5 * kappa1 * ((theta1 - sigma1 * sigma1 / 4.0 / kappa1) / w1[t - 1, k] - w1[t - 1, k]) * dt + 0.5 * sigma1 * Math.Sqrt(dt) * Zv1;
                        w2[t, k] = w2[t - 1, k] + 0.5 * kappa2 * ((theta2 - sigma2 * sigma2 / 4.0 / kappa2) / w2[t - 1, k] - w2[t - 1, k]) * dt + 0.5 * sigma2 * Math.Sqrt(dt) * Zv2;
                    }
                    else if (scheme == "ZhuTV")
                    {
                        // Zhu (2010) process for the transformed volatility
                        m11      = theta1 + (w1[t - 1, k] * w1[t - 1, k] - theta1) * Math.Exp(-kappa1 * dt);
                        m21      = theta2 + (w2[t - 1, k] * w2[t - 1, k] - theta2) * Math.Exp(-kappa2 * dt);
                        m12      = sigma1 * sigma1 / 4.0 / kappa1 * (1.0 - Math.Exp(-kappa1 * dt));
                        m22      = sigma2 * sigma2 / 4.0 / kappa2 * (1.0 - Math.Exp(-kappa2 * dt));
                        beta1    = Math.Sqrt(Math.Max(0, m11 - m12));
                        beta2    = Math.Sqrt(Math.Max(0, m21 - m22));
                        thetav1  = (beta1 - w1[t - 1, k] * Math.Exp(-kappa1 * dt / 2.0)) / (1.0 - Math.Exp(-kappa1 * dt / 2.0));
                        thetav2  = (beta2 - w2[t - 1, k] * Math.Exp(-kappa2 * dt / 2.0)) / (1.0 - Math.Exp(-kappa2 * dt / 2.0));
                        w1[t, k] = w1[t - 1, k] + 0.5 * kappa1 * (thetav1 - w1[t - 1, k]) * dt + 0.5 * sigma1 * Math.Sqrt(dt) * Zv1;
                        w2[t, k] = w2[t - 1, k] + 0.5 * kappa2 * (thetav2 - w2[t - 1, k]) * dt + 0.5 * sigma2 * Math.Sqrt(dt) * Zv2;
                    }
                    // Predictor-Corrector for the stock price
                    B1   = RN.RandomNorm();
                    B2   = RN.RandomNorm();
                    logS = Math.Log(Math.Exp(-r * Convert.ToDouble(t) * dt) * S[t - 1, k])
                           + K01 + K11 * w1[t - 1, k] * w1[t - 1, k] + K21 * w1[t, k] * w1[t, k] + Math.Sqrt(K31 * (w1[t, k] * w1[t, k] + w1[t - 1, k] * w1[t - 1, k])) * B1
                           + K02 + K12 * w2[t - 1, k] * w2[t - 1, k] + K22 * w2[t, k] * w2[t, k] + Math.Sqrt(K32 * (w2[t, k] * w2[t, k] + w2[t - 1, k] * w2[t - 1, k])) * B2;
                    S[t, k] = Math.Exp(logS) * Math.Exp(r * Convert.ToDouble(t + 1) * dt);
                }
            }
            // Terminal stock prices
            double[] ST = new double[N];
            for (int k = 0; k <= N - 1; k++)
            {
                ST[k] = S[T - 1, k];
            }

            // Payoff vectors
            double[] Payoff = new double[N];
            for (int k = 0; k <= N - 1; k++)
            {
                if (PutCall == "C")
                {
                    Payoff[k] = Math.Max(ST[k] - Strike, 0.0);
                }
                else if (PutCall == "P")
                {
                    Payoff[k] = Math.Max(Strike - ST[k], 0.0);
                }
            }

            // Simulated price
            EulerAlfonsiSimulation EA = new EulerAlfonsiSimulation();
            double SimPrice           = Math.Exp(-r * Mat) * EA.VMean(Payoff);

            return(SimPrice);
        }