Changeset 139
- Timestamp:
- 04/29/08 11:20:58 (6 months ago)
- Files:
-
- trunk/QLNet/QLNet/Instruments/CapFloor.cs (modified) (2 diffs)
- trunk/QLNet/QLNet/Pricingengines/CapFloor/BlackCapFloorEngine.cs (modified) (3 diffs)
- trunk/QLNet/QLNet/Pricingengines/blackformula.cs (modified) (2 diffs)
- trunk/QLNet/QLNet/Termstructures/TermStructure.cs (modified) (1 diff)
- trunk/QLNet/Test2008/T_CapFloor.cs (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/QLNet/QLNet/Instruments/CapFloor.cs
r138 r139 75 75 throw new ArgumentException("no floor rates given"); 76 76 77 while (floorRates_.Count < flo orRates_.Count)77 while (floorRates_.Count < floatingLeg_.Count) 78 78 floorRates_.Add(floorRates_.Last()); 79 79 } … … 140 140 int n = floatingLeg_.Count; 141 141 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); 152 152 153 153 arguments.type = type_; trunk/QLNet/QLNet/Pricingengines/CapFloor/BlackCapFloorEngine.cs
r138 r139 47 47 } 48 48 49 public void calculate()49 public override void calculate() 50 50 { 51 51 double value = 0.0; 52 52 double vega = 0.0; 53 53 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); 57 57 CapFloorType type = arguments_.type; 58 58 Date today = volatility_.link.referenceDate(); … … 82 82 if (sqrtTime>0.0) 83 83 { 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; 86 86 } 87 87 // 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 } 91 91 if (type == CapFloorType.Floor || type == CapFloorType.Collar) 92 92 { … … 96 96 if (sqrtTime>0.0) 97 97 { 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; 100 100 } 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); 103 103 if (type == CapFloorType.Floor) 104 104 { trunk/QLNet/QLNet/Pricingengines/blackformula.cs
r107 r139 40 40 return blackFormula(optionType, strike, forward, stdDev, 1.0, 0.0); 41 41 } 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 } 42 46 public static double blackFormula(Option.Type optionType, double strike, double forward, double stdDev, 43 47 double discount, double displacement) { … … 73 77 return result; 74 78 } 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 } 75 123 } 76 124 } trunk/QLNet/QLNet/Termstructures/TermStructure.cs
r101 r139 107 107 108 108 //! date/time conversion 109 p rotecteddouble timeFromReference(Date d) { return dayCounter().yearFraction(referenceDate(), d); }109 public double timeFromReference(Date d) { return dayCounter().yearFraction(referenceDate(), d); } 110 110 111 111 //! date-range check trunk/QLNet/Test2008/T_CapFloor.cs
r138 r139 163 163 if (numericalVega>1.0e-4) 164 164 { 165 double analyticalVega = capFloor.result("vega");165 double analyticalVega = (double) capFloor.result("vega"); 166 166 double discrepancy = Math.Abs(numericalVega - analyticalVega); 167 167 discrepancy /= numericalVega;