예제 #1
0
        public override double optionprice_cv(out double se)
        {
            Simulation sim = new Simulation();

            double[,] stockmatrix = sim.simulation(trials, steps, r, T, sigma, s, randsaved, index_anti, threadingindex);
            double[,] payoff      = new double[trials, 1];
            double cv1, cv2;
            double delta, delta2;
            double dt      = T / steps;
            double num     = -1;
            double sum_ct  = 0;
            double sum_ct2 = 0;
            double d2;

            for (int i = 0; i < trials; i++)
            {
                cv1 = 0;
                cv2 = 0;

                for (int j = 1; j < steps; j++)
                {
                    double t  = (j - 1) * dt;
                    double d1 = ((Math.Log(stockmatrix[i, j - 1] / k) + (r + (sigma * sigma) * 0.5) * (T - t))) / (sigma * Math.Sqrt(T - t));
                    if (index == 1)
                    {
                        delta = CDF(d1);
                    }
                    else
                    {
                        delta = CDF(d1) - 1;
                    }
                    cv1 += delta * (stockmatrix[i, j] - (stockmatrix[i, j - 1] * Math.Exp(r * T / (steps - 1))));

                    if (index_anti)
                    {
                        d2 = ((Math.Log(stockmatrix[i + trials, j - 1] / k) + (r + (sigma * sigma) * 0.5) * (T - t))) / (sigma * Math.Sqrt(T - t));
                        if (index == 1)
                        {
                            delta2 = CDF(d2);
                        }
                        else
                        {
                            delta2 = CDF(d2) - 1;
                        }
                        cv2 += delta2 * (stockmatrix[i + trials, j] - (stockmatrix[i + trials, j - 1] * Math.Exp(r * T / (steps - 1))));
                    }
                }

                if (index == 1)
                {
                    if (index_anti)
                    {
                        double max1 = 0;
                        double max2 = 0;
                        for (int j = 0; j < stockmatrix.GetLength(1); j++)
                        {
                            max1 = Math.Max(stockmatrix[i, j], max1);
                            max2 = Math.Max(stockmatrix[i + trials, j], max2);
                        }
                        payoff[i, 0] = 0.5 * ((Math.Max(max1 - k, 0) + num * cv1) + (Math.Max(max2 - k, 0) + num * cv2));
                    }
                    else
                    {
                        double max = 0;
                        for (int j = 0; j < stockmatrix.GetLength(1); j++)
                        {
                            max = Math.Max(stockmatrix[i, j], max);
                        }
                        payoff[i, 0] = Math.Max(max - k, 0) + num * cv1;
                    }
                }

                else
                {
                    if (index_anti)
                    {
                        double max1 = 0;
                        double max2 = 0;
                        for (int j = 0; j < stockmatrix.GetLength(1); j++)
                        {
                            max1 = Math.Max(stockmatrix[i, j], max1);
                            max2 = Math.Max(stockmatrix[i + trials, j], max2);
                        }
                        payoff[i, 0] = 0.5 * ((Math.Max(k - max1, 0) + num * cv1) + (Math.Max(k - max2, 0) + num * cv2));
                    }
                    else
                    {
                        double max = 0;
                        for (int j = 0; j < stockmatrix.GetLength(1); j++)
                        {
                            max = Math.Max(stockmatrix[i, j], max);
                        }
                        payoff[i, 0] = Math.Max(k - max, 0) + num * cv1;
                    }
                }

                sum_ct  += payoff[i, 0];
                sum_ct2 += Math.Pow(payoff[i, 0], 2);
            }

            double price = (sum_ct / Convert.ToDouble(trials)) * Math.Exp(-r * T);
            double std   = Math.Sqrt((sum_ct2 - sum_ct * sum_ct / Convert.ToDouble(trials)) * Math.Exp(-2 * r * T) / (Convert.ToDouble(trials - 1)));

            se = std / Math.Sqrt(trials);
            return(price);
        }
예제 #2
0
        public override double optionprice(out double se)
        {
            Simulation sim = new Simulation();

            double[,] stockmatrix = sim.simulation(trials, steps, r, T, sigma, s, randsaved, index_anti, threadingindex);
            double[,] payoff;
            if (index_anti)
            {
                payoff = new double[trials * 2, 1];
            }
            else
            {
                payoff = new double[trials, 1];
            }


            if (index == 1)
            {
                for (int i = 0; i < stockmatrix.GetLength(0); i++)
                {
                    double max = 0;
                    for (int j = 0; j < stockmatrix.GetLength(1); j++)
                    {
                        max = Math.Max(stockmatrix[i, j], max);
                    }
                    payoff[i, 0] = Math.Max((max - k), 0);
                }
            }

            if (index != 1)
            {
                for (int i = 0; i < stockmatrix.GetLength(0); i++)
                {
                    double max = 0;
                    for (int j = 0; j < stockmatrix.GetLength(1); j++)
                    {
                        max = Math.Max(stockmatrix[i, j], max);
                    }
                    payoff[i, 0] = Math.Max((k - max), 0);
                }
            }

            double sum    = 0;
            double sum_se = 0;

            for (int i = 0; i < trials; i++)
            {
                sum += payoff[i, 0];
            }
            double price = (sum / Convert.ToDouble(trials)) * Math.Exp(-r * T);

            if (index_anti)
            {
                double[,] ave_p = new double[trials, 1];
                for (int i = 0; i < trials; i++)
                {
                    ave_p[i, 0] = (payoff[i, 0] + payoff[i + trials, 0]) * 0.5;
                }

                for (int i = 0; i < trials; i++)
                {
                    sum_se += Math.Pow((ave_p[i, 0] * Math.Exp(-r * T)) - price, 2);
                }
            }
            else
            {
                for (int i = 0; i < trials; i++)
                {
                    sum_se += Math.Pow((payoff[i, 0] * Math.Exp(-r * T)) - price, 2);
                }
            }

            double std = Math.Sqrt((1 / Convert.ToDouble((trials - 1))) * sum_se);

            se = std / Math.Sqrt(trials);

            return(price);
        }