Пример #1
0
        public static double Price(double startVal, double strike, double riskFreeRate, double volatility, double yearsToMaturity,
            int numCurvePoints, int numSteps, double maxTimeStepDays, int numBatches, int numScenariosPerBatch, int numStates, int seed)
        {
            //Grid Setup
            int numPivotPoints = 7;
            double[] pivotPoints = new double[numPivotPoints];
            double[] gridSpacings = new double[numPivotPoints];
            pivotPoints[0] = 1; gridSpacings[0] = 5;
            pivotPoints[1] = 70; gridSpacings[1] = 2.5;
            pivotPoints[2] = 90; gridSpacings[2] = 1;
            pivotPoints[3] = 110; gridSpacings[3] = 2.5;
            pivotPoints[4] = 140; gridSpacings[4] = 5;
            pivotPoints[5] = 200; gridSpacings[5] = 7.5;
            pivotPoints[6] = 300; gridSpacings[6] = 10;

            //Setup interest rate curve and discount curve
            DateTime today = DateTime.Today;
            double daysInYear = 365.25;
            double daysToMaturity = yearsToMaturity * daysInYear;

            DateTime[] rateCurveTimes = BuildDates(today, daysToMaturity, numCurvePoints);

            double[] rates = new double[numCurvePoints];
            double[] discountFactors = new double[numCurvePoints];
            for (int iPoint = 0; iPoint < numCurvePoints; ++iPoint)
            {
                rates[iPoint] = riskFreeRate;

                if (iPoint == 0)
                    discountFactors[0] = Math.Exp(-rates[iPoint] * (rateCurveTimes[0] - today).Days / daysInYear);
                else
                    discountFactors[iPoint] = discountFactors[iPoint - 1] * Math.Exp(-rates[iPoint] * (rateCurveTimes[iPoint] - rateCurveTimes[iPoint - 1]).Days / daysInYear);
            }

            //Create grid

            //CDevice device = new CDevice(EFloatingPointPrecision.bit32, EFloatingPointUnit.device, 0);
            CDevice device = new CDevice(EFloatingPointPrecision.bit64, EFloatingPointUnit.host, 0);

            S1DGrid grid = new S1DGrid(device, numStates, today, rateCurveTimes, TimeSpan.FromDays(maxTimeStepDays), startVal,
                pivotPoints, gridSpacings);

            //Setup drift and vol arrays for geometric brownian motion
            double[][] drift = new double[numCurvePoints][];
            double[][] vol = new double[numCurvePoints][];
            for (int iCurvePoint = 0; iCurvePoint < numCurvePoints; ++iCurvePoint)
            {
                drift[iCurvePoint] = new double[numStates];
                vol[iCurvePoint] = new double[numStates];

                for (int iState = 0; iState < numStates; ++iState)
                {
                    drift[iCurvePoint][iState] = rates[iCurvePoint] * grid.host_d_xval(iState);
                    vol[iCurvePoint][iState] = volatility * grid.host_d_xval(iState);
                }
            }

            //Create model
            CLVModel model = new CLVModel(grid, "BSTestModel");
            model.mkgen(drift, vol); //drift and vol are expressed annually
            model.set_discount_curve(discountFactors);

            DateTime[] stepTimes = BuildDates(today, daysToMaturity, numSteps);

            model.make_mc_plan(numScenariosPerBatch, numBatches, stepTimes);
            model.exe_mc_plan();

            VanillaPayoffEvaluator evaluator = new VanillaPayoffEvaluator(model, strike);

            //model.device_mc_init();
            //model.device_mc_run1f(null, evaluator); //first parameter is not used

            if (seed != 0) model.host_d_mc_init_seed(seed);
            else model.host_d_mc_init();
            model.host_d_mc_run1f(null, evaluator); //first parameter is not used

            double payoff = evaluator.GetPayoff();

            //do discounting
            double pv = payoff * discountFactors[discountFactors.Length - 1];

            return pv;
        }
Пример #2
0
 public S1DGrid(S1DGrid grid, bool deep_copy)
     : base(grid, deep_copy)
 {
 }
