Exemplo n.º 1
0
        public static void TestVasicekPricing()
        {
            double r0    = 0.01;
            double k     = 0.5;
            double theta = 0.1;
            double sigma = 0.1;

            VasicekModel model = new VasicekModel(r0, k, theta, sigma);

            double[] bondMaturities = new double[] { 2, 10 };
            Console.WriteLine("Bond prices");
            for (int i = 0; i < bondMaturities.Length; ++i)
            {
                double T = bondMaturities[i];
                double p = model.PriceZCB(T);
                Console.WriteLine("T={0}, p={1}", T, p);
            }

            double[] optionExerciseTimes = new double[] { 1, 2 };
            double[] optionStrikes       = new double[] { 0.9, 0.9 };

            Console.WriteLine("European call options on ZCBs, prices");
            for (int i = 0; i < bondMaturities.Length; ++i)
            {
                double T = bondMaturities[i], S = optionExerciseTimes[i], K = optionStrikes[i];
                double v = model.PriceZCBCall(S, K, T);
                Console.WriteLine("T={0}, S={1}, K={2}, v={3}", T, S, K, v);
            }
        }
Exemplo n.º 2
0
        public static void TestVasicekCalibration()
        {
            double r0    = 0.01;
            double k     = 0.5;
            double theta = 0.01;
            double sigma = 0.2;

            VasicekModel model = new VasicekModel(r0, k, theta, sigma);

            double[] bondMaturities      = new double[] { 1, 2, 10 };
            double[] optionExerciseTimes = new double[] { 0.5, 1, 2 };
            double[] optionStrikes       = new double[] { 0.9, 0.9, 0.9 };
            double[] prices = new double[bondMaturities.Length];
            //Console.WriteLine("European call options on ZCBs, prices");
            for (int i = 0; i < bondMaturities.Length; ++i)
            {
                double T = bondMaturities[i], S = optionExerciseTimes[i], K = optionStrikes[i];
                //prices[i] = model.PriceZCBCall(S, K, T);
                prices[i] = 0.05;
            }

            VasicekCalibrator calibrator = new VasicekCalibrator(r0, 1e-15, 2000);

            for (int i = 0; i < bondMaturities.Length; ++i)
            {
                calibrator.AddObservedOption(bondMaturities[i], optionExerciseTimes[i], optionStrikes[i], prices[i]);
            }
            calibrator.Calibrate();
            double             error   = 0;
            CalibrationOutcome outcome = CalibrationOutcome.NotStarted;

            calibrator.GetCalibrationStatus(ref outcome, ref error);
            Console.WriteLine("Calibration outcome: {0} and error: {1}", outcome, error);
        }
Exemplo n.º 3
0
 public override void Visit(VasicekModel model)
 { // Price a put or call using Jamshidian (1989)
     if (type == OptionType.Call)
     {
         price = CallPrice(model);   // implements Jamshidian 1989
     }
     else
     {
         price = PutPrice(model);    // implements Jamshidian 1989
     }
 }
Exemplo n.º 4
0
    private double PutPrice(VasicekModel model)
    {
        double nu = 0.5 * model.vol * model.vol * (1.0 - Math.Exp(-2.0 * model.kappa * (T - t))) / model.kappa;

        nu = Math.Sqrt(nu);

        double sigP = nu * (1.0 - Math.Exp(-model.kappa * (s - T))) / model.kappa;

        double d1 = (Math.Log(model.P(t, s) / (model.P(t, T) * K)) / sigP) + 0.5 * sigP;
        double d2 = d1 - sigP;

        /*  Console.WriteLine(" d1, d2 {0}, {1}", d1, d2);
         * Console.WriteLine("{0}, {1}", model.kappa, model.vol);
         * Console.WriteLine("{0}, {1}", nu, sigP);*/
        return(K * model.P(t, T) * SpecialFunctions.N(-d2) - model.P(t, s) * SpecialFunctions.N(-d1));
    }
