Ejemplo n.º 1
0
        public override double[] CVput(double strike, double stockprice, double rate, double vol, int step, int trial, double time, double[,] randommatrix)
        {
            double[,] price = pricematrix(strike, stockprice, rate, vol, step, trial, time, randommatrix);
            double    dt = time / step; double t;
            CDFmethod cdf = new CDFmethod();

            double[] ct = new double[trial];//define a list to save option price and standard error
            for (int i = 0; i < trial; i++)
            {
                double cv = 0;//define delta hedge
                for (int j = 1; j < step; j++)
                {
                    t = dt * (step - j);
                    double d1    = (Math.Log(price[i, j - 1] / strike) + (rate + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                    double delta = cdf.CumDensity(d1) - 1;
                    cv = cv + delta * (price[i, j] - price[i, j - 1] * Math.Exp(rate * dt));
                }
                if (Math.Max(0, strike - price[i, step - 1]) > 0)
                {
                    ct[i] = (rebate - cv) * Math.Exp(-rate * time);
                }
                else
                {
                    ct[i] = (0 - cv) * Math.Exp(-rate * time);
                }
            }
            return(ct);
        }
Ejemplo n.º 2
0
        public override double[] CVput(double strike, double stockprice, double rate, double vol, int step, int trial, double time, double[,] randommatrix)
        {
            double[,] price = pricematrix(strike, stockprice, rate, vol, step, trial, time, randommatrix);

            double    dt = time / step; double t;
            CDFmethod cdf = new CDFmethod();       //compute cdf

            double[] ct       = new double[trial]; //define a list to save option price and standard error
            double[] avgprice = new double[trial];
            for (int i = 0; i < trial; i++)
            {
                double cv = 0;//define delta hedge
                for (int j = 1; j < step; j++)
                {
                    avgprice[i] = avgprice[i] + price[i, j];
                    t           = dt * (step - j);
                    double d1    = (Math.Log(price[i, j - 1] / strike) + (rate + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                    double delta = cdf.CumDensity(d1) - 1;
                    cv = cv + delta * (price[i, j] - price[i, j - 1] * Math.Exp(rate * dt));
                }
                avgprice[i] = avgprice[i] / step; //find the mean stockprice of every trial
                ct[i]       = (Math.Max(0, -avgprice[i] + strike) - cv) * Math.Exp(-rate * time);
            }
            return(ct);
        }
Ejemplo n.º 3
0
        //control variate method to get european put option value list
        public static double[] CVepo(double strike, double stockprice, double rate, double vol, int step, int trial, double time, double[,] randommatrix)
        {
            double[,] price = pricematrix(strike, stockprice, rate, vol, step, trial, time, randommatrix);
            double    dt = time / step; double t;
            CDFmethod cdf = new CDFmethod();

            double[] ct = new double[trial];//define a list to save option price and standard error
            for (int i = 0; i < trial; i++)
            {
                double cv = 0;//define delta hedge
                for (int j = 1; j < step; j++)
                {
                    t = dt * (step - j);
                    double d1    = (Math.Log(price[i, j - 1] / strike) + (rate + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                    double delta = cdf.CumDensity(d1) - 1;
                    cv = cv + delta * (price[i, j] - price[i, j - 1] * Math.Exp(rate * dt));
                }
                ct[i] = (Math.Max(0, -price[i, step - 1] + strike) - cv) * Math.Exp(-rate * time);
            }
            //double putprice = ct.Average();
            //double se = std(ct) / Math.Sqrt(trial);//get traditional standard error
            //double ase = antise(ct); //get antithetic standard error
            //double[] a = putprice, ct };
            return(ct);
        }
Ejemplo n.º 4
0
        public override double[] CVput(double strike, double stockprice, double rate, double vol, int step, int trial, double time, double[,] randommatrix)
        {
            double[,] price = pricematrix(strike, stockprice, rate, vol, step, trial, time, randommatrix);
            double    dt = time / step; double t;
            CDFmethod cdf = new CDFmethod();       //compute cdf

            double[] ct       = new double[trial]; //define a list to save option price and standard error
            double[] minprice = new double[trial]; //define minimum price in every trial
            double[] maxprice = new double[trial]; //define maximum price in every trial
            for (int i = 0; i < trial; i++)
            {
                double cv = 0;//define delta hedge
                maxprice[i] = 0; minprice[i] = price[i, 0];
                for (int j = 1; j < step; j++)
                {
                    if (price[i, j] < minprice[i])
                    {
                        minprice[i] = price[i, j];
                    }
                    if (price[i, j] > maxprice[i])
                    {
                        maxprice[i] = price[i, j];
                    }
                    t = dt * (step - j);
                    double d1    = (Math.Log(price[i, j - 1] / strike) + (rate + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                    double delta = cdf.CumDensity(d1);
                    cv = cv + delta * (price[i, j] - price[i, j - 1] * Math.Exp(rate * dt));
                }
                ct[i] = (maxprice[i] - minprice[i] - cv) * Math.Exp(-rate * time);
            }
            return(ct);
        }
Ejemplo n.º 5
0
        //use CV method to get call and put in control variate
        public override double[] CVmethod(double strike, double stockprice, double rate, double vol, int step, int trial, double time, double[,] pricematrix, bool calloption)
        {
            double    deltas = 0.001 * stockprice;
            double    deltavol = 0.001 * vol;
            double    deltar = 0.001 * rate;
            double    dt = time / step; double t;
            CDFmethod cdf = new CDFmethod();     //compute cdf

            double[] ct = new double[8 * trial]; //define a list to save option price
            if (isdown == true)
            {
                if (isout == true)//option value is 0 when price is less than bar
                {
                    bool[] iszero = new bool[8];
                    for (int i = 0; i < 8 * trial; i = i + 8)
                    {
                        for (int k = i; k < 8 + i; k++)
                        {
                            iszero[k - i] = false; //define whether satisfy conditions
                        }
                        double cv1 = 0;            //define delta hedge
                        double cv2 = 0; double cv3 = 0; double cv4 = 0; double cv5 = 0; double cv6 = 0; double cv7 = 0; double cv8 = 0;
                        for (int j = 1; j < step; j++)
                        {
                            if (pricematrix[i, j] < bar)
                            {
                                iszero[0] = true; break;
                            }
                            if (pricematrix[i + 1, j] < bar)
                            {
                                iszero[1] = true; break;
                            }
                            if (pricematrix[i + 2, j] < bar)
                            {
                                iszero[2] = true; break;
                            }
                            if (pricematrix[i + 3, j] < bar)
                            {
                                iszero[3] = true; break;
                            }
                            if (pricematrix[i + 4, j] < bar)
                            {
                                iszero[4] = true; break;
                            }
                            if (pricematrix[i + 5, j] < bar)
                            {
                                iszero[5] = true; break;
                            }
                            if (pricematrix[i + 6, j] < bar)
                            {
                                iszero[6] = true; break;
                            }
                            if (pricematrix[i + 7, j] < bar)
                            {
                                iszero[7] = true; break;
                            }                                                            //determine whether meet the condition
                            t = dt * (step - j);
                            //get d1 of bs formula
                            double d11 = (Math.Log(pricematrix[i, j - 1] / strike) + (rate + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                            double d12 = (Math.Log(pricematrix[i + 1, j - 1] / strike) + (rate + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                            double d13 = (Math.Log(pricematrix[i + 2, j - 1] / strike) + (rate + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                            double d14 = (Math.Log(pricematrix[i + 3, j - 1] / strike) + (rate + Math.Pow(vol + deltavol, 2) / 2) * t) / ((vol + deltavol) * Math.Pow(t, 0.5));
                            double d15 = (Math.Log(pricematrix[i + 4, j - 1] / strike) + (rate + deltar + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                            double d16 = (Math.Log(pricematrix[i + 5, j - 1] / strike) + (rate + Math.Pow(vol, 2) / 2) * (t * step + dt) / step) / (vol * Math.Pow((t * step + dt) / step, 0.5));
                            double d17 = (Math.Log(pricematrix[i + 6, j - 1] / strike) + (rate + Math.Pow(vol - deltavol, 2) / 2) * t) / ((vol - deltavol) * Math.Pow(t, 0.5));
                            double d18 = (Math.Log(pricematrix[i + 7, j - 1] / strike) + (rate - deltar + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                            //get delta
                            if (calloption)
                            {
                                double delta1 = cdf.CumDensity(d11); double delta2 = cdf.CumDensity(d12);
                                double delta3 = cdf.CumDensity(d13); double delta4 = cdf.CumDensity(d14);
                                double delta5 = cdf.CumDensity(d15); double delta6 = cdf.CumDensity(d16);
                                double delta7 = cdf.CumDensity(d17); double delta8 = cdf.CumDensity(d18);
                                //get delta hedge
                                cv1 = cv1 + delta1 * (pricematrix[i, j] - pricematrix[i, j - 1] * Math.Exp(rate * dt));
                                cv2 = cv2 + delta2 * (pricematrix[i + 1, j] - pricematrix[i + 1, j - 1] * Math.Exp(rate * dt));
                                cv3 = cv3 + delta3 * (pricematrix[i + 2, j] - pricematrix[i + 2, j - 1] * Math.Exp(rate * dt));
                                cv4 = cv4 + delta4 * (pricematrix[i + 3, j] - pricematrix[i + 3, j - 1] * Math.Exp(rate * dt));
                                cv5 = cv5 + delta5 * (pricematrix[i + 4, j] - pricematrix[i + 4, j - 1] * Math.Exp((rate + deltar) * dt));
                                cv6 = cv6 + delta6 * (pricematrix[i + 5, j] - pricematrix[i + 5, j - 1] * Math.Exp(rate * (dt + dt / step)));
                                cv7 = cv7 + delta7 * (pricematrix[i + 6, j] - pricematrix[i + 6, j - 1] * Math.Exp(rate * dt));
                                cv8 = cv8 + delta8 * (pricematrix[i + 7, j] - pricematrix[i + 7, j - 1] * Math.Exp((rate - deltar) * dt));
                            }
                            else
                            {
                                double delta1 = cdf.CumDensity(d11) - 1; double delta2 = cdf.CumDensity(d12) - 1;
                                double delta3 = cdf.CumDensity(d13) - 1; double delta4 = cdf.CumDensity(d14) - 1;
                                double delta5 = cdf.CumDensity(d15) - 1; double delta6 = cdf.CumDensity(d16) - 1;
                                double delta7 = cdf.CumDensity(d17) - 1; double delta8 = cdf.CumDensity(d18) - 1;
                                //get delta hedge
                                cv1 = cv1 + delta1 * (pricematrix[i, j] - pricematrix[i, j - 1] * Math.Exp(rate * dt));
                                cv2 = cv2 + delta2 * (pricematrix[i + 1, j] - pricematrix[i + 1, j - 1] * Math.Exp(rate * dt));
                                cv3 = cv3 + delta3 * (pricematrix[i + 2, j] - pricematrix[i + 2, j - 1] * Math.Exp(rate * dt));
                                cv4 = cv4 + delta4 * (pricematrix[i + 3, j] - pricematrix[i + 3, j - 1] * Math.Exp(rate * dt));
                                cv5 = cv5 + delta5 * (pricematrix[i + 4, j] - pricematrix[i + 4, j - 1] * Math.Exp((rate + deltar) * dt));
                                cv6 = cv6 + delta6 * (pricematrix[i + 5, j] - pricematrix[i + 5, j - 1] * Math.Exp(rate * (dt + dt / step)));
                                cv7 = cv7 + delta7 * (pricematrix[i + 6, j] - pricematrix[i + 6, j - 1] * Math.Exp(rate * dt));
                                cv8 = cv8 + delta8 * (pricematrix[i + 7, j] - pricematrix[i + 7, j - 1] * Math.Exp((rate - deltar) * dt));
                            }
                        }
                        if (calloption) //get optionprice
                        {
                            ct[i]     = (Math.Max(0, pricematrix[i, step - 1] - strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[0])) - cv1 * Math.Exp(-rate * time);
                            ct[i + 1] = (Math.Max(0, pricematrix[i + 1, step - 1] - strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[1])) - cv2 * Math.Exp(-rate * time);
                            ct[i + 2] = (Math.Max(0, pricematrix[i + 2, step - 1] - strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[2])) - cv3 * Math.Exp(-rate * time);
                            ct[i + 3] = (Math.Max(0, pricematrix[i + 3, step - 1] - strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[3])) - cv4 * Math.Exp(-rate * time);
                            ct[i + 4] = (Math.Max(0, pricematrix[i + 4, step - 1] - strike)) * Math.Exp(-(rate + deltar) * time) * (1 - Convert.ToInt32(iszero[4])) - cv5 * Math.Exp(-rate * time);
                            ct[i + 5] = (Math.Max(0, pricematrix[i + 5, step - 1] - strike)) * Math.Exp(-rate * (time + dt)) * (1 - Convert.ToInt32(iszero[5])) - cv6 * Math.Exp(-rate * time);
                            ct[i + 6] = (Math.Max(0, pricematrix[i + 6, step - 1] - strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[6])) - cv7 * Math.Exp(-rate * time);
                            ct[i + 7] = (Math.Max(0, pricematrix[i + 7, step - 1] - strike)) * Math.Exp(-(rate - deltar) * time) * (1 - Convert.ToInt32(iszero[7])) - cv8 * Math.Exp(-rate * time);
                        }
                        else
                        {
                            ct[i]     = (Math.Max(0, -pricematrix[i, step - 1] + strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[0])) - cv1 * Math.Exp(-rate * time);
                            ct[i + 1] = (Math.Max(0, -pricematrix[i + 1, step - 1] + strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[1])) - cv2 * Math.Exp(-rate * time);
                            ct[i + 2] = (Math.Max(0, -pricematrix[i + 2, step - 1] + strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[2])) - cv3 * Math.Exp(-rate * time);
                            ct[i + 3] = (Math.Max(0, -pricematrix[i + 3, step - 1] + strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[3])) - cv4 * Math.Exp(-rate * time);
                            ct[i + 4] = (Math.Max(0, -pricematrix[i + 4, step - 1] + strike)) * Math.Exp(-(rate + deltar) * time) * (1 - Convert.ToInt32(iszero[4])) - cv5 * Math.Exp(-rate * time);
                            ct[i + 5] = (Math.Max(0, -pricematrix[i + 5, step - 1] + strike)) * Math.Exp(-rate * (time + dt)) * (1 - Convert.ToInt32(iszero[5])) - cv6 * Math.Exp(-rate * time);
                            ct[i + 6] = (Math.Max(0, -pricematrix[i + 6, step - 1] + strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[6])) - cv7 * Math.Exp(-rate * time);
                            ct[i + 7] = (Math.Max(0, -pricematrix[i + 7, step - 1] + strike)) * Math.Exp(-(rate - deltar) * time) * (1 - Convert.ToInt32(iszero[7])) - cv8 * Math.Exp(-rate * time);
                        }
                    }
                }
                else //option value is not 0 once price is less than bar
                {
                    bool[] iszero = new bool[8];
                    for (int i = 0; i < 8 * trial; i = i + 8)
                    {
                        for (int k = i; k < 8 + i; k++)
                        {
                            iszero[k - i] = true; //define whether satisfy conditions
                        }
                        double cv1 = 0;           //define delta hedge
                        double cv2 = 0; double cv3 = 0; double cv4 = 0; double cv5 = 0; double cv6 = 0; double cv7 = 0; double cv8 = 0;
                        for (int j = 1; j < step; j++)
                        {
                            if (pricematrix[i, j] < bar)
                            {
                                iszero[0] = false; break;
                            }
                            if (pricematrix[i + 1, j] < bar)
                            {
                                iszero[1] = false; break;
                            }
                            if (pricematrix[i + 2, j] < bar)
                            {
                                iszero[2] = false; break;
                            }
                            if (pricematrix[i + 3, j] < bar)
                            {
                                iszero[3] = false; break;
                            }
                            if (pricematrix[i + 4, j] < bar)
                            {
                                iszero[4] = false; break;
                            }
                            if (pricematrix[i + 5, j] < bar)
                            {
                                iszero[5] = false; break;
                            }
                            if (pricematrix[i + 6, j] < bar)
                            {
                                iszero[6] = false; break;
                            }
                            if (pricematrix[i + 7, j] < bar)
                            {
                                iszero[7] = false; break;
                            }                                                         //determine whether meet the condition
                            t = dt * (step - j);
                            //get d1 of bs formula
                            double d11 = (Math.Log(pricematrix[i, j - 1] / strike) + (rate + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                            double d12 = (Math.Log(pricematrix[i + 1, j - 1] / strike) + (rate + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                            double d13 = (Math.Log(pricematrix[i + 2, j - 1] / strike) + (rate + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                            double d14 = (Math.Log(pricematrix[i + 3, j - 1] / strike) + (rate + Math.Pow(vol + deltavol, 2) / 2) * t) / ((vol + deltavol) * Math.Pow(t, 0.5));
                            double d15 = (Math.Log(pricematrix[i + 4, j - 1] / strike) + (rate + deltar + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                            double d16 = (Math.Log(pricematrix[i + 5, j - 1] / strike) + (rate + Math.Pow(vol, 2) / 2) * (t * step + dt) / step) / (vol * Math.Pow((t * step + dt) / step, 0.5));
                            double d17 = (Math.Log(pricematrix[i + 6, j - 1] / strike) + (rate + Math.Pow(vol - deltavol, 2) / 2) * t) / ((vol - deltavol) * Math.Pow(t, 0.5));
                            double d18 = (Math.Log(pricematrix[i + 7, j - 1] / strike) + (rate - deltar + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                            //get delta
                            if (calloption)
                            {
                                double delta1 = cdf.CumDensity(d11); double delta2 = cdf.CumDensity(d12);
                                double delta3 = cdf.CumDensity(d13); double delta4 = cdf.CumDensity(d14);
                                double delta5 = cdf.CumDensity(d15); double delta6 = cdf.CumDensity(d16);
                                double delta7 = cdf.CumDensity(d17); double delta8 = cdf.CumDensity(d18);
                                //get delta hedge
                                cv1 = cv1 + delta1 * (pricematrix[i, j] - pricematrix[i, j - 1] * Math.Exp(rate * dt));
                                cv2 = cv2 + delta2 * (pricematrix[i + 1, j] - pricematrix[i + 1, j - 1] * Math.Exp(rate * dt));
                                cv3 = cv3 + delta3 * (pricematrix[i + 2, j] - pricematrix[i + 2, j - 1] * Math.Exp(rate * dt));
                                cv4 = cv4 + delta4 * (pricematrix[i + 3, j] - pricematrix[i + 3, j - 1] * Math.Exp(rate * dt));
                                cv5 = cv5 + delta5 * (pricematrix[i + 4, j] - pricematrix[i + 4, j - 1] * Math.Exp((rate + deltar) * dt));
                                cv6 = cv6 + delta6 * (pricematrix[i + 5, j] - pricematrix[i + 5, j - 1] * Math.Exp(rate * (dt + dt / step)));
                                cv7 = cv7 + delta7 * (pricematrix[i + 6, j] - pricematrix[i + 6, j - 1] * Math.Exp(rate * dt));
                                cv8 = cv8 + delta8 * (pricematrix[i + 7, j] - pricematrix[i + 7, j - 1] * Math.Exp((rate - deltar) * dt));
                            }
                            else
                            {
                                double delta1 = cdf.CumDensity(d11) - 1; double delta2 = cdf.CumDensity(d12) - 1;
                                double delta3 = cdf.CumDensity(d13) - 1; double delta4 = cdf.CumDensity(d14) - 1;
                                double delta5 = cdf.CumDensity(d15) - 1; double delta6 = cdf.CumDensity(d16) - 1;
                                double delta7 = cdf.CumDensity(d17) - 1; double delta8 = cdf.CumDensity(d18) - 1;
                                //get delta hedge
                                cv1 = cv1 + delta1 * (pricematrix[i, j] - pricematrix[i, j - 1] * Math.Exp(rate * dt));
                                cv2 = cv2 + delta2 * (pricematrix[i + 1, j] - pricematrix[i + 1, j - 1] * Math.Exp(rate * dt));
                                cv3 = cv3 + delta3 * (pricematrix[i + 2, j] - pricematrix[i + 2, j - 1] * Math.Exp(rate * dt));
                                cv4 = cv4 + delta4 * (pricematrix[i + 3, j] - pricematrix[i + 3, j - 1] * Math.Exp(rate * dt));
                                cv5 = cv5 + delta5 * (pricematrix[i + 4, j] - pricematrix[i + 4, j - 1] * Math.Exp((rate + deltar) * dt));
                                cv6 = cv6 + delta6 * (pricematrix[i + 5, j] - pricematrix[i + 5, j - 1] * Math.Exp(rate * (dt + dt / step)));
                                cv7 = cv7 + delta7 * (pricematrix[i + 6, j] - pricematrix[i + 6, j - 1] * Math.Exp(rate * dt));
                                cv8 = cv8 + delta8 * (pricematrix[i + 7, j] - pricematrix[i + 7, j - 1] * Math.Exp((rate - deltar) * dt));
                            }
                        }
                        if (calloption) //get optionprice
                        {
                            ct[i]     = (Math.Max(0, pricematrix[i, step - 1] - strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[0])) - cv1 * Math.Exp(-rate * time);
                            ct[i + 1] = (Math.Max(0, pricematrix[i + 1, step - 1] - strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[1])) - cv2 * Math.Exp(-rate * time);
                            ct[i + 2] = (Math.Max(0, pricematrix[i + 2, step - 1] - strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[2])) - cv3 * Math.Exp(-rate * time);
                            ct[i + 3] = (Math.Max(0, pricematrix[i + 3, step - 1] - strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[3])) - cv4 * Math.Exp(-rate * time);
                            ct[i + 4] = (Math.Max(0, pricematrix[i + 4, step - 1] - strike)) * Math.Exp(-(rate + deltar) * time) * (1 - Convert.ToInt32(iszero[4])) - cv5 * Math.Exp(-rate * time);
                            ct[i + 5] = (Math.Max(0, pricematrix[i + 5, step - 1] - strike)) * Math.Exp(-rate * (time + dt)) * (1 - Convert.ToInt32(iszero[5])) - cv6 * Math.Exp(-rate * time);
                            ct[i + 6] = (Math.Max(0, pricematrix[i + 6, step - 1] - strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[6])) - cv7 * Math.Exp(-rate * time);
                            ct[i + 7] = (Math.Max(0, pricematrix[i + 7, step - 1] - strike)) * Math.Exp(-(rate - deltar) * time) * (1 - Convert.ToInt32(iszero[7])) - cv8 * Math.Exp(-rate * time);
                        }
                        else
                        {
                            ct[i]     = (Math.Max(0, -pricematrix[i, step - 1] + strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[0])) - cv1 * Math.Exp(-rate * time);
                            ct[i + 1] = (Math.Max(0, -pricematrix[i + 1, step - 1] + strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[1])) - cv2 * Math.Exp(-rate * time);
                            ct[i + 2] = (Math.Max(0, -pricematrix[i + 2, step - 1] + strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[2])) - cv3 * Math.Exp(-rate * time);
                            ct[i + 3] = (Math.Max(0, -pricematrix[i + 3, step - 1] + strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[3])) - cv4 * Math.Exp(-rate * time);
                            ct[i + 4] = (Math.Max(0, -pricematrix[i + 4, step - 1] + strike)) * Math.Exp(-(rate + deltar) * time) * (1 - Convert.ToInt32(iszero[4])) - cv5 * Math.Exp(-rate * time);
                            ct[i + 5] = (Math.Max(0, -pricematrix[i + 5, step - 1] + strike)) * Math.Exp(-rate * (time + dt)) * (1 - Convert.ToInt32(iszero[5])) - cv6 * Math.Exp(-rate * time);
                            ct[i + 6] = (Math.Max(0, -pricematrix[i + 6, step - 1] + strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[6])) - cv7 * Math.Exp(-rate * time);
                            ct[i + 7] = (Math.Max(0, -pricematrix[i + 7, step - 1] + strike)) * Math.Exp(-(rate - deltar) * time) * (1 - Convert.ToInt32(iszero[7])) - cv8 * Math.Exp(-rate * time);
                        }
                    }
                }
            }
            else
            {
                if (isout == true)//option value is 0 when price is higher than bar
                {
                    bool[] iszero = new bool[8];
                    for (int i = 0; i < 8 * trial; i = i + 8)
                    {
                        for (int k = i; k < 8 + i; k++)
                        {
                            iszero[k - i] = false; //define whether satisfy conditions
                        }
                        double cv1 = 0;            //define delta hedge
                        double cv2 = 0; double cv3 = 0; double cv4 = 0; double cv5 = 0; double cv6 = 0; double cv7 = 0; double cv8 = 0;
                        for (int j = 1; j < step; j++)
                        {
                            if (pricematrix[i, j] > bar)
                            {
                                iszero[0] = true; break;
                            }
                            if (pricematrix[i + 1, j] > bar)
                            {
                                iszero[1] = true; break;
                            }
                            if (pricematrix[i + 2, j] > bar)
                            {
                                iszero[2] = true; break;
                            }
                            if (pricematrix[i + 3, j] > bar)
                            {
                                iszero[3] = true; break;
                            }
                            if (pricematrix[i + 4, j] > bar)
                            {
                                iszero[4] = true; break;
                            }
                            if (pricematrix[i + 5, j] > bar)
                            {
                                iszero[5] = true; break;
                            }
                            if (pricematrix[i + 6, j] > bar)
                            {
                                iszero[6] = true; break;
                            }
                            if (pricematrix[i + 7, j] > bar)
                            {
                                iszero[7] = true; break;
                            }                                                            //determine whether meet the condition
                            t = dt * (step - j);
                            //get d1 of bs formula
                            double d11 = (Math.Log(pricematrix[i, j - 1] / strike) + (rate + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                            double d12 = (Math.Log(pricematrix[i + 1, j - 1] / strike) + (rate + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                            double d13 = (Math.Log(pricematrix[i + 2, j - 1] / strike) + (rate + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                            double d14 = (Math.Log(pricematrix[i + 3, j - 1] / strike) + (rate + Math.Pow(vol + deltavol, 2) / 2) * t) / ((vol + deltavol) * Math.Pow(t, 0.5));
                            double d15 = (Math.Log(pricematrix[i + 4, j - 1] / strike) + (rate + deltar + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                            double d16 = (Math.Log(pricematrix[i + 5, j - 1] / strike) + (rate + Math.Pow(vol, 2) / 2) * (t * step + dt) / step) / (vol * Math.Pow((t * step + dt) / step, 0.5));
                            double d17 = (Math.Log(pricematrix[i + 6, j - 1] / strike) + (rate + Math.Pow(vol - deltavol, 2) / 2) * t) / ((vol - deltavol) * Math.Pow(t, 0.5));
                            double d18 = (Math.Log(pricematrix[i + 7, j - 1] / strike) + (rate - deltar + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                            //get delta
                            if (calloption)
                            {
                                double delta1 = cdf.CumDensity(d11); double delta2 = cdf.CumDensity(d12);
                                double delta3 = cdf.CumDensity(d13); double delta4 = cdf.CumDensity(d14);
                                double delta5 = cdf.CumDensity(d15); double delta6 = cdf.CumDensity(d16);
                                double delta7 = cdf.CumDensity(d17); double delta8 = cdf.CumDensity(d18);
                                //get delta hedge
                                cv1 = cv1 + delta1 * (pricematrix[i, j] - pricematrix[i, j - 1] * Math.Exp(rate * dt));
                                cv2 = cv2 + delta2 * (pricematrix[i + 1, j] - pricematrix[i + 1, j - 1] * Math.Exp(rate * dt));
                                cv3 = cv3 + delta3 * (pricematrix[i + 2, j] - pricematrix[i + 2, j - 1] * Math.Exp(rate * dt));
                                cv4 = cv4 + delta4 * (pricematrix[i + 3, j] - pricematrix[i + 3, j - 1] * Math.Exp(rate * dt));
                                cv5 = cv5 + delta5 * (pricematrix[i + 4, j] - pricematrix[i + 4, j - 1] * Math.Exp((rate + deltar) * dt));
                                cv6 = cv6 + delta6 * (pricematrix[i + 5, j] - pricematrix[i + 5, j - 1] * Math.Exp(rate * (dt + dt / step)));
                                cv7 = cv7 + delta7 * (pricematrix[i + 6, j] - pricematrix[i + 6, j - 1] * Math.Exp(rate * dt));
                                cv8 = cv8 + delta8 * (pricematrix[i + 7, j] - pricematrix[i + 7, j - 1] * Math.Exp((rate - deltar) * dt));
                            }
                            else
                            {
                                double delta1 = cdf.CumDensity(d11) - 1; double delta2 = cdf.CumDensity(d12) - 1;
                                double delta3 = cdf.CumDensity(d13) - 1; double delta4 = cdf.CumDensity(d14) - 1;
                                double delta5 = cdf.CumDensity(d15) - 1; double delta6 = cdf.CumDensity(d16) - 1;
                                double delta7 = cdf.CumDensity(d17) - 1; double delta8 = cdf.CumDensity(d18) - 1;
                                //get delta hedge
                                cv1 = cv1 + delta1 * (pricematrix[i, j] - pricematrix[i, j - 1] * Math.Exp(rate * dt));
                                cv2 = cv2 + delta2 * (pricematrix[i + 1, j] - pricematrix[i + 1, j - 1] * Math.Exp(rate * dt));
                                cv3 = cv3 + delta3 * (pricematrix[i + 2, j] - pricematrix[i + 2, j - 1] * Math.Exp(rate * dt));
                                cv4 = cv4 + delta4 * (pricematrix[i + 3, j] - pricematrix[i + 3, j - 1] * Math.Exp(rate * dt));
                                cv5 = cv5 + delta5 * (pricematrix[i + 4, j] - pricematrix[i + 4, j - 1] * Math.Exp((rate + deltar) * dt));
                                cv6 = cv6 + delta6 * (pricematrix[i + 5, j] - pricematrix[i + 5, j - 1] * Math.Exp(rate * (dt + dt / step)));
                                cv7 = cv7 + delta7 * (pricematrix[i + 6, j] - pricematrix[i + 6, j - 1] * Math.Exp(rate * dt));
                                cv8 = cv8 + delta8 * (pricematrix[i + 7, j] - pricematrix[i + 7, j - 1] * Math.Exp((rate - deltar) * dt));
                            }
                        }
                        if (calloption) //get optionprice
                        {
                            ct[i]     = (Math.Max(0, pricematrix[i, step - 1] - strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[0])) - cv1 * Math.Exp(-rate * time);
                            ct[i + 1] = (Math.Max(0, pricematrix[i + 1, step - 1] - strike) - cv2) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[1])) - cv2 * Math.Exp(-rate * time);
                            ct[i + 2] = (Math.Max(0, pricematrix[i + 2, step - 1] - strike) - cv3) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[2])) - cv3 * Math.Exp(-rate * time);
                            ct[i + 3] = (Math.Max(0, pricematrix[i + 3, step - 1] - strike) - cv4) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[3])) - cv4 * Math.Exp(-rate * time);
                            ct[i + 4] = (Math.Max(0, pricematrix[i + 4, step - 1] - strike) - cv5) * Math.Exp(-(rate + deltar) * time) * (1 - Convert.ToInt32(iszero[4])) - cv5 * Math.Exp(-rate * time);
                            ct[i + 5] = (Math.Max(0, pricematrix[i + 5, step - 1] - strike) - cv6) * Math.Exp(-rate * (time + dt)) * (1 - Convert.ToInt32(iszero[5])) - cv6 * Math.Exp(-rate * time);
                            ct[i + 6] = (Math.Max(0, pricematrix[i + 6, step - 1] - strike) - cv7) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[6])) - cv7 * Math.Exp(-rate * time);
                            ct[i + 7] = (Math.Max(0, pricematrix[i + 7, step - 1] - strike) - cv8) * Math.Exp(-(rate - deltar) * time) * (1 - Convert.ToInt32(iszero[7])) - cv8 * Math.Exp(-rate * time);
                        }
                        else
                        {
                            ct[i]     = (Math.Max(0, -pricematrix[i, step - 1] + strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[0])) - cv1 * Math.Exp(-rate * time);
                            ct[i + 1] = (Math.Max(0, -pricematrix[i + 1, step - 1] - +strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[1])) - cv2 * Math.Exp(-rate * time);
                            ct[i + 2] = (Math.Max(0, -pricematrix[i + 2, step - 1] + strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[2])) - cv3 * Math.Exp(-rate * time);
                            ct[i + 3] = (Math.Max(0, -pricematrix[i + 3, step - 1] + strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[3])) - cv4 * Math.Exp(-rate * time);
                            ct[i + 4] = (Math.Max(0, -pricematrix[i + 4, step - 1] + strike)) * Math.Exp(-(rate + deltar) * time) * (1 - Convert.ToInt32(iszero[4])) - cv5 * Math.Exp(-rate * time);
                            ct[i + 5] = (Math.Max(0, -pricematrix[i + 5, step - 1] + strike)) * Math.Exp(-rate * (time + dt)) * (1 - Convert.ToInt32(iszero[5])) - cv6 * Math.Exp(-rate * time);
                            ct[i + 6] = (Math.Max(0, -pricematrix[i + 6, step - 1] + strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[6])) - cv7 * Math.Exp(-rate * time);
                            ct[i + 7] = (Math.Max(0, -pricematrix[i + 7, step - 1] + strike)) * Math.Exp(-(rate - deltar) * time) * (1 - Convert.ToInt32(iszero[7])) - cv8 * Math.Exp(-rate * time);
                        }
                    }
                }
                else//option value is not 0 once price is high than bar
                {
                    bool[] iszero = new bool[8];
                    for (int i = 0; i < 8 * trial; i = i + 8)
                    {
                        for (int k = i; k < 8 + i; k++)
                        {
                            iszero[k - i] = true; //define whether satisfy conditions
                        }
                        double cv1 = 0;           //define delta hedge
                        double cv2 = 0; double cv3 = 0; double cv4 = 0; double cv5 = 0; double cv6 = 0; double cv7 = 0; double cv8 = 0;
                        for (int j = 1; j < step; j++)
                        {
                            if (pricematrix[i, j] > bar)
                            {
                                iszero[0] = false; break;
                            }
                            if (pricematrix[i + 1, j] > bar)
                            {
                                iszero[1] = false; break;
                            }
                            if (pricematrix[i + 2, j] > bar)
                            {
                                iszero[2] = false; break;
                            }
                            if (pricematrix[i + 3, j] > bar)
                            {
                                iszero[3] = false; break;
                            }
                            if (pricematrix[i + 4, j] > bar)
                            {
                                iszero[4] = false; break;
                            }
                            if (pricematrix[i + 5, j] > bar)
                            {
                                iszero[5] = false; break;
                            }
                            if (pricematrix[i + 6, j] > bar)
                            {
                                iszero[6] = false; break;
                            }
                            if (pricematrix[i + 7, j] > bar)
                            {
                                iszero[7] = false; break;
                            }                                                             //determine whether meet the condition
                            t = dt * (step - j);
                            //get d1 of bs formula
                            double d11 = (Math.Log(pricematrix[i, j - 1] / strike) + (rate + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                            double d12 = (Math.Log(pricematrix[i + 1, j - 1] / strike) + (rate + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                            double d13 = (Math.Log(pricematrix[i + 2, j - 1] / strike) + (rate + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                            double d14 = (Math.Log(pricematrix[i + 3, j - 1] / strike) + (rate + Math.Pow(vol + deltavol, 2) / 2) * t) / ((vol + deltavol) * Math.Pow(t, 0.5));
                            double d15 = (Math.Log(pricematrix[i + 4, j - 1] / strike) + (rate + deltar + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                            double d16 = (Math.Log(pricematrix[i + 5, j - 1] / strike) + (rate + Math.Pow(vol, 2) / 2) * (t * step + dt) / step) / (vol * Math.Pow((t * step + dt) / step, 0.5));
                            double d17 = (Math.Log(pricematrix[i + 6, j - 1] / strike) + (rate + Math.Pow(vol - deltavol, 2) / 2) * t) / ((vol - deltavol) * Math.Pow(t, 0.5));
                            double d18 = (Math.Log(pricematrix[i + 7, j - 1] / strike) + (rate - deltar + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                            //get delta
                            if (calloption)
                            {
                                double delta1 = cdf.CumDensity(d11); double delta2 = cdf.CumDensity(d12);
                                double delta3 = cdf.CumDensity(d13); double delta4 = cdf.CumDensity(d14);
                                double delta5 = cdf.CumDensity(d15); double delta6 = cdf.CumDensity(d16);
                                double delta7 = cdf.CumDensity(d17); double delta8 = cdf.CumDensity(d18);
                                //get delta hedge
                                cv1 = cv1 + delta1 * (pricematrix[i, j] - pricematrix[i, j - 1] * Math.Exp(rate * dt));
                                cv2 = cv2 + delta2 * (pricematrix[i + 1, j] - pricematrix[i + 1, j - 1] * Math.Exp(rate * dt));
                                cv3 = cv3 + delta3 * (pricematrix[i + 2, j] - pricematrix[i + 2, j - 1] * Math.Exp(rate * dt));
                                cv4 = cv4 + delta4 * (pricematrix[i + 3, j] - pricematrix[i + 3, j - 1] * Math.Exp(rate * dt));
                                cv5 = cv5 + delta5 * (pricematrix[i + 4, j] - pricematrix[i + 4, j - 1] * Math.Exp((rate + deltar) * dt));
                                cv6 = cv6 + delta6 * (pricematrix[i + 5, j] - pricematrix[i + 5, j - 1] * Math.Exp(rate * (dt + dt / step)));
                                cv7 = cv7 + delta7 * (pricematrix[i + 6, j] - pricematrix[i + 6, j - 1] * Math.Exp(rate * dt));
                                cv8 = cv8 + delta8 * (pricematrix[i + 7, j] - pricematrix[i + 7, j - 1] * Math.Exp((rate - deltar) * dt));
                            }
                            else
                            {
                                double delta1 = cdf.CumDensity(d11) - 1; double delta2 = cdf.CumDensity(d12) - 1;
                                double delta3 = cdf.CumDensity(d13) - 1; double delta4 = cdf.CumDensity(d14) - 1;
                                double delta5 = cdf.CumDensity(d15) - 1; double delta6 = cdf.CumDensity(d16) - 1;
                                double delta7 = cdf.CumDensity(d17) - 1; double delta8 = cdf.CumDensity(d18) - 1;
                                //get delta hedge
                                cv1 = cv1 + delta1 * (pricematrix[i, j] - pricematrix[i, j - 1] * Math.Exp(rate * dt));
                                cv2 = cv2 + delta2 * (pricematrix[i + 1, j] - pricematrix[i + 1, j - 1] * Math.Exp(rate * dt));
                                cv3 = cv3 + delta3 * (pricematrix[i + 2, j] - pricematrix[i + 2, j - 1] * Math.Exp(rate * dt));
                                cv4 = cv4 + delta4 * (pricematrix[i + 3, j] - pricematrix[i + 3, j - 1] * Math.Exp(rate * dt));
                                cv5 = cv5 + delta5 * (pricematrix[i + 4, j] - pricematrix[i + 4, j - 1] * Math.Exp((rate + deltar) * dt));
                                cv6 = cv6 + delta6 * (pricematrix[i + 5, j] - pricematrix[i + 5, j - 1] * Math.Exp(rate * (dt + dt / step)));
                                cv7 = cv7 + delta7 * (pricematrix[i + 6, j] - pricematrix[i + 6, j - 1] * Math.Exp(rate * dt));
                                cv8 = cv8 + delta8 * (pricematrix[i + 7, j] - pricematrix[i + 7, j - 1] * Math.Exp((rate - deltar) * dt));
                            }
                        }
                        if (calloption) //get optionprice
                        {
                            ct[i]     = (Math.Max(0, pricematrix[i, step - 1] - strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[0])) - cv1 * Math.Exp(-rate * time);
                            ct[i + 1] = (Math.Max(0, pricematrix[i + 1, step - 1] - strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[1])) - cv2 * Math.Exp(-rate * time);
                            ct[i + 2] = (Math.Max(0, pricematrix[i + 2, step - 1] - strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[2])) - cv3 * Math.Exp(-rate * time);
                            ct[i + 3] = (Math.Max(0, pricematrix[i + 3, step - 1] - strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[3])) - cv4 * Math.Exp(-rate * time);
                            ct[i + 4] = (Math.Max(0, pricematrix[i + 4, step - 1] - strike)) * Math.Exp(-(rate + deltar) * time) * (1 - Convert.ToInt32(iszero[4])) - cv5 * Math.Exp(-rate * time);
                            ct[i + 5] = (Math.Max(0, pricematrix[i + 5, step - 1] - strike)) * Math.Exp(-rate * (time + dt)) * (1 - Convert.ToInt32(iszero[5])) - cv6 * Math.Exp(-rate * time);
                            ct[i + 6] = (Math.Max(0, pricematrix[i + 6, step - 1] - strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[6])) - cv7 * Math.Exp(-rate * time);
                            ct[i + 7] = (Math.Max(0, pricematrix[i + 7, step - 1] - strike)) * Math.Exp(-(rate - deltar) * time) * (1 - Convert.ToInt32(iszero[7])) - cv8 * Math.Exp(-rate * time);
                        }
                        else
                        {
                            ct[i]     = (Math.Max(0, -pricematrix[i, step - 1] + strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[0])) - cv1 * Math.Exp(-rate * time);
                            ct[i + 1] = (Math.Max(0, -pricematrix[i + 1, step - 1] + strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[1])) - cv2 * Math.Exp(-rate * time);
                            ct[i + 2] = (Math.Max(0, -pricematrix[i + 2, step - 1] + strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[2])) - cv3 * Math.Exp(-rate * time);
                            ct[i + 3] = (Math.Max(0, -pricematrix[i + 3, step - 1] + strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[3])) - cv4 * Math.Exp(-rate * time);
                            ct[i + 4] = (Math.Max(0, -pricematrix[i + 4, step - 1] + strike)) * Math.Exp(-(rate + deltar) * time) * (1 - Convert.ToInt32(iszero[4])) - cv5 * Math.Exp(-rate * time);
                            ct[i + 5] = (Math.Max(0, -pricematrix[i + 5, step - 1] + strike)) * Math.Exp(-rate * (time + dt)) * (1 - Convert.ToInt32(iszero[5])) - cv6 * Math.Exp(-rate * time);
                            ct[i + 6] = (Math.Max(0, -pricematrix[i + 6, step - 1] + strike)) * Math.Exp(-rate * time) * (1 - Convert.ToInt32(iszero[6])) - cv7 * Math.Exp(-rate * time);
                            ct[i + 7] = (Math.Max(0, -pricematrix[i + 7, step - 1] + strike)) * Math.Exp(-(rate - deltar) * time) * (1 - Convert.ToInt32(iszero[7])) - cv8 * Math.Exp(-rate * time);
                        }
                    }
                }
            }
            return(ct);
        }
Ejemplo n.º 6
0
        //use CV method to get call and put in control variate
        public override double[] CVmethod(double strike, double stockprice, double rate, double vol, int step, int trial, double time, double[,] pricematrix, bool calloption)
        {
            double    deltas = 0.001 * stockprice;
            double    deltavol = 0.001 * vol;
            double    deltar = 0.001 * rate;
            double    dt = time / step; double t;
            CDFmethod cdf = new CDFmethod();           //compute cdf

            double[] ct       = new double[8 * trial]; //define a list to save option price
            double[] maxprice = new double[8 * trial];
            double[] minprice = new double[trial * 8];
            for (int i = 0; i < 8 * trial; i = i + 8)
            {
                double cv1 = 0;//define delta hedge
                double cv2 = 0; double cv3 = 0; double cv4 = 0; double cv5 = 0; double cv6 = 0; double cv7 = 0; double cv8 = 0;
                for (int k = i; k < i + 8; k++)
                {
                    maxprice[k] = 0; minprice[k] = pricematrix[i, 0];
                }
                for (int j = 1; j < step; j++)
                {
                    t = dt * (step - j);
                    //get d1 of bs formula
                    for (int k = i; k < 8 + i; k++)
                    {
                        if (maxprice[k] < pricematrix[k, j])
                        {
                            maxprice[k] = pricematrix[k, j];
                        }
                        if (minprice[k] > pricematrix[k, j])
                        {
                            minprice[k] = pricematrix[k, j];
                        }
                    }
                    double d11 = (Math.Log(pricematrix[i, j - 1] / strike) + (rate + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                    double d12 = (Math.Log(pricematrix[i + 1, j - 1] / strike) + (rate + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                    double d13 = (Math.Log(pricematrix[i + 2, j - 1] / strike) + (rate + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                    double d14 = (Math.Log(pricematrix[i + 3, j - 1] / strike) + (rate + Math.Pow(vol + deltavol, 2) / 2) * t) / ((vol + deltavol) * Math.Pow(t, 0.5));
                    double d15 = (Math.Log(pricematrix[i + 4, j - 1] / strike) + (rate + deltar + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                    double d16 = (Math.Log(pricematrix[i + 5, j - 1] / strike) + (rate + Math.Pow(vol, 2) / 2) * (t * step + dt) / step) / (vol * Math.Pow((t * step + dt) / step, 0.5));
                    double d17 = (Math.Log(pricematrix[i + 6, j - 1] / strike) + (rate + Math.Pow(vol - deltavol, 2) / 2) * t) / ((vol - deltavol) * Math.Pow(t, 0.5));
                    double d18 = (Math.Log(pricematrix[i + 7, j - 1] / strike) + (rate - deltar + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                    //get delta
                    if (calloption)
                    {
                        double delta1 = cdf.CumDensity(d11); double delta2 = cdf.CumDensity(d12);
                        double delta3 = cdf.CumDensity(d13); double delta4 = cdf.CumDensity(d14);
                        double delta5 = cdf.CumDensity(d15); double delta6 = cdf.CumDensity(d16);
                        double delta7 = cdf.CumDensity(d17); double delta8 = cdf.CumDensity(d18);
                        //get delta hedge
                        cv1 = cv1 + delta1 * (pricematrix[i, j] - pricematrix[i, j - 1] * Math.Exp(rate * dt));
                        cv2 = cv2 + delta2 * (pricematrix[i + 1, j] - pricematrix[i + 1, j - 1] * Math.Exp(rate * dt));
                        cv3 = cv3 + delta3 * (pricematrix[i + 2, j] - pricematrix[i + 2, j - 1] * Math.Exp(rate * dt));
                        cv4 = cv4 + delta4 * (pricematrix[i + 3, j] - pricematrix[i + 3, j - 1] * Math.Exp(rate * dt));
                        cv5 = cv5 + delta5 * (pricematrix[i + 4, j] - pricematrix[i + 4, j - 1] * Math.Exp((rate + deltar) * dt));
                        cv6 = cv6 + delta6 * (pricematrix[i + 5, j] - pricematrix[i + 5, j - 1] * Math.Exp(rate * (dt + dt / step)));
                        cv7 = cv7 + delta7 * (pricematrix[i + 6, j] - pricematrix[i + 6, j - 1] * Math.Exp(rate * dt));
                        cv8 = cv8 + delta8 * (pricematrix[i + 7, j] - pricematrix[i + 7, j - 1] * Math.Exp((rate - deltar) * dt));
                    }
                    else
                    {
                        double delta1 = cdf.CumDensity(d11) - 1; double delta2 = cdf.CumDensity(d12) - 1;
                        double delta3 = cdf.CumDensity(d13) - 1; double delta4 = cdf.CumDensity(d14) - 1;
                        double delta5 = cdf.CumDensity(d15) - 1; double delta6 = cdf.CumDensity(d16) - 1;
                        double delta7 = cdf.CumDensity(d17) - 1; double delta8 = cdf.CumDensity(d18) - 1;
                        //get delta hedge
                        cv1 = cv1 + delta1 * (pricematrix[i, j] - pricematrix[i, j - 1] * Math.Exp(rate * dt));
                        cv2 = cv2 + delta2 * (pricematrix[i + 1, j] - pricematrix[i + 1, j - 1] * Math.Exp(rate * dt));
                        cv3 = cv3 + delta3 * (pricematrix[i + 2, j] - pricematrix[i + 2, j - 1] * Math.Exp(rate * dt));
                        cv4 = cv4 + delta4 * (pricematrix[i + 3, j] - pricematrix[i + 3, j - 1] * Math.Exp(rate * dt));
                        cv5 = cv5 + delta5 * (pricematrix[i + 4, j] - pricematrix[i + 4, j - 1] * Math.Exp((rate + deltar) * dt));
                        cv6 = cv6 + delta6 * (pricematrix[i + 5, j] - pricematrix[i + 5, j - 1] * Math.Exp(rate * (dt + dt / step)));
                        cv7 = cv7 + delta7 * (pricematrix[i + 6, j] - pricematrix[i + 6, j - 1] * Math.Exp(rate * dt));
                        cv8 = cv8 + delta8 * (pricematrix[i + 7, j] - pricematrix[i + 7, j - 1] * Math.Exp((rate - deltar) * dt));
                    }
                }
                ct[i]     = (maxprice[i] - minprice[i] - cv1) * Math.Exp(-rate * time);
                ct[i + 1] = (maxprice[i + 1] - minprice[i + 1] - cv2) * Math.Exp(-rate * time);
                ct[i + 2] = (maxprice[i + 2] - minprice[i + 2] - cv3) * Math.Exp(-rate * time);
                ct[i + 3] = (maxprice[i + 3] - minprice[i + 3] - cv4) * Math.Exp(-rate * time);
                ct[i + 4] = (maxprice[i + 4] - minprice[i + 4] - cv5) * Math.Exp(-(rate + deltar) * time);
                ct[i + 5] = (maxprice[i + 5] - minprice[i + 5] - cv6) * Math.Exp(-rate * (time + dt));
                ct[i + 6] = (maxprice[i + 6] - minprice[i + 6] - cv7) * Math.Exp(-rate * time);
                ct[i + 7] = (maxprice[i + 7] - minprice[i + 7] - cv8) * Math.Exp(-(rate - deltar) * time);
            }
            return(ct);
        }
Ejemplo n.º 7
0
        public override double[] CVput(double strike, double stockprice, double rate, double vol, int step, int trial, double time, double[,] randommatrix)
        {
            double[,] price = pricematrix(strike, stockprice, rate, vol, step, trial, time, randommatrix);
            double    dt = time / step; double t;
            CDFmethod cdf = new CDFmethod();

            double[] ct = new double[trial];//define a list to save option price and standard error
            if (isdown == true)
            {
                if (isout == true)//option value is 0 when price is less than bar
                {
                    for (int i = 0; i < trial; i++)
                    {
                        bool iszero = false; //define whether satisfy conditions
                        ct[i] = 0;
                        double cv = 0;       //define delta hedge
                        for (int j = 1; j < step; j++)
                        {
                            if (price[i, j] < bar)
                            {
                                iszero = true; break;
                            }
                            t = dt * (step - j);
                            double d1    = (Math.Log(price[i, j - 1] / strike) + (rate + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                            double delta = cdf.CumDensity(d1) - 1;
                            cv = cv + delta * (price[i, j] - price[i, j - 1] * Math.Exp(rate * dt));
                        }
                        if (iszero == false)
                        {
                            ct[i] = (Math.Max(0, -price[i, step - 1] + strike) - cv) * Math.Exp(-rate * time);
                        }
                        else
                        {
                            ct[i] = -cv *Math.Exp(-rate *time);
                        }
                    }
                }
                else//option value is not zero when price is less than bar
                {
                    for (int i = 0; i < trial; i++)
                    {
                        ct[i] = 0;
                        bool   iszero = true; //define whether satisfy conditions
                        double cv     = 0;    //define delta hedge
                        for (int j = 1; j < step; j++)
                        {
                            if (price[i, j] < bar)
                            {
                                iszero = false;
                            }
                            t = dt * (step - j);
                            double d1    = (Math.Log(price[i, j - 1] / strike) + (rate + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                            double delta = cdf.CumDensity(d1) - 1;
                            cv = cv + delta * (price[i, j] - price[i, j - 1] * Math.Exp(rate * dt));
                        }
                        if (iszero == false)
                        {
                            ct[i] = (Math.Max(0, -price[i, step - 1] + strike) - cv) * Math.Exp(-rate * time);
                        }
                        else
                        {
                            ct[i] = -cv *Math.Exp(-rate *time);
                        }
                    }
                }
            }
            else
            {
                if (isout == true)//option value is 0 when price is more than bar
                {
                    for (int i = 0; i < trial; i++)
                    {
                        bool iszero = false; //define whether satisfy conditions
                        ct[i] = 0;
                        double cv = 0;       //define delta hedge
                        for (int j = 1; j < step; j++)
                        {
                            if (price[i, j] > bar)
                            {
                                iszero = true; break;
                            }
                            t = dt * (step - j);
                            double d1    = (Math.Log(price[i, j - 1] / strike) + (rate + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                            double delta = cdf.CumDensity(d1) - 1;
                            cv = cv + delta * (price[i, j] - price[i, j - 1] * Math.Exp(rate * dt));
                        }
                        if (iszero == false)
                        {
                            ct[i] = (Math.Max(0, -price[i, step - 1] + strike) - cv) * Math.Exp(-rate * time);
                        }
                        else
                        {
                            ct[i] = -cv *Math.Exp(-rate *time);
                        }
                    }
                }
                else//option value is not zero when price is more than bar
                {
                    for (int i = 0; i < trial; i++)
                    {
                        ct[i] = 0;
                        bool   iszero = true; //define whether satisfy conditions
                        double cv     = 0;    //define delta hedge
                        for (int j = 1; j < step; j++)
                        {
                            if (price[i, j] > bar)
                            {
                                iszero = false;
                            }
                            t = dt * (step - j);
                            double d1    = (Math.Log(price[i, j - 1] / strike) + (rate + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                            double delta = cdf.CumDensity(d1) - 1;
                            cv = cv + delta * (price[i, j] - price[i, j - 1] * Math.Exp(rate * dt));
                        }
                        if (iszero == false)
                        {
                            ct[i] = (Math.Max(0, -price[i, step - 1] + strike) - cv) * Math.Exp(-rate * time);
                        }
                        else
                        {
                            ct[i] = -cv *Math.Exp(-rate *time);
                        }
                    }
                }
            }
            return(ct);
        }
Ejemplo n.º 8
0
        //use CV method to get call and put in control variate
        public override double[] CVmethod(double strike, double stockprice, double rate, double vol, int step, int trial, double time, double[,] pricematrix, bool calloption)
        {
            double    deltas = 0.001 * stockprice;
            double    deltavol = 0.001 * vol;
            double    deltar = 0.001 * rate;
            double    dt = time / step; double t;
            CDFmethod cdf = new CDFmethod();     //compute cdf

            double[] ct = new double[8 * trial]; //define a list to save option price
            for (int i = 0; i < 8 * trial; i = i + 8)
            {
                double cv1 = 0;//define delta hedge
                double cv2 = 0; double cv3 = 0; double cv4 = 0; double cv5 = 0; double cv6 = 0; double cv7 = 0; double cv8 = 0;
                for (int j = 1; j < step; j++)
                {
                    t = dt * (step - j);
                    //get d1 of bs formula
                    double d11 = (Math.Log(pricematrix[i, j - 1] / strike) + (rate + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                    double d12 = (Math.Log(pricematrix[i + 1, j - 1] / strike) + (rate + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                    double d13 = (Math.Log(pricematrix[i + 2, j - 1] / strike) + (rate + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                    double d14 = (Math.Log(pricematrix[i + 3, j - 1] / strike) + (rate + Math.Pow(vol + deltavol, 2) / 2) * t) / ((vol + deltavol) * Math.Pow(t, 0.5));
                    double d15 = (Math.Log(pricematrix[i + 4, j - 1] / strike) + (rate + deltar + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                    double d16 = (Math.Log(pricematrix[i + 5, j - 1] / strike) + (rate + Math.Pow(vol, 2) / 2) * (t * step + dt) / step) / (vol * Math.Pow((t * step + dt) / step, 0.5));
                    double d17 = (Math.Log(pricematrix[i + 6, j - 1] / strike) + (rate + Math.Pow(vol - deltavol, 2) / 2) * t) / ((vol - deltavol) * Math.Pow(t, 0.5));
                    double d18 = (Math.Log(pricematrix[i + 7, j - 1] / strike) + (rate - deltar + Math.Pow(vol, 2) / 2) * t) / (vol * Math.Pow(t, 0.5));
                    //get delta
                    if (calloption)
                    {
                        double delta1 = cdf.CumDensity(d11); double delta2 = cdf.CumDensity(d12);
                        double delta3 = cdf.CumDensity(d13); double delta4 = cdf.CumDensity(d14);
                        double delta5 = cdf.CumDensity(d15); double delta6 = cdf.CumDensity(d16);
                        double delta7 = cdf.CumDensity(d17); double delta8 = cdf.CumDensity(d18);
                        //get delta hedge
                        cv1 = cv1 + delta1 * (pricematrix[i, j] - pricematrix[i, j - 1] * Math.Exp(rate * dt));
                        cv2 = cv2 + delta2 * (pricematrix[i + 1, j] - pricematrix[i + 1, j - 1] * Math.Exp(rate * dt));
                        cv3 = cv3 + delta3 * (pricematrix[i + 2, j] - pricematrix[i + 2, j - 1] * Math.Exp(rate * dt));
                        cv4 = cv4 + delta4 * (pricematrix[i + 3, j] - pricematrix[i + 3, j - 1] * Math.Exp(rate * dt));
                        cv5 = cv5 + delta5 * (pricematrix[i + 4, j] - pricematrix[i + 4, j - 1] * Math.Exp((rate + deltar) * dt));
                        cv6 = cv6 + delta6 * (pricematrix[i + 5, j] - pricematrix[i + 5, j - 1] * Math.Exp(rate * (dt + dt / step)));
                        cv7 = cv7 + delta7 * (pricematrix[i + 6, j] - pricematrix[i + 6, j - 1] * Math.Exp(rate * dt));
                        cv8 = cv8 + delta8 * (pricematrix[i + 7, j] - pricematrix[i + 7, j - 1] * Math.Exp((rate - deltar) * dt));
                    }
                    else
                    {
                        double delta1 = cdf.CumDensity(d11) - 1; double delta2 = cdf.CumDensity(d12) - 1;
                        double delta3 = cdf.CumDensity(d13) - 1; double delta4 = cdf.CumDensity(d14) - 1;
                        double delta5 = cdf.CumDensity(d15) - 1; double delta6 = cdf.CumDensity(d16) - 1;
                        double delta7 = cdf.CumDensity(d17) - 1; double delta8 = cdf.CumDensity(d18) - 1;
                        //get delta hedge
                        cv1 = cv1 + delta1 * (pricematrix[i, j] - pricematrix[i, j - 1] * Math.Exp(rate * dt));
                        cv2 = cv2 + delta2 * (pricematrix[i + 1, j] - pricematrix[i + 1, j - 1] * Math.Exp(rate * dt));
                        cv3 = cv3 + delta3 * (pricematrix[i + 2, j] - pricematrix[i + 2, j - 1] * Math.Exp(rate * dt));
                        cv4 = cv4 + delta4 * (pricematrix[i + 3, j] - pricematrix[i + 3, j - 1] * Math.Exp(rate * dt));
                        cv5 = cv5 + delta5 * (pricematrix[i + 4, j] - pricematrix[i + 4, j - 1] * Math.Exp((rate + deltar) * dt));
                        cv6 = cv6 + delta6 * (pricematrix[i + 5, j] - pricematrix[i + 5, j - 1] * Math.Exp(rate * (dt + dt / step)));
                        cv7 = cv7 + delta7 * (pricematrix[i + 6, j] - pricematrix[i + 6, j - 1] * Math.Exp(rate * dt));
                        cv8 = cv8 + delta8 * (pricematrix[i + 7, j] - pricematrix[i + 7, j - 1] * Math.Exp((rate - deltar) * dt));
                    }
                }
                if (calloption) //get call optionprice
                {
                    if (Math.Max(pricematrix[i, step - 1] - strike, 0) > 0)
                    {
                        ct[i] = (rebate - cv1) * Math.Exp(-rate * time);
                    }
                    else
                    {
                        ct[i] = -cv1 *Math.Exp(-rate *time);
                    }
                    if (Math.Max(pricematrix[i + 1, step - 1] - strike, 0) > 0)
                    {
                        ct[i + 1] = (rebate - cv2) * Math.Exp(-rate * time);
                    }
                    else
                    {
                        ct[i + 1] = -cv2 *Math.Exp(-rate *time);
                    }
                    if (Math.Max(pricematrix[i + 2, step - 1] - strike, 0) > 0)
                    {
                        ct[i + 2] = (rebate - cv3) * Math.Exp(-rate * time);
                    }
                    else
                    {
                        ct[i + 2] = -cv3 *Math.Exp(-rate *time);
                    }
                    if (Math.Max(pricematrix[i + 3, step - 1] - strike, 0) > 0)
                    {
                        ct[i + 3] = (rebate - cv4) * Math.Exp(-rate * time);
                    }
                    else
                    {
                        ct[i + 3] = -cv4 *Math.Exp(-rate *time);
                    }
                    if (Math.Max(pricematrix[i + 4, step - 1] - strike, 0) > 0)
                    {
                        ct[i + 4] = (rebate - cv5) * Math.Exp(-rate * time);
                    }
                    else
                    {
                        ct[i + 4] = -cv5 *Math.Exp(-rate *time);
                    }
                    if (Math.Max(pricematrix[i + 5, step - 1] - strike, 0) > 0)
                    {
                        ct[i + 5] = (rebate - cv6) * Math.Exp(-rate * time);
                    }
                    else
                    {
                        ct[i + 5] = -cv6 *Math.Exp(-rate *time);
                    }
                    if (Math.Max(pricematrix[i + 6, step - 1] - strike, 0) > 0)
                    {
                        ct[i + 6] = (rebate - cv7) * Math.Exp(-rate * time);
                    }
                    else
                    {
                        ct[i + 6] = -cv7 *Math.Exp(-rate *time);
                    }
                    if (Math.Max(pricematrix[i + 7, step - 1] - strike, 0) > 0)
                    {
                        ct[i + 7] = (rebate - cv8) * Math.Exp(-rate * time);
                    }
                    else
                    {
                        ct[i + 7] = -cv8 *Math.Exp(-rate *time);
                    }
                }
                else//get put option price
                {
                    if (Math.Max(-pricematrix[i, step - 1] + strike, 0) > 0)
                    {
                        ct[i] = (rebate - cv1) * Math.Exp(-rate * time);
                    }
                    else
                    {
                        ct[i] = -cv1 *Math.Exp(-rate *time);
                    }
                    if (Math.Max(-pricematrix[i + 1, step - 1] + strike, 0) > 0)
                    {
                        ct[i + 1] = (rebate - cv2) * Math.Exp(-rate * time);
                    }
                    else
                    {
                        ct[i + 1] = -cv2 *Math.Exp(-rate *time);
                    }
                    if (Math.Max(-pricematrix[i + 2, step - 1] + strike, 0) > 0)
                    {
                        ct[i + 2] = (rebate - cv3) * Math.Exp(-rate * time);
                    }
                    else
                    {
                        ct[i + 2] = -cv3 *Math.Exp(-rate *time);
                    }
                    if (Math.Max(-pricematrix[i + 3, step - 1] + strike, 0) > 0)
                    {
                        ct[i + 3] = (rebate - cv4) * Math.Exp(-rate * time);
                    }
                    else
                    {
                        ct[i + 3] = -cv4 *Math.Exp(-rate *time);
                    }
                    if (Math.Max(-pricematrix[i + 4, step - 1] + strike, 0) > 0)
                    {
                        ct[i + 4] = (rebate - cv5) * Math.Exp(-rate * time);
                    }
                    else
                    {
                        ct[i + 4] = -cv5 *Math.Exp(-rate *time);
                    }
                    if (Math.Max(-pricematrix[i + 5, step - 1] + strike, 0) > 0)
                    {
                        ct[i + 5] = (rebate - cv6) * Math.Exp(-rate * time);
                    }
                    else
                    {
                        ct[i + 5] = -cv6 *Math.Exp(-rate *time);
                    }
                    if (Math.Max(-pricematrix[i + 6, step - 1] + strike, 0) > 0)
                    {
                        ct[i + 6] = (rebate - cv7) * Math.Exp(-rate * time);
                    }
                    else
                    {
                        ct[i + 6] = -cv7 *Math.Exp(-rate *time);
                    }
                    if (Math.Max(-pricematrix[i + 7, step - 1] + strike, 0) > 0)
                    {
                        ct[i + 7] = (rebate - cv8) * Math.Exp(-rate * time);
                    }
                    else
                    {
                        ct[i + 7] = -cv8 *Math.Exp(-rate *time);
                    }
                }
            }
            return(ct);
        }