Assembla home | Assembla project page
 

Changeset 139

Show
Ignore:
Timestamp:
04/29/08 11:20:58 (6 months ago)
Author:
Vice
Message:

Cap & Floor first test done.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/QLNet/QLNet/Instruments/CapFloor.cs

    r138 r139  
    7575               throw new ArgumentException("no floor rates given"); 
    7676 
    77             while (floorRates_.Count < floorRates_.Count) 
     77            while (floorRates_.Count < floatingLeg_.Count) 
    7878               floorRates_.Add(floorRates_.Last()); 
    7979         } 
     
    140140         int n = floatingLeg_.Count; 
    141141 
    142          arguments.startDates.Capacity = n
    143          arguments.fixingDates.Capacity = n
    144          arguments.endDates.Capacity = n
    145          arguments.accrualTimes.Capacity = n
    146          arguments.forwards.Capacity = n
    147          arguments.nominals.Capacity = n
    148          arguments.gearings.Capacity = n
    149          arguments.capRates.Capacity = n
    150          arguments.floorRates.Capacity = n
    151          arguments.spreads.Capacity = n
     142         arguments.startDates = new InitializedList<Date>(n)
     143         arguments.fixingDates = new InitializedList<Date>(n)
     144         arguments.endDates = new InitializedList<Date>(n)
     145         arguments.accrualTimes = new InitializedList<double>(n)
     146         arguments.forwards = new InitializedList<double?>(n)
     147         arguments.nominals = new InitializedList<double>(n)
     148         arguments.gearings = new InitializedList<double>(n)
     149         arguments.capRates = new InitializedList<double?>(n)
     150         arguments.floorRates = new InitializedList<double?>(n)
     151         arguments.spreads = new InitializedList<double>(n)
    152152 
    153153         arguments.type = type_; 
  • trunk/QLNet/QLNet/Pricingengines/CapFloor/BlackCapFloorEngine.cs

    r138 r139  
    4747      } 
    4848 
    49       public void calculate()  
     49      public override void calculate()  
    5050      { 
    5151         double value = 0.0; 
    5252         double vega = 0.0; 
    5353         int optionlets = arguments_.startDates.Count; 
    54          List<double> values = new List<double>(optionlets); 
    55          List<double> vegas = new List<double>(optionlets); 
    56          List<double> stdDevs = new List<double>(optionlets); 
     54         List<double> values = new InitializedList<double>(optionlets); 
     55         List<double> vegas = new InitializedList<double>(optionlets); 
     56         List<double> stdDevs = new InitializedList<double>(optionlets); 
    5757         CapFloorType type = arguments_.type; 
    5858         Date today = volatility_.link.referenceDate(); 
     
    8282                  if (sqrtTime>0.0)  
    8383                  { 
    84                      stdDevs[i] = Math.Sqrt(volatility_.link.blackVariance(fixingDate, strike)); 
    85                      vegas[i] = blackFormulaStdDevDerivative(strike, forward, stdDevs[i], d) * sqrtTime; 
     84                     stdDevs[i] = Math.Sqrt(volatility_.link.blackVariance(fixingDate, strike.Value)); 
     85                     vegas[i] = Utils.blackFormulaStdDevDerivative(strike.Value, forward.Value, stdDevs[i], d) * sqrtTime; 
    8686                  } 
    8787                  // include caplets with past fixing date 
    88                   values[i] = blackFormula(Option.Type.Call, strike, 
    89                                              forward, stdDevs[i], d); 
    90                
     88                  values[i] = Utils.blackFormula(Option.Type.Call, strike.Value, 
     89                                             forward.Value, stdDevs[i], d); 
     90               
    9191                if (type == CapFloorType.Floor || type == CapFloorType.Collar)  
    9292                { 
     
    9696                  if (sqrtTime>0.0)  
    9797                  { 
    98                      stdDevs[i] = Math.Sqrt(volatility_.blackVariance(fixingDate, strike)); 
    99                      floorletVega = blackFormulaStdDevDerivative(strike, forward, stdDevs[i], d) * sqrtTime; 
     98                     stdDevs[i] = Math.Sqrt(volatility_.link.blackVariance(fixingDate, strike.Value)); 
     99                     floorletVega = Utils.blackFormulaStdDevDerivative(strike.Value, forward.Value, stdDevs[i], d) * sqrtTime; 
    100100                  } 
    101                   double floorlet = blackFormula(Option.Type.Put, strike, 
    102                                                  forward, stdDevs[i], d); 
     101                  double floorlet = Utils.blackFormula(Option.Type.Put, strike.Value, 
     102                                                 forward.Value, stdDevs[i], d); 
    103103                  if (type == CapFloorType.Floor)  
    104104                  { 
  • trunk/QLNet/QLNet/Pricingengines/blackformula.cs

    r107 r139  
    4040            return blackFormula(optionType, strike, forward, stdDev, 1.0, 0.0); 
    4141        } 
     42        public static double blackFormula(Option.Type optionType, double strike, double forward, double stdDev, double discount) 
     43        { 
     44           return blackFormula(optionType, strike, forward, stdDev, discount, 0.0); 
     45        } 
    4246        public static double blackFormula(Option.Type optionType, double strike, double forward, double stdDev, 
    4347                                          double discount, double displacement) { 
     
    7377            return result; 
    7478        } 
     79 
     80       /// <summary> 
     81       /// Black 1976 formula for standard deviation derivative 
     82       /// \warning instead of volatility it uses standard deviation, i.e. 
     83       /// volatility*sqrt(timeToMaturity), and it returns the 
     84       /// derivative with respect to the standard deviation. 
     85       /// If T is the time to maturity Black vega would be 
     86       /// blackStdDevDerivative(strike, forward, stdDev)*sqrt(T) 
     87       /// </summary> 
     88       public static double blackFormulaStdDevDerivative(double strike,double forward,double stdDev) 
     89       { return blackFormulaStdDevDerivative(strike, forward, stdDev, 1.0, 0.0);} 
     90       public static double blackFormulaStdDevDerivative(double strike, double forward, double stdDev,double discount) 
     91       { return blackFormulaStdDevDerivative(strike, forward, stdDev, discount, 0.0); } 
     92 
     93 
     94        public static double blackFormulaStdDevDerivative(double strike, 
     95                                            double forward, 
     96                                            double stdDev, 
     97                                            double discount, 
     98                                            double displacement) 
     99        { 
     100            
     101           checkParameters(strike, forward, displacement); 
     102 
     103           if (stdDev<0.0) 
     104              throw new ArgumentException("stdDev (" + stdDev + ") must be non-negative"); 
     105 
     106           if (discount<=0.0)  
     107              throw new ArgumentException("discount (" + discount + ") must be positive"); 
     108 
     109           forward = forward + displacement; 
     110           strike = strike + displacement; 
     111 
     112           if (stdDev==0.0) 
     113              if (forward>strike) 
     114                 return discount * forward; 
     115              else 
     116                 return 0.0; 
     117 
     118           double d1 = Math.Log(forward/strike)/stdDev + .5*stdDev; 
     119           CumulativeNormalDistribution phi = new CumulativeNormalDistribution(); 
     120           return discount * forward * phi.derivative(d1); 
     121 
     122        } 
    75123    } 
    76124} 
  • trunk/QLNet/QLNet/Termstructures/TermStructure.cs

    r101 r139  
    107107 
    108108        //! date/time conversion 
    109         protected double timeFromReference(Date d) { return dayCounter().yearFraction(referenceDate(), d); } 
     109        public double timeFromReference(Date d) { return dayCounter().yearFraction(referenceDate(), d); } 
    110110 
    111111        //! date-range check 
  • trunk/QLNet/Test2008/T_CapFloor.cs

    r138 r139  
    163163                     if (numericalVega>1.0e-4)  
    164164                     { 
    165                               double analyticalVega = capFloor.result("vega"); 
     165                              double analyticalVega = (double) capFloor.result("vega"); 
    166166                              double discrepancy = Math.Abs(numericalVega - analyticalVega); 
    167167                              discrepancy /= numericalVega;