Exemplo n.º 5
0
        public void TestVasicekModel()
        {
            var r     = 0.05;
            var speed = 0.15;
            var sig   = 0.01;
            var model = new VasicekModel(speed, r, sig);

            model.TimeToMaturity = 5;

            var rate     = model.SpotRate;
            var price    = model.PresentValue;
            var yieldSig = model.YieldVolatility;

            model.TimeToMaturity = 1;
            rate     = model.SpotRate;
            price    = model.PresentValue;
            yieldSig = model.YieldVolatility;
        }
Exemplo n.º 6
0
        public static void TestVasicekCalibration()
        {
            double r0    = 0.01;
            double k     = 0.5;
            double theta = 0.01;
            double sigma = 0.2;

            VasicekModel model = new VasicekModel(r0, k, theta, sigma);

            double[] bondMaturities      = new double[] { 1, 2, 10 };
            double[] optionExerciseTimes = new double[] { 0.5, 1, 2 };
            double[] optionStrikes       = new double[] { 0.9, 0.9, 0.9 };
            double[] prices = new double[bondMaturities.Length];
            //Console.WriteLine("European call options on ZCBs, prices");
            for (int i = 0; i < bondMaturities.Length; ++i)
            {
                double T = bondMaturities[i], S = optionExerciseTimes[i], K = optionStrikes[i];
                prices[i] = model.PriceZCBCall(S, K, T);
            }

            VasicekCalibrator calibrator = new VasicekCalibrator(r0, 1e-20, 2000);

            for (int i = 0; i < bondMaturities.Length; ++i)
            {
                // Here you should add the market data to the vasicekCalibrator...
                // .. for that you will need to modify the calibrator appropriately
                // e.g. add an AddObservedOption method to the VasicekCalibrator as follows
                //calibrator.AddObservedOption(bondMaturities[i], optionExerciseTimes[i], optionStrikes[i], prices[i]);
                throw new NotImplementedException("Method not implemented... that's part of the exercise.");
            }
            calibrator.Calibrate();
            double             error   = 0;
            CalibrationOutcome outcome = CalibrationOutcome.NotStarted;

            calibrator.GetCalibrationStatus(ref outcome, ref error);
            Console.WriteLine("Calibration outcome: {0} and error: {1}", outcome, error);
        }
    public static void Main()
    {
        Console.WriteLine("\nXML Serialiser..");

        // Create option data.
        OptionData opt = new OptionData();

        opt.ID   = "IBM CALL DEC";
        opt.r    = 0.08;
        opt.sig  = 0.30;
        opt.K    = 65.0;
        opt.T    = 0.25;
        opt.b    = opt.r; // Stock option
        opt.otyp = "C";

        // Create an XML serialiser.
        XmlSerializer xs = new XmlSerializer(typeof(OptionData));

        using (Stream s = File.Create("OptionXML.xml"))
        {
            xs.Serialize(s, opt);
        }

        // Recreate the object from the XML file.
        OptionData opt2;

        using (FileStream s = File.OpenRead("OptionXML.xml"))
        {
            opt2 = (OptionData)xs.Deserialize(s);
        }

        //    opt2.print();

        // We now do the same with XmlWriter and XmlReader
        XmlWriterSettings writeSettings = new XmlWriterSettings();

        writeSettings.Indent = true;

        using (XmlWriter wr = XmlWriter.Create("OptionXML2.xml", writeSettings))
        {
            wr.WriteStartElement("Option");

            // The recommended way to write XML data (e.g. call XmlConvert to peform
            // XML-compliant string conversions).
            wr.WriteStartElement("r"); wr.WriteValue(0.1); wr.WriteEndElement();

            wr.WriteStartElement("vol"); wr.WriteValue(0.2); wr.WriteEndElement();

            wr.WriteStartElement("K"); wr.WriteValue(100); wr.WriteEndElement();

            wr.WriteStartElement("T"); wr.WriteValue(0.25); wr.WriteEndElement();

            wr.WriteStartElement("coc"); wr.WriteValue(0.1); wr.WriteEndElement();

            wr.WriteStartElement("type"); wr.WriteValue("Call"); wr.WriteEndElement();

            wr.WriteEndElement();
        }

        XmlReaderSettings readSettings = new XmlReaderSettings();

        readSettings.IgnoreWhitespace = true;

        // Open XML file and print the values

        /*    using (XmlReader reader = XmlReader.Create("OptionXML2.xml", readSettings))
         *  while (reader.Read())
         *  {
         *      Console.Write(new string (' ', reader.Depth*2));   // Depth of current node
         *      Console.WriteLine(reader.NodeType);                // Type of current node
         *      Console.Write(reader.Name);                        // Qualified name of current node
         *      Console.WriteLine(reader.Value);                   // Value of current node
         *  }*/

        using (XmlReader reader = XmlReader.Create("OptionXML2.xml", readSettings))
        {
            reader.MoveToContent();             // Skip over XML declaration

            reader.ReadStartElement("Option");  // Start with data structure

            // Read the elements in the order as they were written
            string sa = reader.ReadElementContentAsString("r", ""); Console.WriteLine(sa);
            sa = reader.ReadElementContentAsString("vol", ""); Console.WriteLine(sa);
            sa = reader.ReadElementContentAsString("K", ""); Console.WriteLine(sa);
            sa = reader.ReadElementContentAsString("T", ""); Console.WriteLine(sa);
            sa = reader.ReadElementContentAsString("coc", ""); Console.WriteLine(sa);
            sa = reader.ReadElementContentAsString("type", ""); Console.WriteLine(sa);
        }


        // Start the process IE) that can process this XML file.
        // Then you view the output.
        // System.Diagnostics.Process.Start("OptionXML.xml");

        // Using serialisation with inheritance.
        // Parameters for Vasicek model
        double r     = 0.05;
        double kappa = 0.15;
        double vol   = 0.01;
        double theta = r;

        BondModel vasicek = new VasicekModel(kappa, theta, vol, r);

        XmlSerializer xs2 = new XmlSerializer(typeof(BondModel),
                                              new Type[] { typeof(VasicekModel), typeof(CIRModel) });

        using (Stream s = File.Create("Bond.xml"))
        {
            xs2.Serialize(s, vasicek);
        }

        // Serialise an aggregate object
        DateList myDL_1 = new DateList(new Date(2009, 8, 3), new Date(2011, 8, 3), 2, 1);
        //myDL_1.PrintVectDateList();

        // Create an XML serialiser for a cash flow date list.
        XmlSerializer xsDL = new XmlSerializer(typeof(DateList));

        using (Stream s = File.Create("DateList.xml"))
        {
            xsDL.Serialize(s, myDL_1);
        }

        System.Diagnostics.Process.Start("DateList.xml"); //AG added

        // Recreate the object from the XML file.
        DateList myDL_2;

        using (FileStream s = File.OpenRead("DateList.xml"))
        {
            myDL_2 = (DateList)xsDL.Deserialize(s);
        }

        // Using IXmlSerializer.
        OptionDataV optV = new OptionDataV();

        optV.r    = 0.08;
        optV.sig  = 0.30;
        optV.K    = 65.0;
        optV.T    = 0.25;
        optV.b    = opt.r; // Stock option
        optV.otyp = "C";


        // Create an XML serialiser.
        XmlSerializer xsV = new XmlSerializer(typeof(OptionDataV));

        using (Stream s = File.Create("OptionXMLV.xml"))
        {
            xsV.Serialize(s, optV);
        }

        // Recreate the object from the XML file.
        OptionDataV optV2;

        using (FileStream s = File.OpenRead("OptionXMLV.xml"))
        {
            optV2 = (OptionDataV)xsV.Deserialize(s);
        }

        optV2.print();

        System.Diagnostics.Process.Start("OptionXMLV.xml");
    }
