Assembla home | Assembla project page
 

Changeset 131

Show
Ignore:
Timestamp:
04/27/08 13:54:35 (7 months ago)
Author:
snovik
Message:

New: PDE

Files:

Legend:

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

    r126 r131  
    131131            int timeSteps = 801; 
    132132            method = "Finite differences"; 
    133             //europeanOption.setPricingEngine(boost::shared_ptr<PricingEngine>( 
    134             //             new FDEuropeanEngine(bsmProcess,timeSteps,timeSteps-1))); 
     133            europeanOption.setPricingEngine(new FDEuropeanEngine(bsmProcess,timeSteps,timeSteps-1)); 
    135134            //bermudanOption.setPricingEngine(boost::shared_ptr<PricingEngine>( 
    136135            //             new FDBermudanEngine(bsmProcess,timeSteps,timeSteps-1))); 
  • trunk/QLNet/QLNet/Instruments/BarrierOption.cs

    r130 r131  
    4242                        public double? rebate; 
    4343 
    44                         public void validate() 
     44                        public override void validate() 
    4545                        { 
    4646                                base.validate(); 
  • trunk/QLNet/QLNet/Instruments/Instrument.cs

    r130 r131  
    2929        // The value of these attributes and any other that derived classes might declare must be set during calculation. 
    3030        protected double? NPV_, errorEstimate_; 
    31         protected Dictionary<string, double> additionalResults_ = new Dictionary<string,double>(); 
     31        protected Dictionary<string, object> additionalResults_ = new Dictionary<string, object>(); 
    3232        protected IPricingEngine engine_; 
    3333 
     
    108108 
    109109        // returns any additional result returned by the pricing engine. 
    110         public double result(string tag) 
     110        public object result(string tag) 
    111111        { 
    112112            calculate(); 
     
    122122 
    123123        // returns all additional result returned by the pricing engine. 
    124         public Dictionary<string, double> additionalResults() { return additionalResults_; } 
     124        public Dictionary<string, object> additionalResults() { return additionalResults_; } 
    125125        #endregion 
    126126 
     
    140140            public double? value; 
    141141            public double? errorEstimate; 
    142             public Dictionary<string, double> additionalResults = new Dictionary<string, double>(); 
     142            public Dictionary<string, object> additionalResults = new Dictionary<string, object>(); 
    143143 
    144144            public virtual void reset() 
  • trunk/QLNet/QLNet/Instruments/MultiAssetOption.cs

    r130 r131  
    9696                } 
    9797 
    98         public void setupArguments(IPricingEngineArguments args) 
     98        public override void setupArguments(IPricingEngineArguments args) 
    9999                { 
    100100                        MultiAssetOption.Arguments arguments = args as MultiAssetOption.Arguments; 
  • trunk/QLNet/QLNet/Instruments/OneAssetOption.cs

    r128 r131  
    144144 
    145145 
    146  
    147146        //! %Results from single-asset option calculation 
    148147        new public class Results : Instrument.Results { 
  • trunk/QLNet/QLNet/Instruments/VanillaSwap.cs

    r130 r131  
    248248            } 
    249249             
    250             public void validate() { 
     250            public override void validate() { 
    251251                base.validate(); 
    252252 
     
    276276                fairRate = fairSpread = null; 
    277277            } 
    278         }; 
    279  
    280         new class Engine : GenericEngine<VanillaSwap.Arguments, VanillaSwap.Results> { 
    281             public override void calculate() { throw new NotImplementedException(); } 
    282278        } 
    283279    } 
  • trunk/QLNet/QLNet/Math/SampledCurve.cs

    r110 r131  
    7575 
    7676        /*! \todo replace or complement with a more general function firstDerivativeAt(spot) */ 
    77         public double irstDerivativeAtCenter() { 
     77        public double firstDerivativeAtCenter() { 
    7878            if (!(size() >= 3)) 
    7979                throw new ApplicationException("the size of the curve must be at least 3"); 
  • trunk/QLNet/QLNet/Methods/Finitedifferences/BoundaryCondition.cs

    r126 r131  
    3232            applied to an array \f$ u \f$ so that \f$ v = Lu \f$ will 
    3333            satisfy the given condition. */ 
    34         public virtual void applyBeforeApplying(Operator o) { throw new NotSupportedException(); } 
     34        public virtual void applyBeforeApplying(IOperator o) { throw new NotSupportedException(); } 
    3535 
    3636        /*! This method modifies an array \f$ u \f$ so that it satisfies the given condition. */ 
     
    4040            system \f$ Lu' = u \f$ is solved so that \f$ u' \f$ will 
    4141            satisfy the given condition. */ 
    42         public virtual void applyBeforeSolving(Operator o, Vector v) { throw new NotSupportedException(); } 
     42        public virtual void applyBeforeSolving(IOperator o, Vector v) { throw new NotSupportedException(); } 
    4343 
    4444        /*! This method modifies an array \f$ u \f$ so that it satisfies the given condition. */ 
     
    6161        \ingroup findiff 
    6262    */ 
    63     public class NeumannBC : BoundaryCondition<TridiagonalOperator> { 
     63    // NeumanBC works on TridiagonalOperator. IOperator here is for type compatobility with options 
     64    public class NeumannBC : BoundaryCondition<IOperator> { 
    6465        private double value_; 
    6566        private Side side_; 
     
    7172 
    7273        // interface 
    73         public override void applyBeforeApplying(TridiagonalOperator L) { 
     74        public override void applyBeforeApplying(IOperator o) { 
     75            TridiagonalOperator L = o as TridiagonalOperator; 
    7476            switch (side_) { 
    7577                case Side.Lower: 
     
    9799        } 
    98100 
    99         public override void applyBeforeSolving(TridiagonalOperator L, Vector rhs) { 
     101        public override void applyBeforeSolving(IOperator o, Vector rhs) { 
     102            TridiagonalOperator L = o as TridiagonalOperator; 
    100103            switch (side_) { 
    101104                case Side.Lower: 
  • trunk/QLNet/QLNet/Methods/Finitedifferences/OperatorFactory.cs

    r117 r131  
    3232                                                      double residualTime, bool timeDependent) { 
    3333            if (timeDependent) 
    34                 throw new NotImplementedException(); 
    35                 //return BSMTermOperator(grid, process, residualTime); 
     34                //! Black-Scholes-Merton differential operator 
     35                /*! \ingroup findiff 
     36 
     37                    \test coefficients are tested against constant BSM operator 
     38                */ 
     39                return new PdeOperator<PdeBSM>(grid, process, residualTime); 
    3640            else 
    37                 throw new NotImplementedException(); 
    38                 //return BSMOperator(grid, process, residualTime); 
     41                return new BSMOperator(grid, process, residualTime); 
    3942        } 
    4043        public static TridiagonalOperator getOperator(OneFactorModel.ShortRateDynamics process, Vector grid) { 
    4144            throw new NotImplementedException(); 
    42             //return OneFactorOperator(grid, process); 
     45            //! Interest-rate single factor model differential operator 
     46            //return new PdeOperator<PdeShortRate>(grid, process); 
    4347        } 
    4448    } 
  • trunk/QLNet/QLNet/Methods/Finitedifferences/cranknicolson.cs

    r127 r131  
    6060    class CrankNicolson<Operator> : MixedScheme<Operator>, ISchemeFactory where Operator : IOperator, new() { 
    6161        // constructors 
     62        public CrankNicolson() { }  // required for generics 
    6263        public CrankNicolson(Operator L, List<BoundaryCondition<IOperator>> bcs) 
    6364            : base(L, 0.5, bcs) { } 
  • trunk/QLNet/QLNet/Methods/Finitedifferences/finitedifferencemodel.cs

    r127 r131  
    3030 
    3131        // constructors 
    32         //public FiniteDifferenceModel(Operator L, List<BoundaryCondition<Operator>> bcs, 
    33         //                      List<double> stoppingTimes = new List<double>()) 
     32        public FiniteDifferenceModel(IOperator L, List<BoundaryCondition<IOperator>> bcs) 
     33            : this(L, bcs, new List<double>()) { } 
    3434        public FiniteDifferenceModel(IOperator L, List<BoundaryCondition<IOperator>> bcs, List<double> stoppingTimes) { 
    3535            evolver_ = (Evolver)new Evolver().factory(L, bcs); 
  • trunk/QLNet/QLNet/Methods/Finitedifferences/mixedscheme.cs

    r127 r131  
    7878 
    7979        // constructors 
     80        public MixedScheme() { }  // required for generics 
    8081        public MixedScheme(Operator L, double theta, List<BoundaryCondition<IOperator>> bcs) { 
    8182            L_ = L; 
  • trunk/QLNet/QLNet/PricingEngine.cs

    r130 r131  
    4343    } 
    4444 
     45    // template base class for option pricing engines 
     46    // Derived engines only need to implement the <tt>calculate()</tt> method. 
     47    public abstract class GenericEngine<ArgumentsType, ResultsType> : IGenericEngine<ArgumentsType, ResultsType> 
     48        where ArgumentsType : IPricingEngineArguments, new() 
     49        where ResultsType : IPricingEngineResults, new() 
     50    { 
     51        protected ArgumentsType arguments_ = new ArgumentsType(); 
     52        protected ResultsType results_ = new ResultsType(); 
     53 
     54        public IPricingEngineArguments getArguments() { return arguments_; } 
     55        public IPricingEngineResults getResults() { return results_; } 
     56        public void reset() { results_.reset(); } 
     57 
     58        public virtual void calculate() { } 
     59 
     60        #region Observer & Observable 
     61        // observable interface 
     62        public event Callback notifyObserversEvent; 
     63        public void registerWith(Callback handler) { notifyObserversEvent += handler; } 
     64        public void unregisterWith(Callback handler) { notifyObserversEvent -= handler; } 
     65        protected void notifyObservers() { 
     66            Callback handler = notifyObserversEvent; 
     67            if (handler != null) { 
     68                handler(); 
     69            } 
     70        } 
     71 
     72        public void update() { notifyObservers(); }  
     73        #endregion 
     74    } 
     75 
     76 
    4577    //! abstract class for pricing engines 
    4678    //public abstract class PricingEngine 
     
    73105    //    } 
    74106    //} 
    75  
    76     // template base class for option pricing engines 
    77     // Derived engines only need to implement the <tt>calculate()</tt> method. 
    78  
    79     public abstract class GenericEngine<ArgumentsType, ResultsType> : IGenericEngine<ArgumentsType, ResultsType> 
    80         where ArgumentsType : IPricingEngineArguments, new() 
    81         where ResultsType : IPricingEngineResults, new() 
    82     { 
    83         protected ArgumentsType arguments_ = new ArgumentsType(); 
    84         protected ResultsType results_ = new ResultsType(); 
    85  
    86         public IPricingEngineArguments getArguments() { return arguments_; } 
    87         public IPricingEngineResults getResults() { return results_; } 
    88         public void reset() { results_.reset(); } 
    89  
    90         public virtual void calculate() { } 
    91  
    92         #region Observer & Observable 
    93         // observable interface 
    94         public event Callback notifyObserversEvent; 
    95         public void registerWith(Callback handler) { notifyObserversEvent += handler; } 
    96         public void unregisterWith(Callback handler) { notifyObserversEvent -= handler; } 
    97         protected void notifyObservers() { 
    98             Callback handler = notifyObserversEvent; 
    99             if (handler != null) { 
    100                 handler(); 
    101             } 
    102         } 
    103  
    104         public void update() { notifyObservers(); }  
    105         #endregion 
    106     }; 
    107107} 
  • trunk/QLNet/QLNet/Pricingengines/vanilla/FDEuropeanEngine.cs

    r128 r131  
    2929              checking it against analytic results. 
    3030    */ 
    31     public class FDEuropeanEngine : OneAssetOption.Engine
     31    public class FDEuropeanEngine : FDVanillaEngine, IGenericEngine<OneAssetOption.Arguments, OneAssetOption.Results>
    3232        private SampledCurve prices_; 
    33         private FDVanillaEngine engine_; 
    3433 
    3534        //public FDEuropeanEngine(GeneralizedBlackScholesProcess process, 
    3635        //                        Size timeSteps=100, Size gridPoints=100, bool timeDependent = false) { 
    37         public FDEuropeanEngine(GeneralizedBlackScholesProcess process, int timeSteps, int gridPoints, bool timeDependent) { 
    38             engine_ = new FDVanillaEngine(process, timeSteps, gridPoints, timeDependent); 
     36        public FDEuropeanEngine(GeneralizedBlackScholesProcess process, int timeSteps, int gridPoints) 
     37            : this(process, timeSteps, gridPoints, false) { } 
     38        public FDEuropeanEngine(GeneralizedBlackScholesProcess process, int timeSteps, int gridPoints, bool timeDependent) 
     39            : base(process, timeSteps, gridPoints, timeDependent) { 
    3940            prices_ = new SampledCurve(gridPoints); 
    4041 
     
    4243        } 
    4344 
    44         //new private void calculate() { 
    45         //    setupArguments(arguments_); 
    46         //    setGridLimits(); 
    47         //    initializeInitialCondition(); 
    48         //    initializeOperator(); 
    49         //    initializeBoundaryConditions(); 
     45        public void calculate() { 
     46            setupArguments(arguments_); 
     47            setGridLimits(); 
     48            initializeInitialCondition(); 
     49            initializeOperator(); 
     50            initializeBoundaryConditions(); 
    5051 
    51         //    var model = new FiniteDifferenceModel<CrankNicolson<TridiagonalOperator>>(finiteDifferenceOperator_, BCs_); 
     52            var model = new FiniteDifferenceModel<CrankNicolson<TridiagonalOperator>>(finiteDifferenceOperator_, BCs_); 
    5253 
    53         //    prices_ = intrinsicValues_; 
     54            prices_ = intrinsicValues_; 
    5455 
    55         //    model.rollback(prices_.values(), getResidualTime(), 0, timeSteps_); 
     56            model.rollback(prices_.values(), getResidualTime(), 0, timeSteps_); 
    5657 
    57         //    results_.value = prices_.valueAtCenter(); 
    58         //    results_.delta = prices_.firstDerivativeAtCenter(); 
    59         //    results_.gamma = prices_.secondDerivativeAtCenter(); 
    60         //    results_.theta = Utils.blackScholesTheta(process_, 
    61         //                                       results_.value, 
    62         //                                       results_.delta, 
    63         //                                       results_.gamma); 
    64         //    results_.additionalResults["priceCurve"] = prices_; 
    65         //} 
     58            results_.value = prices_.valueAtCenter(); 
     59            results_.delta = prices_.firstDerivativeAtCenter(); 
     60            results_.gamma = prices_.secondDerivativeAtCenter(); 
     61            results_.theta = Utils.blackScholesTheta(process_, 
     62                                                     results_.value.GetValueOrDefault(), 
     63                                                     results_.delta.GetValueOrDefault(), 
     64                                                     results_.gamma.GetValueOrDefault()); 
     65            results_.additionalResults.Add("priceCurve", prices_); 
     66        } 
     67 
     68        #region IGenericEngine copy-cat 
     69        protected OneAssetOption.Arguments arguments_ = new OneAssetOption.Arguments(); 
     70        protected OneAssetOption.Results results_ = new OneAssetOption.Results(); 
     71 
     72        public IPricingEngineArguments getArguments() { return arguments_; } 
     73        public IPricingEngineResults getResults() { return results_; } 
     74        public void reset() { results_.reset(); } 
     75 
     76        #region Observer & Observable 
     77        // observable interface 
     78        public event Callback notifyObserversEvent; 
     79        public void registerWith(Callback handler) { notifyObserversEvent += handler; } 
     80        public void unregisterWith(Callback handler) { notifyObserversEvent -= handler; } 
     81        protected void notifyObservers() { 
     82            Callback handler = notifyObserversEvent; 
     83            if (handler != null) { 
     84                handler(); 
     85            } 
     86        } 
     87 
     88        public void update() { notifyObservers(); } 
     89        #endregion  
     90        #endregion 
    6691    } 
    6792} 
  • trunk/QLNet/QLNet/Pricingengines/vanilla/FDVanillaEngine.cs

    r130 r131  
    4040 
    4141        // typedef BoundaryCondition<TridiagonalOperator> bc_type; 
    42         protected List<BoundaryCondition<TridiagonalOperator>> BCs_; 
     42        protected List<BoundaryCondition<IOperator>> BCs_; 
    4343        // temporaries 
    4444        protected double sMin_, center_, sMax_; 
     
    5757            timeDependent_ = timeDependent; 
    5858            intrinsicValues_ = new SampledCurve(gridPoints); 
    59             BCs_ = new InitializedList<BoundaryCondition<TridiagonalOperator>>(2); 
     59            BCs_ = new InitializedList<BoundaryCondition<IOperator>>(2); 
    6060        } 
    6161 
     
    123123 
    124124        protected void initializeBoundaryConditions() { 
    125             BCs_[0] = new NeumannBC(intrinsicValues_.value(1)- intrinsicValues_.value(0), NeumannBC.Side.Lower); 
    126             BCs_[1] = new NeumannBC(intrinsicValues_.value(intrinsicValues_.size()-1) - 
     125            BCs_[0] = new NeumannBC(intrinsicValues_.value(1) - intrinsicValues_.value(0), NeumannBC.Side.Lower); 
     126            BCs_[1] = new NeumannBC(intrinsicValues_.value(intrinsicValues_.size() - 1) - 
    127127                                    intrinsicValues_.value(intrinsicValues_.size()-2), 
    128128                                    NeumannBC.Side.Upper); 
  • trunk/QLNet/QLNet/QLNet.csproj

    r128 r131  
    159159    </Compile> 
    160160    <Compile Include="Math\Solvers1d\Secant.cs" /> 
     161    <Compile Include="Math\transformedgrid.cs" /> 
    161162    <Compile Include="Math\Vector.cs" /> 
    162163    <Compile Include="Methods\Finitedifferences\BoundaryCondition.cs" /> 
     164    <Compile Include="Methods\Finitedifferences\bsmoperator.cs" /> 
    163165    <Compile Include="Methods\Finitedifferences\cranknicolson.cs" /> 
    164166    <Compile Include="Methods\Finitedifferences\finitedifferencemodel.cs" /> 
    165167    <Compile Include="Methods\Finitedifferences\mixedscheme.cs" /> 
    166168    <Compile Include="Methods\Finitedifferences\OperatorFactory.cs" /> 
     169    <Compile Include="Methods\Finitedifferences\pde.cs" /> 
     170    <Compile Include="Methods\Finitedifferences\pdebsm.cs" /> 
     171    <Compile Include="Methods\Finitedifferences\pdeshortrate.cs" /> 
    167172    <Compile Include="Methods\Finitedifferences\StepCondition.cs" /> 
    168173    <Compile Include="Methods\Finitedifferences\TridiagonalOperator.cs" />