Exemplo n.º 1
0
        public void Test()
        {
            Engine.MultiThread = true;

            Document   doc = new Document();
            ProjectROV rov = new ProjectROV(doc);

            doc.Part.Add(rov);
            doc.DefaultProject.NMethods.m_UseAntiteticPaths = true;

            int    n_sim       = 10000;
            int    n_steps     = 512;
            double a           = 0.2;
            double DR          = 0.02;
            double r0          = 0.015;
            double a1          = 1.0;
            double sigma1      = 0.01;
            double a2          = 0.1;
            double sigma2      = 0.0165;
            double correlation = 0.6;
            double maturityOpt = 5.0;
            double strike      = 0.927;
            double tau         = 2.0;

            ModelParameter PT = new ModelParameter(maturityOpt, "TT");

            PT.VarName = "TT";
            rov.Symbols.Add(PT);

            ModelParameter Ptau = new ModelParameter(tau, "tau");

            Ptau.VarName = "tau";
            rov.Symbols.Add(Ptau);

            ModelParameter Pa = new ModelParameter(a, "a");

            Pa.VarName = "a";
            rov.Symbols.Add(Pa);

            ModelParameter PDR = new ModelParameter(DR, "PDR");

            PDR.VarName = "DR";
            rov.Symbols.Add(PDR);

            ModelParameter Pr0 = new ModelParameter(r0, "r0");

            Pr0.VarName = "r0";
            rov.Symbols.Add(Pr0);

            ModelParameter Pstrike = new ModelParameter(strike, "strike");

            Pstrike.VarName = "strike";
            rov.Symbols.Add(Pstrike);

            AFunction zerorate = new AFunction(rov);

            zerorate.VarName = "zr";
            zerorate.m_IndependentVariables = 1;
            zerorate.m_Value = (RightValue)("(1-exp(-a*x1))*DR + r0");
            rov.Symbols.Add(zerorate);
            HW2ProcessType pt = new HW2ProcessType();

            // Set the short rate process.
            pt = HW2ProcessType.ShortRate;
            StocasticProcessHW2 process1 = new StocasticProcessHW2(rov, pt);

            process1.zero_rate_curve = "@zr";
            process1._a = (ModelParameter)a1;
            process1._b = (ModelParameter)sigma1;
            rov.Processes.AddProcess(process1);

            // Set the mean reversion process.
            pt = HW2ProcessType.Unobservable;
            StocasticProcessHW2 process2 = new StocasticProcessHW2(rov, pt);

            process2._a = (ModelParameter)a2;
            process2._b = (ModelParameter)sigma2;
            rov.Processes.AddProcess(process2);

            // Set the correlation.
            rov.Processes.r[0, 1] = (RightValue)correlation;

            // Set the discounting.
            RiskFreeInfo rfi = rov.GetDiscountingModel() as RiskFreeInfo;

            rfi.ActualizationType = EActualizationType.Stochastic;
            rfi.m_deterministicRF = (ModelParameter)"@v1";

            OptionTree op = new OptionTree(rov);

            op.PayoffInfo.PayoffExpression          = "max(bond(TT;TT+tau;@v1)-strike;0)";
            op.PayoffInfo.Timing.EndingTime.m_Value = (RightValue)maturityOpt;
            op.PayoffInfo.European = true;
            rov.Map.Root           = op;

            rov.NMethods.Technology      = ETechType.T_SIMULATION;
            rov.NMethods.PathsNumber     = n_sim;
            rov.NMethods.SimulationSteps = n_steps;

            ROVSolver solver = new ROVSolver();

            solver.BindToProject(rov);
            solver.DoValuation(-1);

            if (rov.HasErrors)
            {
                Console.WriteLine(rov.m_RuntimeErrorList[0]);
            }

            Assert.IsFalse(rov.HasErrors);

            ResultItem price            = rov.m_ResultList[0] as ResultItem;
            double     sampleMean       = price.value;
            double     sampleDevSt      = price.stdDev / Math.Sqrt((double)n_sim);
            double     theoreticalPrice = HW2BondCall(zerorate, maturityOpt, maturityOpt + tau, strike,
                                                      a1, sigma1, a2, sigma2, correlation);

            Console.WriteLine("\nTheoretical Price = " + theoreticalPrice.ToString());
            Console.WriteLine("Monte Carlo Price = " + sampleMean.ToString());
            Console.WriteLine("Standard Deviation = " + sampleDevSt.ToString());

            bool   result;
            double fact = 4.0;

            result = (Math.Abs(sampleMean - theoreticalPrice) < fact * sampleDevSt);

            Assert.IsTrue(result);
        }