Exemplo n.º 8
0
 // Visit Vasicek.
 public abstract void Visit(VasicekModel model);
    static void Main()
    {
        {
            // Parameters for Vasicek model
            double r     = 0.05;
            double kappa = 0.15;
            double vol   = 0.01;
            double theta = r;

            VasicekModel vasicek = new VasicekModel(kappa, theta, vol, r);

            Console.WriteLine(vasicek.P(0.0, 1.0));
            Console.WriteLine(vasicek.P(0.0, 5.0));
            Console.WriteLine(vasicek.R(0.0, 5.0));
        }
        {
            double r     = 0.05;
            double kappa = 0.15;
            double vol   = 0.1;
            double theta = r;


            OptionType type = OptionType.Call;
            // OptionType type = OptionType.Put;
            double   t   = 0.0;
            double   T   = 1.0;
            double   s   = 5.0;
            double   K   = 0.67;
            CIRModel cir = new CIRModel(kappa, theta, vol, r);

            Console.WriteLine("cir bond price {0}", cir.P(0.0, 2.0));
            //    Console.WriteLine(cir.R(0.0, 5.0));

            OptionPricer bv = new OptionPricer(t, T, s, K, type);

            bv.Visit(cir);
            Console.WriteLine("CIR option {0}", bv.price);
        }

        {
            //OptionType type = OptionType.Call;
            OptionType type = OptionType.Call;
            double     t    = 0.0;
            double     T    = 1.0;
            double     s    = 5.0;
            double     K    = 0.67;

            double r     = 0.05;
            double kappa = 0.15;
            double vol   = 0.1;
            double theta = r;

            VasicekModel vasicek = new VasicekModel(kappa, theta, vol, r);
            OptionPricer bv      = new OptionPricer(t, T, s, K, type);
            bv.Visit(vasicek);

            Console.WriteLine("Vasicek price {0}", bv.price);
        }

        {
            //OptionType type = OptionType.Call;
            OptionType type = OptionType.Put;
            double     t    = 0.0;
            double     T    = 1.0;
            double     s    = 5.0;
            double     K    = 0.67;

            OptionPricer bv = new OptionPricer(t, T, s, K, type);
            //     bv.Visit(vasicek);

            //   Console.WriteLine(bv.price);


            // Some Casting
            // Upcasting
            double r     = 0.05;
            double kappa = 0.15;
            double vol   = 0.1;
            double theta = r;

            BondModel bondModel = new VasicekModel(kappa, theta, vol, r);

            bondModel = new CIRModel(kappa, theta, vol, r);

            // Downcasting: correct and incorrect versions.

            CIRModel cirModel = (CIRModel)bondModel;    // OK

            // Cannot convert, hence we get a run-time error.
            //  VasicekModel vasicekModel = (VasicekModel)bondModel;    // OK

            // Two special operators.
            BondModel bondModel2 = new VasicekModel(kappa, theta, vol, r);
            CIRModel  cirModel2  = bondModel2 as CIRModel;
            if (cirModel2 == null)
            {
                Console.WriteLine("Conversion not successful");
            }

            // The 'is' operator
            if (bondModel2 is VasicekModel) // YES
            {
                Console.WriteLine("This is a Vasicek model");
            }
            else if (bondModel2 is CIRModel)
            {
                Console.WriteLine("This is a CIR model");
            }
            else
            {
                Console.WriteLine("Oops, not Vasicek and not CIR");
            }

            /*     // Use of 'var'
             *
             *   var v1 = 10;
             *   var v2 = "hello";
             *   var v3 = new DateTime();
             *
             *
             *   int v1A = 10;
             *   string v2B = "hello";
             *   DateTime v3C = new DateTime();
             *
             *   // Will not compile
             *   var x = 12;
             * //   x = "12";   // Compile-time error; x is an int
             *
             *
             *   var numbers = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };*/
        }
    }