Пример #3
0
        static void Main(string[] args)
        {
            Console.WriteLine("running benchmark for local volatility model on the cpu");
            int ni = 10;
            double S0 = 100;
            int nscen_per_batch = 4096 * 25;
            int nbatches = 120;
            TimeSpan dt0 = TimeSpan.FromDays(1);
            EFloatingPointUnit fpu = EFloatingPointUnit.host;
            EFloatingPointPrecision fpp = EFloatingPointPrecision.bit64;
            DateTime today = DateTime.Today;
            DateTime[] t_k = new DateTime[40];
            for (int k = 0; k < 40; k++) t_k[k] = today.AddDays(7 * (k + 1));
            DateTime[] t_i = new DateTime[ni];
            t_i[0] = today.AddDays(30); t_i[1] = today.AddDays(60);
            t_i[2] = today.AddDays(90); t_i[3] = today.AddDays(120);
            t_i[4] = today.AddDays(150); t_i[5] = today.AddDays(180);
            t_i[6] = today.AddDays(210); t_i[7] = today.AddDays(240);
            t_i[8] = today.AddDays(270); t_i[9] = today.AddDays(300);
            double[] xpivot_p = new double[7];
            double[] xgridspacing_p = new double[7];
            xpivot_p[0] = 1; xgridspacing_p[0] = 5;
            xpivot_p[1] = 70; xgridspacing_p[1] = 2.5;
            xpivot_p[2] = 90; xgridspacing_p[2] = 1;
            xpivot_p[3] = 110; xgridspacing_p[3] = 2.5;
            xpivot_p[4] = 140; xgridspacing_p[4] = 5;
            xpivot_p[5] = 200; xgridspacing_p[5] = 7.5;
            xpivot_p[6] = 300; xgridspacing_p[6] = 10;
            int nx = 128;

            OPModel.Types.CDevice device = new OPModel.Types.CDevice(fpp, fpu, 0);
            OPModel.Types.S1DGrid grid = new OPModel.Types.S1DGrid(device, nx, today, t_i, dt0, S0, xpivot_p, xgridspacing_p);

            double beta_i;
            double[] ir_i = new double[ni];
            double[] df_i = new double[ni];
            double[][] SDrift_i_y = new double[ni][];
            double[][] SVol_i_y = new double[ni][];
            for (int i = 0; i < ni; i++)
            {
                ir_i[i] = 0.05;
                if (i == 0)
                    df_i[0] = Math.Exp(-ir_i[i] * (t_i[0] - grid.today).Days / 365.25);
                else
                    df_i[i] = df_i[i - 1] * Math.Exp(-ir_i[i] * (t_i[i] - t_i[i - 1]).Days / 365.25);

                double Sigma0 = 0.25;
                beta_i = 1;
                SDrift_i_y[i] = new double[grid.d];
                SVol_i_y[i] = new double[grid.d];
                for (int y = 0; y < grid.d; y++)
                {
                    SDrift_i_y[i][y] = ir_i[i] * grid.host_d_xval(y);
                    SVol_i_y[i][y] = Sigma0 * grid.host_d_xval(grid.y0) * Math.Pow(grid.host_d_xval(y) / grid.host_d_xval(grid.y0), beta_i);
                }
            }

            CStopWatch sw = new CStopWatch();
            sw.Reset();
            CLVModel model = new CLVModel(grid, "DCLV1F");

            model.set_discount_curve(df_i);
            model.mkgen(SDrift_i_y, SVol_i_y);
            model.make_mc_plan(nscen_per_batch, nbatches, t_k);

            model.reset_flop_counter();
            double time = sw.Peek();
            sw.Reset();
            model.exe_mc_plan();
            time = sw.Peek();
            double nflops = model.cpu_nflops;

            double gigaflops_per_second = nflops / (1000000000d * time);

            Console.WriteLine("blas performance: " + String.Format("{0:0.0}", gigaflops_per_second) + " GF/sec");

            CMCEvaluator evaluator = new emptyEvaluator();
            model.host_d_mc_init();
            double[] payoff_a = new double[nscen_per_batch * model.mcplan.nth];
            sw.Reset();
            unsafe
            {
                model.host_d_mc_run1f(payoff_a, evaluator);
            }

            time = sw.Peek();

            double nevals = (double)t_k.Length * (double)nbatches * (double)nscen_per_batch;
            double milion_evals_per_second = nevals / (1000000 * time);

            Console.WriteLine("mc performance: " + String.Format("{0:0.0}", milion_evals_per_second) + " milion eval/sec");
            Console.Read();
        }
