Changeset 180
- Timestamp:
- 05/22/08 19:23:17 (5 months ago)
- Files:
-
- trunk/QLNet/QLNet.vsmdi (modified) (3 diffs)
- trunk/QLNet/QLNet/Cashflows/CashFlows.cs (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/QLNet/QLNet.vsmdi
r178 r180 12 12 <TestLink id="bbe2d937-bb70-a666-a879-b3063800a42e" name="testFairSpread" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" /> 13 13 <TestLink id="25b4118b-8066-f12f-3687-a5ef1886eb1c" name="testInArrears" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" /> 14 <TestLink id="0ee842b0-6275-252f-a1d0-000b0a6a4c52" name="testCachedValue" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" /> 14 15 <TestLink id="27ded393-6046-4057-bd65-31f629c9bf55" name="testFairRate" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" /> 15 <TestLink id="0ee842b0-6275-252f-a1d0-000b0a6a4c52" name="testCachedValue" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" />16 16 <TestLink id="3b1816a9-7785-f64f-bc52-7014af11e8e5" name="testSpreadDependency" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" /> 17 17 <TestLink id="2b04fe68-610a-b2de-e241-ef9a50fdb3fb" name="testRateDependency" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" /> … … 64 64 <TestList name="American Option" id="da9a808d-acf7-4baf-9dfb-e4b091d30fef" parentListId="8c43106b-9dc1-4907-a29f-aa66a61bf5b6"> 65 65 <TestLinks> 66 <TestLink id=" 33c4d801-48ac-44c4-329e-d6fe84f5e034" name="testJuValues" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" />66 <TestLink id="2bbf531b-68cb-84c0-a06f-6bedfb89f20f" name="testBaroneAdesiWhaleyValues" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" /> 67 67 <TestLink id="3be50179-04ee-902d-73d8-3c94e9305a67" name="testFdShoutGreeks" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" /> 68 68 <TestLink id="783f96db-cce8-bf61-b280-f5010f7d7eb3" name="testFdValues" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" /> 69 69 <TestLink id="1e42c90a-8e82-1369-9ed9-52b036b1c07e" name="testBjerksundStenslandValues" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" /> 70 70 <TestLink id="9f4ff6f8-9938-8ecb-5608-7dbc928cbda3" name="testFdAmericanGreeks" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" /> 71 <TestLink id=" 2bbf531b-68cb-84c0-a06f-6bedfb89f20f" name="testBaroneAdesiWhaleyValues" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" />71 <TestLink id="33c4d801-48ac-44c4-329e-d6fe84f5e034" name="testJuValues" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" /> 72 72 </TestLinks> 73 73 </TestList> 74 74 <TestList name="PiecewiseYieldCurve" id="db6f77ff-70a4-4779-821e-8f5548c91d54" parentListId="8c43106b-9dc1-4907-a29f-aa66a61bf5b6"> 75 75 <TestLinks> 76 <TestLink id=" dd6a79cf-cc1b-21c8-db38-fcf6f11d4e96" name="testObservability" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" />77 <TestLink id=" d70a4ef3-3967-0c12-9abc-8767e83dc8d2" name="testLogLinearZeroConsistency" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" />78 <TestLink id=" fe3bc37a-4955-31a4-c477-3e6856295cd6" name="testLinearForwardConsistency" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" />79 <TestLink id=" 7e4c5406-1597-4b55-b9fd-bbb29b4613d7" name="testLinearZeroConsistency" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" />80 <TestLink id=" 89ee0555-61db-dd08-e3fa-bbb6465b9f48" name="testLogLinearDiscountConsistency" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" />81 <TestLink id=" 37361527-9684-66e1-c987-17d393f416f0" name="testLogCubicDiscountConsistency" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" />82 <TestLink id=" 762cd2ac-5715-69e6-563e-4525bc437b84" name="testLinearDiscountConsistency" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" />76 <TestLink id="b61b2dd1-c391-ff6e-c17b-a16304449f43" name="testObservability" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" /> 77 <TestLink id="7b66dffe-f838-19e6-54b9-2d30645b0501" name="testLogLinearDiscountConsistency" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" /> 78 <TestLink id="d0a4985d-c3d0-7f0b-42cf-1f5a8b45bff4" name="testLogCubicDiscountConsistency" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" /> 79 <TestLink id="56925441-bb1d-00d2-1f1b-030dfeaa3048" name="testLinearDiscountConsistency" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" /> 80 <TestLink id="ef62ddb1-2d60-38aa-e6b5-1fa2091c548c" name="testLinearZeroConsistency" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" /> 81 <TestLink id="c1eb7a21-2e89-2c67-d40b-f6ad422def58" name="testLogLinearZeroConsistency" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" /> 82 <TestLink id="388385b3-7adb-345f-3889-6bb251cc8534" name="testLinearForwardConsistency" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" /> 83 83 </TestLinks> 84 84 </TestList> … … 86 86 <TestLinks> 87 87 <TestLink id="905e7e83-ac17-d164-7c00-332aee943ebf" name="testBrazilianCached" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" /> 88 <TestLink id=" af486b6a-ffe8-d458-89c3-9fcd3f6ec404" name="testYield" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" />88 <TestLink id="6dedd105-7532-140b-2b82-37bc69a8d72c" name="testCachedFixed" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" /> 89 89 <TestLink id="a7feffcc-db48-6ab4-df10-00a5494c44ea" name="testTheoretical" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" /> 90 90 <TestLink id="2e159c46-764f-ad21-7b0f-676f2bfdc323" name="testCachedFloating" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" /> 91 91 <TestLink id="ab3ac37e-d457-63de-2204-c9a25f55fec9" name="testCached" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" /> 92 <TestLink id="6dedd105-7532-140b-2b82-37bc69a8d72c" name="testCachedFixed" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" />93 92 <TestLink id="d7571d29-ddb9-5b86-6135-2acbcc05515d" name="testCachedZero" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" /> 93 <TestLink id="af486b6a-ffe8-d458-89c3-9fcd3f6ec404" name="testYield" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel, PublicKeyToken=b03f5f7f11d50a3a" /> 94 94 </TestLinks> 95 95 </TestList> trunk/QLNet/QLNet/Cashflows/CashFlows.cs
r109 r180 42 42 double result = 0.0; 43 43 44 foreach (CashFlow x in leg.FindAll(x => x.date() == paymentDate)) 45 { 46 // recheck 47 throw new NotImplementedException(); 48 Coupon cp = (Coupon)x; 49 if (firstCouponFound) 50 { 51 if (!(nominal == cp.nominal() && accrualPeriod == cp.accrualPeriod() && dc == cp.dayCounter())) 52 throw new ApplicationException("cannot aggregate two different coupons on " + paymentDate); 44 foreach (CashFlow x in leg.Where(x => x.date() == paymentDate)) 45 { 46 Coupon cp = x as Coupon; 47 if (cp != null) { 48 if (firstCouponFound) { 49 if (!(nominal == cp.nominal() && accrualPeriod == cp.accrualPeriod() && dc == cp.dayCounter())) 50 throw new ApplicationException("cannot aggregate two different coupons on " + paymentDate); 51 } else { 52 firstCouponFound = true; 53 nominal = cp.nominal(); 54 accrualPeriod = cp.accrualPeriod(); 55 dc = cp.dayCounter(); 56 } 57 result += cp.rate(); 53 58 } 54 else55 {56 firstCouponFound = true;57 nominal = cp.nominal();58 accrualPeriod = cp.accrualPeriod();59 dc = cp.dayCounter();60 }61 result += cp.rate();62 59 } 63 60 … … 70 67 double P = 0, dPdy = 0; 71 68 DayCounter dc = y.dayCounter(); 72 foreach (CashFlow cf in cashflows) 73 if (!cf.hasOccurred(settlementDate)) 74 { 75 double t = dc.yearFraction(settlementDate, cf.date()); 76 double c = cf.amount(); 77 double B = y.discountFactor(t); 78 P += c * B; 79 dPdy += t * c * B; 80 } 69 foreach (CashFlow cf in cashflows.Where(cf => !cf.hasOccurred(settlementDate))) { 70 double t = dc.yearFraction(settlementDate, cf.date()); 71 double c = cf.amount(); 72 double B = y.discountFactor(t); 73 P += c * B; 74 dPdy += t * c * B; 75 } 81 76 82 77 // no cashflows … … 93 88 DayCounter dc = y.dayCounter(); 94 89 95 foreach (CashFlow cf in cashflows) 96 { 97 if (!cf.hasOccurred(settlementDate)) 90 foreach (CashFlow cf in cashflows.Where(cf => !cf.hasOccurred(settlementDate))) 91 { 92 double t = dc.yearFraction(settlementDate, cf.date()); 93 double c = cf.amount(); 94 double B = y.discountFactor(t); 95 96 P += c * B; 97 switch (y.compounding()) 98 98 { 99 double t = dc.yearFraction(settlementDate, cf.date()); 100 double c = cf.amount(); 101 double B = y.discountFactor(t); 102 103 P += c * B; 104 switch (y.compounding()) 105 { 106 case Compounding.Simple: 99 case Compounding.Simple: 100 dPdy -= c * B * B * t; 101 break; 102 case Compounding.Compounded: 103 dPdy -= c * t * B / (1 + r / N); 104 break; 105 case Compounding.Continuous: 106 dPdy -= c * B * t; 107 break; 108 case Compounding.SimpleThenCompounded: 109 if (t <= 1.0 / N) 107 110 dPdy -= c * B * B * t; 108 break; 109 case Compounding.Compounded: 111 else 110 112 dPdy -= c * t * B / (1 + r / N); 111 break; 112 case Compounding.Continuous: 113 dPdy -= c * B * t; 114 break; 115 case Compounding.SimpleThenCompounded: 116 if (t <= 1.0 / N) 117 dPdy -= c * B * B * t; 118 else 119 dPdy -= c * t * B / (1 + r / N); 120 break; 121 default: 122 throw new ArgumentException("unknown compounding convention (" + y.compounding() + ")"); 123 } 113 break; 114 default: 115 throw new ArgumentException("unknown compounding convention (" + y.compounding() + ")"); 124 116 } 125 117 } … … 212 204 213 205 public static CashFlow previousCashFlow(List<CashFlow> leg) { return previousCashFlow(leg, null); } 214 public static CashFlow previousCashFlow(List<CashFlow> leg, Date refDate) 215 { 206 public static CashFlow previousCashFlow(List<CashFlow> leg, Date refDate) { 216 207 Date d = (refDate == null ? Settings.evaluationDate() : refDate); 217 208 … … 222 213 223 214 public static CashFlow nextCashFlow(List<CashFlow> leg) { return nextCashFlow(leg, null); } 224 public static CashFlow nextCashFlow(List<CashFlow> leg, Date refDate) 225 { 215 public static CashFlow nextCashFlow(List<CashFlow> leg, Date refDate) { 226 216 Date d = (refDate == null ? Settings.evaluationDate() : refDate); 227 217 … … 246 236 } 247 237 248 public static Date startDate(List<CashFlow> cashflows) 249 { 250 Date d = Date.maxDate(); 251 foreach (CashFlow cf in cashflows) 252 { 253 if (cf.GetType().IsSubclassOf(typeof(Coupon))) 254 d = Date.Min(d, ((Coupon)cf).accrualStartDate()); 255 } 256 if (d == Date.maxDate()) throw new ArgumentException("not enough information available"); 257 return d; 258 } 259 260 public static Date maturityDate(List<CashFlow> cashflows) 261 { 262 Date d = Date.minDate(); 263 foreach (CashFlow c in cashflows) 264 d = Date.Max(d, c.date()); 265 if (d == Date.minDate()) throw new ArgumentException("no cashflows"); 266 return d; 238 public static Date startDate(List<CashFlow> cashflows) { 239 if (cashflows.Count == 0) throw new ArgumentException("no cashflows"); 240 return cashflows.Where(cf => cf is Coupon).Min(cf => ((Coupon)cf).accrualStartDate()); 241 } 242 243 public static Date maturityDate(List<CashFlow> cashflows) { 244 if (cashflows.Count == 0) throw new ArgumentException("no cashflows"); 245 return cashflows.Max(c => c.date()); 267 246 } 268 247 … … 276 255 return npv(cashflows, discountCurve, settlementDate, npvDate, 0); 277 256 } 278 public static double npv(List<CashFlow> cashflows, YieldTermStructure discountCurve, Date settlementDate, Date npvDate, int exDividendDays)279 {257 public static double npv(List<CashFlow> cashflows, YieldTermStructure discountCurve, Date settlementDate, 258 Date npvDate, int exDividendDays) { 280 259 if (settlementDate == null) 281 260 settlementDate = discountCurve.referenceDate(); 282 261 283 double totalNPV = 0; 284 foreach (CashFlow c in cashflows.FindAll(x => !x.hasOccurred(settlementDate + exDividendDays))) 285 { 286 double a = c.amount(); 287 double b = discountCurve.discount(c.date()); 288 totalNPV += a * b; 289 } 262 double totalNPV = cashflows.Where(x => !x.hasOccurred(settlementDate + exDividendDays)). 263 Sum(c => c.amount() * discountCurve.discount(c.date())); 290 264 291 265 if (npvDate == null) return totalNPV; … … 317 291 } 318 292 public static double bps(List<CashFlow> cashflows, YieldTermStructure discountCurve, 319 Date settlementDate, Date npvDate, int exDividendDays) 320 { 293 Date settlementDate, Date npvDate, int exDividendDays) { 321 294 if (settlementDate == null) 322 295 settlementDate = discountCurve.referenceDate(); 323 296 324 297 BPSCalculator calc = new BPSCalculator(discountCurve, npvDate); 325 foreach (CashFlow cf in cashflows) 326 { 327 if (!cf.hasOccurred(settlementDate + exDividendDays)) 328 { 329 cf.accept(calc); 330 } 331 } 298 for(int i = 0; i<cashflows.Count; i++) 299 if (!cashflows[i].hasOccurred(settlementDate + exDividendDays)) 300 cashflows[i].accept(calc); 332 301 return basisPoint_ * calc.result(); 333 302 } … … 374 343 the theoretical existance of an IRR and numerically establishes the IRR to the desired precision. */ 375 344 public static double irr(List<CashFlow> cashflows, double marketPrice, DayCounter dayCounter, Compounding compounding, 376 Frequency frequency, Date settlementDate, double accuracy, int maxIterations, double guess) 377 { 378 345 Frequency frequency, Date settlementDate, double accuracy, int maxIterations, double guess) { 379 346 if (settlementDate == null) 380 347 settlementDate = Settings.evaluationDate(); … … 386 353 signChanges = 0; 387 354 388 foreach (CashFlow cf in cashflows) 389 { 390 if (!cf.hasOccurred(settlementDate)) 391 { 392 int thisSign = Math.Sign(cf.amount()); 393 if (lastSign * thisSign < 0) // sign change 394 signChanges++; 395 396 if (thisSign != 0) 397 lastSign = thisSign; 398 } 355 foreach (CashFlow cf in cashflows.Where(cf => !cf.hasOccurred(settlementDate))) 356 { 357 int thisSign = Math.Sign(cf.amount()); 358 if (lastSign * thisSign < 0) // sign change 359 signChanges++; 360 361 if (thisSign != 0) 362 lastSign = thisSign; 399 363 } 400 364 if (!(signChanges > 0)) … … 474 438 */ 475 439 public static double convexity(List<CashFlow> cashflows, InterestRate rate) { return convexity(cashflows, rate, null); } 476 public static double convexity(List<CashFlow> cashflows, InterestRate rate, Date settlementDate) 477 { 440 public static double convexity(List<CashFlow> cashflows, InterestRate rate, Date settlementDate) { 478 441 if (settlementDate == null) settlementDate = Settings.evaluationDate(); 479 442 … … 485 448 int N = (int)rate.frequency(); 486 449 487 foreach (CashFlow cashflow in cashflows) 488 { 489 if (!cashflow.hasOccurred(settlementDate)) 450 foreach (CashFlow cashflow in cashflows.Where(cashflow => !cashflow.hasOccurred(settlementDate))) 451 { 452 double t = dayCounter.yearFraction(settlementDate, cashflow.date()); 453 double c = cashflow.amount(); 454 double B = rate.discountFactor(t); 455 456 P += c * B; 457 switch (rate.compounding()) 490 458 { 491 double t = dayCounter.yearFraction(settlementDate, cashflow.date()); 492 double c = cashflow.amount(); 493 double B = rate.discountFactor(t); 494 495 P += c * B; 496 switch (rate.compounding()) 497 { 498 case Compounding.Simple: 459 case Compounding.Simple: 460 d2Pdy2 += c * 2.0 * B * B * B * t * t; 461 break; 462 case Compounding.Compounded: 463 d2Pdy2 += c * B * t * (N * t + 1) / (N * (1 + y / N) * (1 + y / N)); 464 break; 465 case Compounding.Continuous: 466 d2Pdy2 += c * B * t * t; 467 break; 468 case Compounding.SimpleThenCompounded: 469 if (t <= 1.0 / N) 499 470 d2Pdy2 += c * 2.0 * B * B * B * t * t; 500 break; 501 case Compounding.Compounded: 471 else 502 472 d2Pdy2 += c * B * t * (N * t + 1) / (N * (1 + y / N) * (1 + y / N)); 503 break; 504 case Compounding.Continuous: 505 d2Pdy2 += c * B * t * t; 506 break; 507 case Compounding.SimpleThenCompounded: 508 if (t <= 1.0 / N) 509 d2Pdy2 += c * 2.0 * B * B * B * t * t; 510 else 511 d2Pdy2 += c * B * t * (N * t + 1) / (N * (1 + y / N) * (1 + y / N)); 512 break; 513 default: 514 throw new ArgumentException("unknown compounding convention (" + rate.compounding() + ")"); 515 } 473 break; 474 default: 475 throw new ArgumentException("unknown compounding convention (" + rate.compounding() + ")"); 516 476 } 517 477 }