Assembla home | Assembla project page
 

Changeset 180

Show
Ignore:
Timestamp:
05/22/08 19:23:17 (5 months ago)
Author:
snovik
Message:

Change: Some .NET specific optimisations for CashFlows?.cs

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/QLNet/QLNet.vsmdi

    r178 r180  
    1212      <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" /> 
    1313      <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" /> 
    1415      <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" /> 
    1616      <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" /> 
    1717      <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" /> 
     
    6464  <TestList name="American Option" id="da9a808d-acf7-4baf-9dfb-e4b091d30fef" parentListId="8c43106b-9dc1-4907-a29f-aa66a61bf5b6"> 
    6565    <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" /> 
    6767      <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" /> 
    6868      <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" /> 
    6969      <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" /> 
    7070      <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" /> 
    7272    </TestLinks> 
    7373  </TestList> 
    7474  <TestList name="PiecewiseYieldCurve" id="db6f77ff-70a4-4779-821e-8f5548c91d54" parentListId="8c43106b-9dc1-4907-a29f-aa66a61bf5b6"> 
    7575    <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" /> 
    8383    </TestLinks> 
    8484  </TestList> 
     
    8686    <TestLinks> 
    8787      <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" /> 
    8989      <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" /> 
    9090      <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" /> 
    9191      <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" /> 
    9392      <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" /> 
    9494    </TestLinks> 
    9595  </TestList> 
  • trunk/QLNet/QLNet/Cashflows/CashFlows.cs

    r109 r180  
    4242            double result = 0.0; 
    4343 
    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(); 
    5358                } 
    54                 else 
    55                 { 
    56                     firstCouponFound = true; 
    57                     nominal = cp.nominal(); 
    58                     accrualPeriod = cp.accrualPeriod(); 
    59                     dc = cp.dayCounter(); 
    60                 } 
    61                 result += cp.rate(); 
    6259            } 
    6360 
     
    7067            double P = 0, dPdy = 0; 
    7168            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            } 
    8176 
    8277            // no cashflows 
     
    9388            DayCounter dc = y.dayCounter(); 
    9489 
    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()) 
    9898                { 
    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) 
    107110                            dPdy -= c * B * B * t; 
    108                             break; 
    109                         case Compounding.Compounded: 
     111                        else 
    110112                            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() + ")"); 
    124116                } 
    125117            } 
     
    212204 
    213205        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) { 
    216207            Date d = (refDate == null ? Settings.evaluationDate() : refDate); 
    217208 
     
    222213 
    223214        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) { 
    226216            Date d = (refDate == null ? Settings.evaluationDate() : refDate); 
    227217 
     
    246236        } 
    247237 
    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()); 
    267246        } 
    268247 
     
    276255            return npv(cashflows, discountCurve, settlementDate, npvDate, 0); 
    277256        } 
    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)
    280259            if (settlementDate == null) 
    281260                settlementDate = discountCurve.referenceDate(); 
    282261 
    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())); 
    290264 
    291265            if (npvDate == null) return totalNPV; 
     
    317291        } 
    318292        public static double bps(List<CashFlow> cashflows, YieldTermStructure discountCurve, 
    319                                   Date settlementDate, Date npvDate, int exDividendDays) 
    320         { 
     293                                 Date settlementDate, Date npvDate, int exDividendDays) { 
    321294            if (settlementDate == null) 
    322295                settlementDate = discountCurve.referenceDate(); 
    323296 
    324297            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); 
    332301            return basisPoint_ * calc.result(); 
    333302        } 
     
    374343            the theoretical existance of an IRR and numerically establishes the IRR to the desired precision. */ 
    375344        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) { 
    379346            if (settlementDate == null) 
    380347                settlementDate = Settings.evaluationDate(); 
     
    386353                signChanges = 0; 
    387354 
    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; 
    399363            } 
    400364            if (!(signChanges > 0)) 
     
    474438                */ 
    475439        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) { 
    478441            if (settlementDate == null) settlementDate = Settings.evaluationDate(); 
    479442 
     
    485448            int N = (int)rate.frequency(); 
    486449 
    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()) 
    490458                { 
    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) 
    499470                            d2Pdy2 += c * 2.0 * B * B * B * t * t; 
    500                             break; 
    501                         case Compounding.Compounded: 
     471                        else 
    502472                            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() + ")"); 
    516476                } 
    517477            }