Пример #4
0
        public static void run_benchmark_gpu_sglv1f(uint dev)
        {
            Console.WriteLine("running sglv1f, device " + dev);

            int ni = 10;
            double S0 = 100;
            int nscen_per_batch = 4096 ;
            int nbatches = 10;
            TimeSpan dt0 = TimeSpan.FromDays(1);
            EFloatingPointUnit fpu = EFloatingPointUnit.device;
            EFloatingPointPrecision fpp = EFloatingPointPrecision.bit32;
            DateTime today = DateTime.Today;
            DateTime[] t_k = new DateTime[40];
            for (int k = 0; k < 40; k++) t_k[k] = today.AddDays(7 * (k + 1));
            DateTime[] t_i = new DateTime[ni];
            t_i[0] = today.AddDays(30); t_i[1] = today.AddDays(60);
            t_i[2] = today.AddDays(90); t_i[3] = today.AddDays(120);
            t_i[4] = today.AddDays(150); t_i[5] = today.AddDays(180);
            t_i[6] = today.AddDays(210); t_i[7] = today.AddDays(240);
            t_i[8] = today.AddDays(270); t_i[9] = today.AddDays(300);
            double[] xpivot_p = new double[7];
            double[] xgridspacing_p = new double[7];
            xpivot_p[0] = 1; xgridspacing_p[0] = 5;
            xpivot_p[1] = 70; xgridspacing_p[1] = 2.5;
            xpivot_p[2] = 90; xgridspacing_p[2] = 1;
            xpivot_p[3] = 110; xgridspacing_p[3] = 2.5;
            xpivot_p[4] = 140; xgridspacing_p[4] = 5;
            xpivot_p[5] = 200; xgridspacing_p[5] = 7.5;
            xpivot_p[6] = 300; xgridspacing_p[6] = 10;
            int nx = 128;

            OPModel.Types.CDevice device = new OPModel.Types.CDevice(fpp, fpu, dev);
            OPModel.Types.S1DGrid grid = new OPModel.Types.S1DGrid(device, nx, today, t_i, dt0, S0, xpivot_p, xgridspacing_p);

            double beta_i;
            double[] ir_i = new double[ni];
            double[] df_i = new double[ni];
            double[][] SDrift_i_y = new double[ni][];
            double[][] SVol_i_y = new double[ni][];
            for (int i = 0; i < ni; i++)
            {
                ir_i[i] = 0.05;

                if (i == 0)
                    df_i[0] = Math.Exp(-ir_i[i] * (t_i[0] - grid.today).Days / 365.25);
                else
                    df_i[i] = df_i[i - 1] * Math.Exp(-ir_i[i] * (t_i[i] - t_i[i - 1]).Days / 365.25);

                double Sigma0 = 0.25;
                beta_i = 1;
                SDrift_i_y[i] = new double[grid.d];
                SVol_i_y[i] = new double[grid.d];
                for (int y = 0; y < grid.d; y++)
                {
                    SDrift_i_y[i][y] = ir_i[i] * grid.host_d_xval(y);
                    SVol_i_y[i][y] = Sigma0 * grid.host_d_xval(grid.y0) * Math.Pow(grid.host_d_xval(y) / grid.host_d_xval(grid.y0), beta_i);
                }
            }

            CStopWatch sw = new CStopWatch();
            sw.Reset();

            CLVModel model = new CLVModel(grid, "SGLV1F");
            model.set_discount_curve(df_i);
            model.mkgen(SDrift_i_y, SVol_i_y);
            model.make_mc_plan(nscen_per_batch, nbatches, t_k);
            model.reset_flop_counter();
            sw.Reset();
            model.exe_mc_plan();
            model.device_thread_synchronize();
            double time = sw.Peek();
            double nflops = model.gpu_nflops;
            double gigaflops_per_second = nflops / (1000000000d * time);

            Console.WriteLine("blas performance: " + String.Format("{0:0.0}", gigaflops_per_second) + " GF/sec");

            CMCEvaluator evaluator = new emptyEvaluator();
            double[] pdf_y = new double[grid.d];
            if (model.device_mc_init() == 1)
            {
                Console.WriteLine("device_mc_init() failed on device " + dev + ". Aborting");
                return;
            }
            sw.Reset();
            unsafe
            {
                model.device_mc_run1f(pdf_y, evaluator);
            }
            model.device_thread_synchronize();
            time = sw.Peek();

            double nevals = (double)t_k.Length * (double)nbatches * (double)nscen_per_batch;
            double milion_evals_per_second = nevals / (1000000 * time);
            int status = opcuda_shutdown();
            if (status != 0) throw new ExecutionEngineException();

            Console.WriteLine("mc performance: " + String.Format("{0:0.0}", milion_evals_per_second) + " milion eval/sec");
        }