Exemplo n.º 2
0
        public void Test()
        {
            Engine.MultiThread = true;

            Document doc = new Document();
            ProjectROV rov = new ProjectROV(doc);
            doc.Part.Add(rov);
            doc.DefaultProject.NMethods.m_UseAntiteticPaths = true;

            int n_sim = 10000;
            int n_steps = 512;
            double a = 0.2;
            double DR = 0.02;
            double r0 = 0.015;
            double a1 = 1.0;
            double sigma1 = 0.01;
            double a2 = 0.1;
            double sigma2 = 0.0165;
            double correlation = 0.6;
            double maturityOpt = 5.0;
            double strike = 0.927;
            double tau = 2.0;

            ModelParameter PT = new ModelParameter(maturityOpt, "TT");
            PT.VarName = "TT";
            rov.Symbols.Add(PT);

            ModelParameter Ptau = new ModelParameter(tau, "tau");
            Ptau.VarName = "tau";
            rov.Symbols.Add(Ptau);

            ModelParameter Pa = new ModelParameter(a, "a");
            Pa.VarName = "a";
            rov.Symbols.Add(Pa);

            ModelParameter PDR = new ModelParameter(DR, "PDR");
            PDR.VarName = "DR";
            rov.Symbols.Add(PDR);

            ModelParameter Pr0 = new ModelParameter(r0, "r0");
            Pr0.VarName = "r0";
            rov.Symbols.Add(Pr0);

            ModelParameter Pstrike = new ModelParameter(strike, "strike");
            Pstrike.VarName = "strike";
            rov.Symbols.Add(Pstrike);

            AFunction zerorate = new AFunction(rov);
            zerorate.VarName = "zr";
            zerorate.m_IndependentVariables = 1;
            zerorate.m_Value = (RightValue)("(1-exp(-a*x1))*DR + r0");
            rov.Symbols.Add(zerorate);
            HW2ProcessType pt = new HW2ProcessType();

            // Set the short rate process.
            pt = HW2ProcessType.ShortRate;
            StocasticProcessHW2 process1 = new StocasticProcessHW2(rov, pt);

            process1.zero_rate_curve = "@zr";
            process1._a = (ModelParameter)a1;
            process1._b = (ModelParameter)sigma1;
            rov.Processes.AddProcess(process1);

            // Set the mean reversion process.
            pt = HW2ProcessType.Unobservable;
            StocasticProcessHW2 process2 = new StocasticProcessHW2(rov, pt);

            process2._a = (ModelParameter)a2;
            process2._b = (ModelParameter)sigma2;
            rov.Processes.AddProcess(process2);

            // Set the correlation.
            rov.Processes.r[0, 1] = (RightValue)correlation;

            // Set the discounting.
            RiskFreeInfo rfi = rov.GetDiscountingModel() as RiskFreeInfo;
            rfi.ActualizationType = EActualizationType.Stochastic;
            rfi.m_deterministicRF = (ModelParameter)"@v1";

            OptionTree op = new OptionTree(rov);
            op.PayoffInfo.PayoffExpression = "max(bond(TT;TT+tau;@v1)-strike;0)";
            op.PayoffInfo.Timing.EndingTime.m_Value = (RightValue)maturityOpt;
            op.PayoffInfo.European = true;
            rov.Map.Root = op;

            rov.NMethods.Technology = ETechType.T_SIMULATION;
            rov.NMethods.PathsNumber = n_sim;
            rov.NMethods.SimulationSteps = n_steps;

            ROVSolver solver = new ROVSolver();
            solver.BindToProject(rov);
            solver.DoValuation(-1);

            if (rov.HasErrors)
            {
                Console.WriteLine(rov.m_RuntimeErrorList[0]);
            }

            Assert.IsFalse(rov.HasErrors);

            ResultItem price = rov.m_ResultList[0] as ResultItem;
            double sampleMean = price.value;
            double sampleDevSt = price.stdDev / Math.Sqrt((double)n_sim);
            double theoreticalPrice = HW2BondCall(zerorate, maturityOpt, maturityOpt + tau, strike,
                                                  a1, sigma1, a2, sigma2, correlation);

            Console.WriteLine("\nTheoretical Price = " + theoreticalPrice.ToString());
            Console.WriteLine("Monte Carlo Price = " + sampleMean.ToString());
            Console.WriteLine("Standard Deviation = " + sampleDevSt.ToString());

            bool result;
            double fact = 4.0;
            result = (Math.Abs(sampleMean - theoreticalPrice) < fact * sampleDevSt);

            Assert.IsTrue(result);
        }