Assembla home | Assembla project page
 

Changeset 202

Show
Ignore:
Timestamp:
06/05/08 19:08:46 (6 months ago)
Author:
snovik
Message:

New: Interpolation tests
New: More interpolation methods
New: LevenbergMarquardt? optimisation (in progress)
New: QL 15018 (work in progress)
Changed: Some code-cleaning on bootstrapping

Files:

Legend:

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

    r199 r202  
    2828      <TestLink id="3d6c3b62-4724-675b-23d4-47989e70e293" name="testLRBinomialEngines" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
    2929      <TestLink id="c634bf0e-7e2c-5908-d535-0d29df99b963" name="testEQPBinomialEngines" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
     30    </TestLinks> 
     31  </TestList> 
     32  <TestList name="Interpolations" id="6c850207-24f9-4759-a03b-f6bd1598cae7" parentListId="8c43106b-9dc1-4907-a29f-aa66a61bf5b6"> 
     33    <TestLinks> 
     34      <TestLink id="013d24e4-afd3-db12-eb66-7e64ca4f1139" name="testSplineOnRPN15AValues" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
     35      <TestLink id="1ef1148e-88a3-f814-99cb-a7af6bd40cac" name="testForwardFlat" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
     36      <TestLink id="ff5e5f35-412d-4921-c02d-078965669fbb" name="testAsFunctor" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
     37      <TestLink id="27b002f6-e817-5440-bdb6-415bc2a07d84" name="testBackwardFlat" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
     38      <TestLink id="5e0a8cf6-5ecb-3988-77cb-1408470c0c79" name="testSimmetricEndConditions" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
     39      <TestLink id="5d82722c-95c4-0f3e-007e-8fe11b6247ab" name="testNonRestrictiveHymanFilter" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
     40      <TestLink id="81819301-dea7-5865-26c2-d526c9ea96e2" name="testSplineErrorOnGaussianValues" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
     41      <TestLink id="4d4733e1-3186-bfe0-5834-d791f70989d0" name="testDerivativeEndConditions" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
     42      <TestLink id="7580c3ac-9665-58cb-9817-6d2a78af1641" name="testSplineOnGaussianValues" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
     43      <TestLink id="570f8ca9-82ab-3192-9b06-be2672a3ae3d" name="testSplineOnGenericValues" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
    3044    </TestLinks> 
    3145  </TestList> 
     
    7488  <TestList name="PiecewiseYieldCurve" id="db6f77ff-70a4-4779-821e-8f5548c91d54" parentListId="8c43106b-9dc1-4907-a29f-aa66a61bf5b6"> 
    7589    <TestLinks> 
     90      <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" /> 
     91      <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" /> 
     92      <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" /> 
     93      <TestLink id="561fa88b-37bb-98f4-4628-a77e4d9a9cd6" name="testFlatForwardConsistency" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
     94      <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" /> 
     95      <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" /> 
     96      <TestLink id="bad5af99-6228-d0eb-b51f-d1568cb399b5" name="testConvexMonotoneForwardConsistency" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
     97      <TestLink id="05e8bb09-b9c7-65f3-dbc2-8539b17dd117" name="testSplineZeroConsistency" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
     98      <TestLink id="4bcf3c17-3471-0971-a4dd-d7a19bd23019" name="testSplineForwardConsistency" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
     99      <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" /> 
     100      <TestLink id="af05efc7-7a38-bc81-5740-41720115b250" name="testLocalBootstrapConsistency" storage="test2008\bin\debug\test2008.dll" type="Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestElement, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel,   PublicKeyToken=b03f5f7f11d50a3a" /> 
    76101      <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" /> 
    83102    </TestLinks> 
    84103  </TestList> 
  • trunk/QLNet/QLNet/Math/Interpolation.cs

    r119 r202  
    3434    } 
    3535 
    36     public abstract class Interpolation : Extrapolator
     36    public abstract class Interpolation : Extrapolator, IValue
    3737        protected Impl impl_; 
    3838 
     
    5454        public double secondDerivative(double x, bool allowExtrapolation) { 
    5555            checkRange(x,allowExtrapolation); 
    56             return impl_.derivative(x); 
     56            return impl_.secondDerivative(x); 
    5757        } 
    5858 
     
    8585 
    8686        // abstract base class interface for interpolation implementations 
    87         protected interface Impl
     87        protected interface Impl : IValue
    8888            void update(); 
    8989            double xMin(); 
  • trunk/QLNet/QLNet/Math/Interpolations/CubicInterpolation.cs

    r93 r202  
    6363        \test to be adapted from old ones. 
    6464    */ 
    65     public class CubicInterpolation : Interpolation
     65    public class CubicInterpolation : Interpolation, IValue
    6666        #region enums 
    6767        public enum DerivativeApprox { 
     
    128128 
    129129        //public List<double> primitiveConstants() { return coeffs_.primitiveConst_; } 
    130         //public List<double> aCoefficients() { return coeffs_.a_; } 
    131         //public List<double> bCoefficients() { return coeffs_.b_; } 
    132         //public List<double> cCoefficients() { return coeffs_.c_; } 
     130        public List<double> aCoefficients() { return ((CubicInterpolationImpl)impl_).a_; } 
     131        public List<double> bCoefficients() { return ((CubicInterpolationImpl)impl_).b_; } 
     132        public List<double> cCoefficients() { return ((CubicInterpolationImpl)impl_).c_; } 
    133133        //public List<bool> monotonicityAdjustments() { return coeffs_.monotonicityAdjustments_; } 
    134134    } 
     
    173173        //           b[i]*(x-x[i])^2 + 
    174174        //           c[i]*(x-x[i])^3 
    175         InitializedList<double> primitiveConst_, a_, b_, c_; 
     175        public InitializedList<double> primitiveConst_, a_, b_, c_; 
    176176        InitializedList<bool> monotonicityAdjustments_; 
    177177 
  • trunk/QLNet/QLNet/QLNet.csproj

    r198 r202  
    144144    <Compile Include="Math\integrals\Kronrodintegral.cs" /> 
    145145    <Compile Include="Math\integrals\Segmentintegral.cs" /> 
     146    <Compile Include="Math\integrals\simpsonintegral.cs" /> 
     147    <Compile Include="Math\integrals\trapezoidintegral.cs" /> 
     148    <Compile Include="Math\Interpolations\backwardflatinterpolation.cs" /> 
     149    <Compile Include="Math\Interpolations\convexmonotoneinterpolation.cs" /> 
    146150    <Compile Include="Math\Interpolations\CubicInterpolation.cs" /> 
    147151    <Compile Include="Math\Interpolations\Extrapolator.cs" /> 
    148152    <Compile Include="Math\Interpolation.cs" /> 
     153    <Compile Include="Math\Interpolations\forwardflatinterpolation.cs" /> 
    149154    <Compile Include="Math\Interpolations\Linearinterpolation.cs" /> 
    150155    <Compile Include="Math\Interpolations\Loginterpolation.cs" /> 
     156    <Compile Include="Math\Interpolations\multicubicspline.cs" /> 
     157    <Compile Include="Math\Interpolations\sabrinterpolation.cs" /> 
    151158    <Compile Include="Math\Matrix.cs" /> 
    152159    <Compile Include="Math\Optimization\ArmijoLineSearch.cs" /> 
     
    155162    <Compile Include="Math\Optimization\EndCriteria.cs" /> 
    156163    <Compile Include="Math\Optimization\LeastSquareProblem.cs" /> 
     164    <Compile Include="Math\Optimization\levenbergmarquardt.cs" /> 
    157165    <Compile Include="Math\Optimization\LineSearch.cs" /> 
    158166    <Compile Include="Math\Optimization\LineSearchBasedMethod.cs" /> 
     
    290298    <Compile Include="Termstructures\InflationTermStructure.cs" /> 
    291299    <Compile Include="Termstructures\Iterativebootstrap.cs" /> 
     300    <Compile Include="Termstructures\localbootstrap.cs" /> 
    292301    <Compile Include="Termstructures\TermStructure.cs" /> 
    293302    <Compile Include="Termstructures\VolatilityTermStructure.cs" /> 
  • trunk/QLNet/QLNet/Termstructures/Bootstraperror.cs

    r63 r202  
    2424namespace QLNet { 
    2525    //! bootstrap error 
    26     public class BootstrapError<Traits, Interpolator> : ISolver1d 
    27             where Traits : ITraits, new() 
    28             where Interpolator : IInterpolationFactory, new() { 
    29         private PiecewiseYieldCurve<Traits, Interpolator> curve_; 
     26    public class BootstrapError<T, I, B> : ISolver1d 
     27            where T : ITraits, new() 
     28            where I : IInterpolationFactory, new() 
     29            where B : IBootStrap, new() { 
     30 
     31        private PiecewiseYieldCurve<T, I, B> curve_; 
    3032        private BootstrapHelper<YieldTermStructure> helper_; 
    3133        private int segment_; 
    3234 
    33         public BootstrapError(PiecewiseYieldCurve<Traits, Interpolator> curve, BootstrapHelper<YieldTermStructure> helper, int segment) { 
     35        public BootstrapError(PiecewiseYieldCurve<T, I, B> curve, BootstrapHelper<YieldTermStructure> helper, int segment) { 
    3436            curve_ = curve; 
    3537            helper_ = helper; 
  • trunk/QLNet/QLNet/Termstructures/Bootstraphelper.cs

    r63 r202  
    2424namespace QLNet { 
    2525    public interface IBootStrap { 
    26         void setup(YieldTermStructure ts); 
    27         void calculate(); 
     26        void setup<T, I, B>(PiecewiseYieldCurve<T, I, B> ts) 
     27            where T : ITraits, new() 
     28            where I : IInterpolationFactory, new() 
     29            where B : IBootStrap, new(); 
     30        void calculate<T, I, B>() 
     31            where T : ITraits, new() 
     32            where I : IInterpolationFactory, new() 
     33            where B : IBootStrap, new(); 
    2834    } 
    2935 
  • trunk/QLNet/QLNet/Termstructures/Iterativebootstrap.cs

    r167 r202  
    2424namespace QLNet { 
    2525    //! Universal piecewise-term-structure boostrapper. 
    26     public class IterativeBootstrap<Traits, Interpolator> : IBootStrap  
    27             where Traits : ITraits, new() 
    28         where Interpolator : IInterpolationFactory, new() { 
     26    public class IterativeBootstrap : IBootStrap { 
    2927         
    3028        private bool validCurve_ = false; 
    31         // define as whatever, actual genrics are not important 
    32         private PiecewiseYieldCurve<Traits, Interpolator> ts_; 
     29        private object tsContainer_; // yes, it is a workaround 
    3330 
    34         public void setup(YieldTermStructure ts) { 
    35             ts_ = ts as PiecewiseYieldCurve<Traits, Interpolator>; 
     31        public void setup<T, I, B>(PiecewiseYieldCurve<T, I, B> ts) 
     32            where T : ITraits, new() 
     33            where I : IInterpolationFactory, new() 
     34            where B : IBootStrap, new() { 
     35 
     36            tsContainer_ = ts; 
     37            PiecewiseYieldCurve<T, I, B> ts_ = tsContainer_ as PiecewiseYieldCurve<T, I, B>; 
    3638 
    3739            int n = ts_.instruments_.Count; 
     
    4547        } 
    4648 
    47         public void calculate() { 
     49        public void calculate<T, I, B>()              
     50            where T : ITraits, new() 
     51            where I : IInterpolationFactory, new() 
     52            where B : IBootStrap, new() { 
     53 
     54            PiecewiseYieldCurve<T, I, B> ts_ = tsContainer_ as PiecewiseYieldCurve<T, I, B>; 
     55 
     56            //prepare instruments 
    4857            int n = ts_.instruments_.Count; 
     58 
     59            // ensure rate helpers are sorted 
     60            ts_.instruments_.Sort((x, y) => x.latestDate().CompareTo(y.latestDate())); 
     61 
     62            // check that there is no instruments with the same maturity 
     63            for (int i = 1; i < n; ++i) { 
     64                Date m1 = ts_.instruments_[i - 1].latestDate(), 
     65                     m2 = ts_.instruments_[i].latestDate(); 
     66                if (m1 == m2) throw new ArgumentException("two instruments have the same maturity (" + m1 + ")"); 
     67            } 
     68 
     69            // check that there is no instruments with invalid quote 
     70            for (int i = 0; i < n; ++i) 
     71                if (!ts_.instruments_[i].quoteIsValid()) 
     72                    throw new ArgumentException("instrument " + i + " (maturity: " + ts_.instruments_[i].latestDate() + 
     73                           ") has an invalid quote"); 
     74 
     75            // setup instruments and register with them 
     76            for (int i = 0; i < n; ++i) { 
     77                // There is a significant interaction with observability. 
     78                ts_.instruments_[i].setTermStructure(ts_); 
     79            } 
     80 
     81            // calculate dates and times 
     82            ts_.dates_ = new InitializedList<Date>(n + 1); 
     83            ts_.times_ = new InitializedList<double>(n + 1); 
     84            ts_.dates_[0] = ts_.initialDate(ts_); 
     85            ts_.times_[0] = ts_.timeFromReference(ts_.dates_[0]); 
     86            for (int i = 0; i < n; ++i) { 
     87                ts_.dates_[i + 1] = ts_.instruments_[i].latestDate(); 
     88                ts_.times_[i + 1] = ts_.timeFromReference(ts_.dates_[i + 1]); 
     89            } 
    4990 
    5091            // set initial guess only if the current curve cannot be used as guess 
     
    100141                    ts_.interpolation_.update(); 
    101142 
    102                     try { 
    103                         var error = new BootstrapError<Traits, Interpolator>(ts_, instrument, i); 
     143                    try { 
     144                        var error = new BootstrapError<T, I, B>(ts_, instrument, i); 
    104145                        double r = solver.solve(error, ts_.accuracy_, guess, min, max); 
    105146                        // redundant assignment (as it has been already performed by BootstrapError in solve procedure), but safe 
     
    107148                    } catch (Exception e) { 
    108149                        validCurve_ = false; 
    109                         throw new ArgumentException(" iteration: " + iteration+1 + 
     150                        throw new ArgumentException(" iteration: " + iteration + 1 + 
    110151                                "could not bootstrap the " + i + " instrument, maturity " + ts_.dates_[i] + 
    111152                                ": " + e.Message); 
  • trunk/QLNet/QLNet/Termstructures/Yield/PiecewiseYieldCurve.cs

    r93 r202  
    2424namespace QLNet { 
    2525    public class PiecewiseYieldCurve<Traits, Interpolator> 
    26         : PiecewiseYieldCurve<Traits, Interpolator, IterativeBootstrap<Traits, Interpolator>
     26        : PiecewiseYieldCurve<Traits, Interpolator, IterativeBootstrap
    2727        where Traits : ITraits, new() 
    2828        where Interpolator : IInterpolationFactory, new() { 
     
    7171        public Interpolator interpolator_; 
    7272 
    73         protected BootStrap bootstrap_ = new BootStrap()
     73        protected BootStrap bootstrap_
    7474        #endregion 
    7575 
     
    112112        #region Constructors 
    113113        public PiecewiseYieldCurve(Date referenceDate, List<BootstrapHelper<YieldTermStructure>> instruments, DayCounter dayCounter) 
    114             : this(referenceDate, instruments, dayCounter, new Handle<Quote>(), 1.0e-12, new Interpolator()) { } 
     114            : this(referenceDate, instruments, dayCounter, new Handle<Quote>(), 1.0e-12, new Interpolator(), new BootStrap()) { } 
    115115        public PiecewiseYieldCurve(Date referenceDate, List<BootstrapHelper<YieldTermStructure>> instruments, 
    116116                                   DayCounter dayCounter, Handle<Quote> turnOfYearEffect) 
    117             : this(referenceDate, instruments, dayCounter, turnOfYearEffect, 1.0e-12, new Interpolator()) { } 
     117            : this(referenceDate, instruments, dayCounter, turnOfYearEffect, 1.0e-12, new Interpolator(), new BootStrap()) { } 
    118118        public PiecewiseYieldCurve(Date referenceDate, List<BootstrapHelper<YieldTermStructure>> instruments, 
    119119                                   DayCounter dayCounter, Handle<Quote> turnOfYearEffect, double accuracy) 
    120             : this(referenceDate, instruments, dayCounter, turnOfYearEffect, accuracy, new Interpolator()) { } 
     120            : this(referenceDate, instruments, dayCounter, turnOfYearEffect, accuracy, new Interpolator(), new BootStrap()) { } 
    121121        public PiecewiseYieldCurve(Date referenceDate, List<BootstrapHelper<YieldTermStructure>> instruments, 
    122122                                   DayCounter dayCounter, Handle<Quote> turnOfYearEffect, double accuracy, Interpolator i) 
     123            : this(referenceDate, instruments, dayCounter, turnOfYearEffect, accuracy, i, new BootStrap()) { } 
     124        public PiecewiseYieldCurve(Date referenceDate, List<BootstrapHelper<YieldTermStructure>> instruments, 
     125                                   DayCounter dayCounter, Handle<Quote> turnOfYearEffect, double accuracy, 
     126                                   Interpolator i, BootStrap bootstrap) 
    123127            : base(referenceDate, new Calendar(), dayCounter) { 
    124128            instruments_ = instruments; 
     
    126130            accuracy_ = accuracy; 
    127131            interpolator_ = i; 
     132            bootstrap_ = bootstrap; 
    128133 
    129134            setTurnOfYear(); 
    130135            turnOfYearEffect_.registerWith(update); 
    131             bootstrap_.setup(this); 
     136            bootstrap_.setup<Traits, Interpolator, BootStrap>(this); 
    132137        } 
    133138 
     
    140145        public PiecewiseYieldCurve(int settlementDays, Calendar calendar, List<BootstrapHelper<YieldTermStructure>> instruments, 
    141146                                   DayCounter dayCounter, Handle<Quote> turnOfYearEffect, double accuracy) 
    142             : this(settlementDays, calendar, instruments, dayCounter, turnOfYearEffect, accuracy, new Interpolator()) { } 
     147            : this(settlementDays, calendar, instruments, dayCounter, turnOfYearEffect, accuracy,  
     148                   new Interpolator(), new BootStrap()) { } 
    143149        public PiecewiseYieldCurve(int settlementDays, Calendar calendar, List<BootstrapHelper<YieldTermStructure>> instruments, 
    144                                    DayCounter dayCounter, Handle<Quote> turnOfYearEffect, double accuracy, Interpolator i) 
     150                                   DayCounter dayCounter, Handle<Quote> turnOfYearEffect, double accuracy, 
     151                                   Interpolator i, BootStrap bootstrap) 
    145152            : base(settlementDays, calendar, dayCounter) { 
    146153            instruments_ = instruments; 
     
    148155            accuracy_ = accuracy; 
    149156            interpolator_ = i; 
     157            bootstrap_ = bootstrap; 
    150158 
    151159            setTurnOfYear(); 
    152160            turnOfYearEffect_.registerWith(update); 
    153             bootstrap_.setup(this); 
     161            bootstrap_.setup<Traits, Interpolator, BootStrap>(this); 
    154162        }  
    155163        #endregion 
     
    185193 
    186194        protected override void performCalculations() { 
    187             // this code was origionally in IterativeBootStrap but it does not make sense to have it there 
    188             // because it relies too much on this class 
    189  
    190             //prepare instruments 
    191             int n = instruments_.Count; 
    192  
    193             // ensure rate helpers are sorted 
    194             instruments_.Sort((x, y) => x.latestDate().CompareTo(y.latestDate())); 
    195  
    196             // check that there is no instruments with the same maturity 
    197             for (int i = 1; i < n; ++i) { 
    198                 Date m1 = instruments_[i - 1].latestDate(), 
    199                      m2 = instruments_[i].latestDate(); 
    200                 if (m1 == m2) throw new ArgumentException("two instruments have the same maturity (" + m1 + ")"); 
    201             } 
    202  
    203             // check that there is no instruments with invalid quote 
    204             for (int i = 0; i < n; ++i) 
    205                 if (!instruments_[i].quoteIsValid()) 
    206                     throw new ArgumentException("instrument " + i + " (maturity: " + instruments_[i].latestDate() + 
    207                            ") has an invalid quote"); 
    208  
    209             // setup instruments and register with them 
    210             for (int i = 0; i < n; ++i) { 
    211                 // There is a significant interaction with observability. 
    212                 instruments_[i].setTermStructure(this); 
    213             } 
    214  
    215             // calculate dates and times 
    216             dates_ = new InitializedList<Date>(n + 1); 
    217             times_ = new InitializedList<double>(n + 1); 
    218             dates_[0] = initialDate(this); 
    219             times_[0] = timeFromReference(dates_[0]); 
    220             for (int i = 0; i < n; ++i) { 
    221                 dates_[i + 1] = instruments_[i].latestDate(); 
    222                 times_[i + 1] = timeFromReference(dates_[i + 1]); 
    223             } 
    224  
    225             // now comes the bootstrapping 
    226195            // just delegate to the bootstrapper 
    227             bootstrap_.calculate(); 
     196            bootstrap_.calculate<Traits, Interpolator, BootStrap>(); 
    228197        } 
    229198    } 
  • trunk/QLNet/Test2008/T_Piecewiseyieldcurve.cs

    r189 r202  
    2323using QLNet; 
    2424 
    25 namespace TestSuite 
    26 
    27    [TestClass()] 
    28    public class T_Piecewiseyieldcurve 
    29    { 
    30       public class CommonVars 
    31       { 
    32          #region Values 
    33          public struct Datum 
    34          { 
    35             public int n; 
    36             public TimeUnit units; 
    37             public double rate; 
    38          }; 
    39          public struct BondDatum 
    40          { 
    41             public int n; 
    42             public TimeUnit units; 
    43             public int length; 
    44             public Frequency frequency; 
    45             public double coupon; 
    46             public double price; 
    47          }; 
    48  
    49          public Datum[] depositData = new Datum[] { 
     25namespace TestSuite { 
     26    [TestClass()] 
     27    public class T_Piecewiseyieldcurve { 
     28        public class CommonVars { 
     29            #region Values 
     30            public struct Datum { 
     31                public int n; 
     32                public TimeUnit units; 
     33                public double rate; 
     34            }; 
     35            public struct BondDatum { 
     36                public int n; 
     37                public TimeUnit units; 
     38                public int length; 
     39                public Frequency frequency; 
     40                public double coupon; 
     41                public double price; 
     42            }; 
     43 
     44            public Datum[] depositData = new Datum[] { 
    5045                new Datum { n = 1, units = TimeUnit.Weeks,  rate = 4.559 }, 
    5146                new Datum { n = 1, units = TimeUnit.Months, rate = 4.581 }, 
     
    5651            }; 
    5752 
    58          public Datum[] fraData = new Datum[] { 
     53            public Datum[] fraData = new Datum[] { 
    5954                new Datum { n = 1, units = TimeUnit.Months, rate = 4.581 }, 
    6055                new Datum { n = 2, units = TimeUnit.Months, rate = 4.573 }, 
     
    6459            }; 
    6560 
    66          public Datum[] swapData = new Datum[] { 
     61            public Datum[] swapData = new Datum[] { 
    6762                new Datum { n =  1, units = TimeUnit.Years, rate = 4.54 }, 
    6863                new Datum { n =  2, units = TimeUnit.Years, rate = 4.63 }, 
     
    8277            }; 
    8378 
    84          public BondDatum[] bondData = new BondDatum[] { 
     79            public BondDatum[] bondData = new BondDatum[] { 
    8580                new BondDatum { n =  6, units = TimeUnit.Months, length = 5,  frequency = Frequency.Semiannual, coupon = 4.75, price = 101.320 }, 
    8681                new BondDatum { n =  1, units = TimeUnit.Years,  length = 3,  frequency = Frequency.Semiannual, coupon = 2.75, price = 100.590 }, 
     
    9085            }; 
    9186 
    92          public Datum[] bmaData = new Datum[] { 
     87            public Datum[] bmaData = new Datum[] { 
    9388                new Datum { n =  1, units = TimeUnit.Years, rate = 67.56 }, 
    9489                new Datum { n =  2, units = TimeUnit.Years, rate = 68.00 }, 
     
    10297                new Datum { n = 30, units = TimeUnit.Years, rate = 73.81 } 
    10398            }; 
    104          #endregion 
    105  
    106          // global variables 
    107          public Calendar calendar; 
    108          public int settlementDays; 
    109          public Date today, settlement; 
    110          public BusinessDayConvention fixedLegConvention; 
    111          public Frequency fixedLegFrequency; 
    112          public DayCounter fixedLegDayCounter; 
    113          public int bondSettlementDays; 
    114          public DayCounter bondDayCounter; 
    115          public BusinessDayConvention bondConvention; 
    116          public double bondRedemption; 
    117          public Frequency bmaFrequency; 
    118          public BusinessDayConvention bmaConvention; 
    119          public DayCounter bmaDayCounter; 
    120  
    121          public int deposits, fras, swaps, bonds, bmas; 
    122          public List<SimpleQuote> rates, fraRates, prices, fractions; 
    123          public List<BootstrapHelper<YieldTermStructure>> instruments, fraHelpers, bondHelpers, bmaHelpers; 
    124          public List<Schedule> schedules; 
    125          public YieldTermStructure termStructure; 
    126  
    127          // cleanup 
    128          // SavedSettings backup = new SavedSettings(); 
    129          // IndexHistoryCleaner cleaner; 
    130  
    131          // setup 
    132          public CommonVars() 
    133          { 
    134  
    135             //cleaner = new IndexHistoryCleaner(); 
    136             ////GC.Collect(); 
    137             //// force garbage collection 
    138             //// garbage collection in .NET is rather weird and we do need when we run several tests in a row 
    139             //GC.Collect(); 
    140  
    141             // data 
    142             calendar = new TARGET(); 
    143             settlementDays = 2; 
    144             today = calendar.adjust(Date.Today); 
    145             Settings.setEvaluationDate(today); 
    146             settlement = calendar.advance(today, settlementDays, TimeUnit.Days); 
    147             fixedLegConvention = BusinessDayConvention.Unadjusted; 
    148             fixedLegFrequency = Frequency.Annual; 
    149             fixedLegDayCounter = new Thirty360(Thirty360.Thirty360Convention.European); 
    150             bondSettlementDays = 3; 
    151             bondDayCounter = new ActualActual(); 
    152             bondConvention = BusinessDayConvention.Following; 
    153             bondRedemption = 100.0; 
    154             bmaFrequency = Frequency.Quarterly; 
    155             bmaConvention = BusinessDayConvention.Following; 
    156             bmaDayCounter = new ActualActual(); 
    157  
    158             deposits = depositData.Length; 
    159             fras = fraData.Length; 
    160             swaps = swapData.Length; 
    161             bonds = bondData.Length; 
    162             bmas = bmaData.Length; 
    163  
    164             // market elements 
    165             rates = new List<SimpleQuote>(deposits + swaps); 
    166             fraRates = new List<SimpleQuote>(fras); 
    167             prices = new List<SimpleQuote>(bonds); 
    168             fractions = new List<SimpleQuote>(bmas); 
    169             for (int i = 0; i < deposits; i++) 
    170             { 
    171                rates.Add(new SimpleQuote(depositData[i].rate / 100)); 
    172             } 
    173             for (int i = 0; i < swaps; i++) 
    174             { 
    175                rates.Add(new SimpleQuote(swapData[i].rate / 100)); 
    176             } 
    177             for (int i = 0; i < fras; i++) 
    178             { 
    179                fraRates.Add(new SimpleQuote(fraData[i].rate / 100)); 
    180             } 
    181             for (int i = 0; i < bonds; i++) 
    182             { 
    183                prices.Add(new SimpleQuote(bondData[i].price)); 
    184             } 
    185             for (int i = 0; i < bmas; i++) 
    186             { 
    187                fractions.Add(new SimpleQuote(bmaData[i].rate / 100)); 
    188             } 
    189  
    190             // rate helpers 
    191             instruments = new List<BootstrapHelper<YieldTermStructure>>(deposits + swaps); 
    192             fraHelpers = new List<BootstrapHelper<YieldTermStructure>>(fras); 
    193             bondHelpers = new List<BootstrapHelper<YieldTermStructure>>(bonds); 
    194             schedules = new List<Schedule>(bonds); 
    195             bmaHelpers = new List<BootstrapHelper<YieldTermStructure>>(bmas); 
    196  
    197             IborIndex euribor6m = new Euribor6M(); 
    198             for (int i = 0; i < deposits; i++) 
    199             { 
    200                Handle<Quote> r = new Handle<Quote>(rates[i]); 
    201                instruments.Add(new DepositRateHelper(r, new Period(depositData[i].n, depositData[i].units), 
    202                                  euribor6m.fixingDays(), calendar, 
    203                                  euribor6m.businessDayConvention(), 
    204                                  euribor6m.endOfMonth(), 
    205                                  euribor6m.dayCounter())); 
    206             } 
    207             for (int i = 0; i < swaps; i++) 
    208             { 
    209                Handle<Quote> r = new Handle<Quote>(rates[i + deposits]); 
    210                instruments.Add(new SwapRateHelper(r, new Period(swapData[i].n, swapData[i].units), calendar, 
    211                                fixedLegFrequency, fixedLegConvention, fixedLegDayCounter, euribor6m)); 
    212             } 
    213  
    214             Euribor3M euribor3m = new Euribor3M(); 
    215             for (int i = 0; i < fras; i++) 
    216             { 
    217                Handle<Quote> r = new Handle<Quote>(fraRates[i]); 
    218                fraHelpers.Add(new FraRateHelper(r, fraData[i].n, fraData[i].n + 3, 
    219                                  euribor3m.fixingDays(), 
    220                                  euribor3m.fixingCalendar(), 
    221                                  euribor3m.businessDayConvention(), 
    222                                  euribor3m.endOfMonth(), 
    223                                  euribor3m.dayCounter())); 
    224             } 
    225  
    226             for (int i = 0; i < bonds; i++) 
    227             { 
    228                Handle<Quote> p = new Handle<Quote>(prices[i]); 
    229                Date maturity = calendar.advance(today, bondData[i].n, bondData[i].units); 
    230                Date issue = calendar.advance(maturity, -bondData[i].length, TimeUnit.Years); 
    231                List<double> coupons = new List<double>() { bondData[i].coupon / 100.0 }; 
    232                schedules.Add(new Schedule(issue, maturity, new Period(bondData[i].frequency), calendar, 
    233                                           bondConvention, bondConvention, DateGeneration.Rule.Backward, false)); 
    234                bondHelpers.Add(new FixedRateBondHelper(p, bondSettlementDays, bondRedemption, schedules[i], 
    235                                            coupons, bondDayCounter, bondConvention, bondRedemption, issue)); 
    236             } 
    237          } 
    238       } 
    239  
    240       [TestMethod()] 
    241       public void testLogCubicDiscountConsistency() 
    242       { 
    243          // "Testing consistency of piecewise-log-cubic discount curve..."); 
    244  
    245          CommonVars vars = new CommonVars(); 
    246  
    247          testCurveConsistency<Discount, LogCubic>(vars, 
    248                      new LogCubic(CubicInterpolation.DerivativeApprox.Spline, true, 
    249                          CubicInterpolation.BoundaryCondition.SecondDerivative, 0.0, 
    250                          CubicInterpolation.BoundaryCondition.SecondDerivative, 0.0)); 
    251          testBMACurveConsistency<Discount, LogCubic>(vars, 
    252                      new LogCubic(CubicInterpolation.DerivativeApprox.Spline, true, 
    253                       CubicInterpolation.BoundaryCondition.SecondDerivative, 0.0, 
    254                       CubicInterpolation.BoundaryCondition.SecondDerivative, 0.0)); 
    255       } 
    256  
    257       [TestMethod()] 
    258       public void testLinearDiscountConsistency() 
    259       { 
    260          // "Testing consistency of piecewise-linear discount curve..." 
    261  
    262          CommonVars vars = new CommonVars(); 
    263  
    264          testCurveConsistency<Discount, Linear>(vars); 
    265          testBMACurveConsistency<Discount, Linear>(vars); 
    266       } 
    267  
    268       [TestMethod()] 
    269       public void testLinearZeroConsistency() 
    270       { 
    271          // "Testing consistency of piecewise-linear zero-yield curve..."); 
    272  
    273          CommonVars vars = new CommonVars(); 
    274  
    275          testCurveConsistency<ZeroYield, Linear>(vars); 
    276          testBMACurveConsistency<ZeroYield, Linear>(vars); 
    277       } 
    278  
    279       [TestMethod()] 
    280       public void testLinearForwardConsistency() 
    281       { 
    282          // "Testing consistency of piecewise-linear forward-rate curve..."); 
    283  
    284          CommonVars vars = new CommonVars(); 
    285  
    286          testCurveConsistency<ForwardRate, Linear>(vars); 
    287          testBMACurveConsistency<ForwardRate, Linear>(vars); 
    288       } 
    289  
    290       [TestMethod()] 
    291       public void testLogLinearDiscountConsistency() 
    292       { 
    293          // "Testing consistency of piecewise-log-linear discount curve..."); 
    294  
    295          CommonVars vars = new CommonVars(); 
    296  
    297          testCurveConsistency<Discount, LogLinear>(vars); 
    298          testBMACurveConsistency<Discount, LogLinear>(vars); 
    299       } 
    300  
    301       [TestMethod()] 
    302       public void testLogLinearZeroConsistency() 
    303       { 
    304          // "Testing consistency of piecewise-log-linear zero-yield curve..."); 
    305  
    306          CommonVars vars = new CommonVars(); 
    307  
    308          testCurveConsistency<ZeroYield, LogLinear>(vars); 
    309          testBMACurveConsistency<ZeroYield, LogLinear>(vars); 
    310       } 
    311  
    312       [TestMethod()] 
    313       public void testObservability() 
    314       { 
    315          // "Testing observability of piecewise yield curve..."); 
    316  
    317          CommonVars vars = new CommonVars(); 
    318  
    319          vars.termStructure = new PiecewiseYieldCurve<Discount, LogLinear>(vars.settlementDays, 
    320                                                         vars.calendar, vars.instruments, new Actual360()); 
    321          Flag f = new Flag(); 
    322          vars.termStructure.registerWith(f.update); 
    323  
    324          for (int i = 0; i < vars.deposits + vars.swaps; i++) 
    325          { 
    326             double testTime = new Actual360().yearFraction(vars.settlement, vars.instruments[i].latestDate()); 
    327             double discount = vars.termStructure.discount(testTime); 
     99            #endregion 
     100 
     101            // global variables 
     102            public Calendar calendar; 
     103            public int settlementDays; 
     104            public Date today, settlement; 
     105            public BusinessDayConvention fixedLegConvention; 
     106            public Frequency fixedLegFrequency; 
     107            public DayCounter fixedLegDayCounter; 
     108            public int bondSettlementDays; 
     109            public DayCounter bondDayCounter; 
     110            public BusinessDayConvention bondConvention; 
     111            public double bondRedemption; 
     112            public Frequency bmaFrequency; 
     113            public BusinessDayConvention bmaConvention; 
     114            public DayCounter bmaDayCounter; 
     115 
     116            public int deposits, fras, swaps, bonds, bmas; 
     117            public List<SimpleQuote> rates, fraRates, prices, fractions; 
     118            public List<BootstrapHelper<YieldTermStructure>> instruments, fraHelpers, bondHelpers, bmaHelpers; 
     119            public List<Schedule> schedules; 
     120            public YieldTermStructure termStructure; 
     121 
     122            // cleanup 
     123            // SavedSettings backup = new SavedSettings(); 
     124            // IndexHistoryCleaner cleaner; 
     125 
     126            // setup 
     127            public CommonVars() { 
     128 
     129                //cleaner = new IndexHistoryCleaner(); 
     130                ////GC.Collect(); 
     131                //// force garbage collection 
     132                //// garbage collection in .NET is rather weird and we do need when we run several tests in a row 
     133                //GC.Collect(); 
     134 
     135                // data 
     136                calendar = new TARGET(); 
     137                settlementDays = 2; 
     138                today = calendar.adjust(Date.Today); 
     139                Settings.setEvaluationDate(today); 
     140                settlement = calendar.advance(today, settlementDays, TimeUnit.Days); 
     141                fixedLegConvention = BusinessDayConvention.Unadjusted; 
     142                fixedLegFrequency = Frequency.Annual; 
     143                fixedLegDayCounter = new Thirty360(Thirty360.Thirty360Convention.European); 
     144                bondSettlementDays = 3; 
     145                bondDayCounter = new ActualActual(); 
     146                bondConvention = BusinessDayConvention.Following; 
     147                bondRedemption = 100.0; 
     148                bmaFrequency = Frequency.Quarterly; 
     149                bmaConvention = BusinessDayConvention.Following; 
     150                bmaDayCounter = new ActualActual(); 
     151 
     152                deposits = depositData.Length; 
     153                fras = fraData.Length; 
     154                swaps = swapData.Length; 
     155                bonds = bondData.Length; 
     156                bmas = bmaData.Length; 
     157 
     158                // market elements 
     159                rates = new List<SimpleQuote>(deposits + swaps); 
     160                fraRates = new List<SimpleQuote>(fras); 
     161                prices = new List<SimpleQuote>(bonds); 
     162                fractions = new List<SimpleQuote>(bmas); 
     163                for (int i = 0; i < deposits; i++) { 
     164                    rates.Add(new SimpleQuote(depositData[i].rate / 100