Пример #1
0
 public void GetFirstNodeInStep()
 {
     Assert.AreEqual(4, BinTree.GetIndexFirstNodeInStep(3));
     Assert.AreEqual(2, BinTree.GetIndexFirstNodeInStep(2));
 }
Пример #2
0
        /// <summary>
        /// Datos de las betas segun el percentil del valor del arbol (beta en 10% sup, 20% sup, ...)
        /// </summary>
        private static void BetaGraph1()
        {
            int    numberOfSteps     = 2000;
            double InitialStockPrice = 100;
            double Volatility        = 0.2;
            double StrikePrice       = 105;
            double TotalTimeYears    = 0.5;
            double RiskFree          = 0.03;
            double MarketRiskPremium = 0.06;
            double StockBeta         = 1;

            var tree = new CapmBinTree(numberOfSteps);

            tree.InitialStockPrice = InitialStockPrice;
            tree.Volatility        = Volatility;
            tree.StrikePrice       = StrikePrice;
            tree.TotalTimeYears    = TotalTimeYears;
            tree.RiskFree          = RiskFree;
            tree.MarketRiskPremium = MarketRiskPremium;
            tree.StockBeta         = StockBeta;

            Console.WriteLine("Calculating stock prices...");
            tree.CalculateStockPrices();

            Console.WriteLine("Calculating option prices...");
            tree.CalculateOptionPrices();

            Console.WriteLine("Outputing the betas...");

            int divisions = 70;

            using (var writer = new StreamWriter("beta_graph_1.csv", false))
            {
                writer.Write("Step");
                for (int i = 1; i <= divisions; i++)
                {
                    writer.Write(",n" + i);
                }
                writer.WriteLine();


                var indexes = new List <int>(divisions);
                for (int step = 1; step < numberOfSteps; step++)
                {
                    int nodeCount = BinTree.GetIndexLastNodeInStep(step) - BinTree.GetIndexFirstNodeInStep(step);
                    indexes.Clear();
                    for (int stp = 0; stp < divisions; stp++)
                    {
                        indexes.Add(nodeCount * stp / divisions);
                    }

                    writer.Write(step + ",");

                    int i = 0;
                    foreach (var node in tree.IterateNodesOfStep(step))
                    {
                        if (indexes.Contains(i))
                        {
                            if (node.OptionBeta <= 0)
                            {
                                break;
                            }
                            writer.Write(node.OptionBeta + ",");
                        }
                        i++;
                    }
                    writer.WriteLine();
                }
            }

            Console.WriteLine("Done. See: beta_graph_1.csv");
        }