Assembla home | Assembla project page
 

Changeset 161

Show
Ignore:
Timestamp:
05/10/08 12:40:56 (1 year ago)
Author:
snovik
Message:

Fix: FD American Option: I believe it looks good now. Only one design problem is left that I know of

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/QLNet/QLNet/Pricingengines/vanilla/FDMultiPeriodEngine.cs

    r160 r161  
    2323 
    2424namespace QLNet { 
    25     public class FDMultiPeriodEngine : FDVanillaEngine { 
     25    public class FDMultiPeriodEngine : FDConditionEngineTemplate { 
    2626        protected List<Event> events_; 
    2727        protected List<double> stoppingTimes_; 
    2828        protected int timeStepPerPeriod_; 
    29         protected SampledCurve prices_; 
    30  
    31         protected IStepCondition<Vector> stepCondition_; 
    3229        protected FiniteDifferenceModel<CrankNicolson<TridiagonalOperator>> model_; 
    3330 
    3431        public FDMultiPeriodEngine() { }    // required for generics 
    35         public override IOptionPricingEngine factory(GeneralizedBlackScholesProcess process, 
    36                                                     int timeSteps, int gridPoints, bool timeDependent) { 
    37             return new FDMultiPeriodEngine(process, timeSteps, gridPoints, timeDependent); 
    38         } 
    3932 
    4033        //protected FDMultiPeriodEngine(GeneralizedBlackScholesProcess process, 
     
    5750        protected virtual void executeIntermediateStep(int step) { throw new NotSupportedException(); } 
    5851 
    59         protected virtual void initializeStepCondition() { 
     52        protected override void initializeStepCondition() { 
    6053            stepCondition_ = new NullCondition<Vector>(); 
    6154        } 
     
    6962        } 
    7063 
    71         #region IOptionPricingEngine 
    7264        public override void setupArguments(IPricingEngineArguments a) { 
    7365            base.setupArguments(a); 
     
    173165            results.additionalResults.Add("priceCurve", prices_); 
    174166        } 
    175  
    176         #endregion 
    177167    } 
    178168} 
  • trunk/QLNet/QLNet/Pricingengines/vanilla/FDStepConditionEngine.cs

    r160 r161  
    2424namespace QLNet { 
    2525    //! Finite-differences pricing engine for American-style vanilla options 
    26     public class FDStepConditionEngine : FDVanillaEngine { 
    27         protected IStepCondition<Vector> stepCondition_; 
    28         protected SampledCurve prices_; 
     26    public class FDStepConditionEngine : FDConditionEngineTemplate { 
    2927        protected TridiagonalOperator controlOperator_; 
    3028        protected List<BoundaryCondition<IOperator>> controlBCs_; 
     
    3331        // required for generics 
    3432        public FDStepConditionEngine() { } 
    35         public override IOptionPricingEngine factory(GeneralizedBlackScholesProcess process, 
    36                                                      int timeSteps, int gridPoints, bool timeDependent) { 
    37             return new FDStepConditionEngine(process, timeSteps, gridPoints, timeDependent); 
    38         } 
    39  
    4033 
    4134        //public FDStepConditionEngine(GeneralizedBlackScholesProcess process, int timeSteps, int gridPoints, 
     
    4639            controlPrices_ = new SampledCurve(gridPoints); 
    4740        } 
    48          
    49         protected virtual void initializeStepCondition() { } 
    5041 
    5142        public override void calculate(IPricingEngineResults r) { 
     
    114105                + black.gamma(spot); 
    115106            results.additionalResults.Add("priceCurve", prices_); 
    116         } 
    117  
     107        }  
    118108    } 
    119109} 
  • trunk/QLNet/QLNet/Pricingengines/vanilla/FDVanillaEngine.cs

    r160 r161  
    2424namespace QLNet { 
    2525    //! Finite-differences pricing engine for BSM one asset options 
    26     /*! The name is a misnomer as this is a base class for any finite 
    27         difference scheme.  Its main job is to handle grid layout. 
     26    /*! The name is a misnomer as this is a base class for any finite difference scheme.  Its main job is to handle grid layout. 
    2827 
    2928        \ingroup vanillaengines 
    3029    */ 
    31     public class FDVanillaEngine : IOptionPricingEngine
     30    public class FDVanillaEngine
    3231        protected GeneralizedBlackScholesProcess process_; 
    3332        protected int timeSteps_, gridPoints_; 
     
    4847        const double safetyZoneFactor_ = 1.1; 
    4948 
    50         // required for generics 
     49        // required for generics and template iheritance 
    5150        public FDVanillaEngine() { } 
    52         public virtual IOptionPricingEngine factory(GeneralizedBlackScholesProcess process, 
    53                                                     int timeSteps, int gridPoints, bool timeDependent) { 
     51        public virtual FDVanillaEngine factory(GeneralizedBlackScholesProcess process, 
     52                                               int timeSteps, int gridPoints, bool timeDependent) { 
    5453            return new FDVanillaEngine(process, timeSteps, gridPoints, timeDependent); 
    5554        } 
     
    141140        } 
    142141 
    143         #region IOptionPricingEngine 
    144142        public virtual void setupArguments(IPricingEngineArguments a) { 
    145143            OneAssetOption.Arguments args = a as OneAssetOption.Arguments; 
     
    151149        } 
    152150        public virtual void calculate(IPricingEngineResults r) { throw new NotSupportedException(); } 
    153         #endregion 
    154151    } 
    155152 
    156153 
    157     public interface IOptionPricingEngine { 
    158         void calculate(IPricingEngineResults r); 
    159         void setupArguments(IPricingEngineArguments a); 
    160         IOptionPricingEngine factory(GeneralizedBlackScholesProcess process, int timeSteps, int gridPoints, bool timeDependent); 
    161     } 
    162  
    163154    public class FDEngineAdapter<Base, Engine, ArgumentsType, ResultsType> : FDVanillaEngine 
    164         where Base : IOptionPricingEngine, new() 
     155        where Base : FDVanillaEngine, new() 
    165156        where Engine : IGenericEngine<ArgumentsType, ResultsType> 
    166157        where ArgumentsType : IPricingEngineArguments, new() 
    167158        where ResultsType : IPricingEngineResults, new() { 
    168159 
     160        // a wrap-up of base engine 
    169161        Base optionBase; 
    170162 
  • trunk/QLNet/QLNet/Pricingengines/vanilla/fdconditions.cs

    r160 r161  
    2323 
    2424namespace QLNet { 
    25     public class FDAmericanCondition<baseEngine> : FDVanillaEngine, IOptionPricingEngine  
    26             where baseEngine : IOptionPricingEngine, new() { 
    27         baseEngine engine_; 
     25    // this is template version to serve as base for FDStepConditionEngine and FDMultiPeriodEngine 
     26    public class FDConditionEngineTemplate : FDVanillaEngine { 
     27        #region Common definitions for deriving classes 
     28        protected IStepCondition<Vector> stepCondition_; 
     29        protected SampledCurve prices_; 
     30        protected virtual void initializeStepCondition() { throw new NotSupportedException(); } 
     31        #endregion 
    2832 
    2933        // required for generics 
    30         public FDAmericanCondition() { } 
    31         public override IOptionPricingEngine factory(GeneralizedBlackScholesProcess process, 
     34        public FDConditionEngineTemplate() { } 
     35 
     36        public FDConditionEngineTemplate(GeneralizedBlackScholesProcess process, int timeSteps, int gridPoints, bool timeDependent) 
     37            : base(process, timeSteps, gridPoints, timeDependent) { } 
     38    } 
     39 
     40    // this is template version to serve as base for FDAmericanCondition and FDShoutCondition 
     41    public class FDConditionTemplate<baseEngine> : FDConditionEngineTemplate where baseEngine : FDVanillaEngine, new() { 
     42        #region Common definitions for deriving classes 
     43        protected baseEngine engine_; 
     44        public override FDVanillaEngine factory(GeneralizedBlackScholesProcess process, 
    3245                                            int timeSteps, int gridPoints, bool timeDependent) { 
    3346            engine_ = (baseEngine)new baseEngine().factory(process, timeSteps, gridPoints, timeDependent); 
     
    3548        } 
    3649 
     50        // below is a wrap-up of baseEngine instead of c++ template inheritance 
     51        public override void setupArguments(IPricingEngineArguments a) { engine_.setupArguments(a); } 
     52        public override void calculate(IPricingEngineResults r) { engine_.calculate(r); } 
     53        #endregion 
     54 
     55        // required for generics 
     56        public FDConditionTemplate() { } 
     57 
     58        public FDConditionTemplate(GeneralizedBlackScholesProcess process, int timeSteps, int gridPoints, bool timeDependent) 
     59            : base(process, timeSteps, gridPoints, timeDependent) { 
     60            // init engine 
     61            factory(process, timeSteps, gridPoints, timeDependent); 
     62        } 
     63    } 
     64 
     65 
     66    public class FDAmericanCondition<baseEngine> : FDConditionTemplate<baseEngine> 
     67            where baseEngine : FDVanillaEngine, new() { 
     68 
     69        // required for generics 
     70        public FDAmericanCondition() { } 
     71 
    3772        //public FDAmericanCondition(GeneralizedBlackScholesProcess process, 
    3873        //     int timeSteps = 100, int gridPoints = 100, bool timeDependent = false) 
    39         public FDAmericanCondition(GeneralizedBlackScholesProcess process, int timeSteps, int gridPoints, bool timeDependent) { 
     74        public FDAmericanCondition(GeneralizedBlackScholesProcess process, int timeSteps, int gridPoints, bool timeDependent) 
     75            : base(process, timeSteps, gridPoints, timeDependent) { } 
     76 
     77        protected override void initializeStepCondition() { 
     78            stepCondition_ = new AmericanCondition(intrinsicValues_.values()); 
    4079        } 
     80    } 
    4181 
    42         protected void initializeStepCondition() { 
    43             // stepCondition_ = new AmericanCondition(intrinsicValues_.values()); 
    44         } 
    4582 
    46         public override void setupArguments(IPricingEngineArguments a) { 
    47             engine_.setupArguments(a); 
    48         } 
    49         public override void calculate(IPricingEngineResults r) { 
    50             engine_.calculate(r); 
     83    public class FDShoutCondition<baseEngine> : FDConditionTemplate<baseEngine> 
     84            where baseEngine : FDVanillaEngine, new() { 
     85 
     86        // required for generics 
     87        public FDShoutCondition() { } 
     88 
     89        //public FDShoutCondition(GeneralizedBlackScholesProcess process, 
     90        //        Size timeSteps = 100, Size gridPoints = 100, bool timeDependent = false) 
     91        public FDShoutCondition(GeneralizedBlackScholesProcess process, int timeSteps, int gridPoints, bool timeDependent) 
     92            : base(process, timeSteps, gridPoints, timeDependent) { } 
     93         
     94        protected override void initializeStepCondition() { 
     95            double residualTime = getResidualTime(); 
     96            double riskFreeRate = process_.riskFreeRate().link.zeroRate(residualTime, Compounding.Continuous).rate(); 
     97 
     98            stepCondition_ = new ShoutCondition(intrinsicValues_.values(), residualTime, riskFreeRate); 
    5199        } 
    52100    } 
  • trunk/QLNet/QLNet/QLNet.csproj

    r159 r161  
    194194    <Compile Include="Methods\Finitedifferences\pdebsm.cs" /> 
    195195    <Compile Include="Methods\Finitedifferences\pdeshortrate.cs" /> 
     196    <Compile Include="Methods\Finitedifferences\ShoutCondition.cs" /> 
    196197    <Compile Include="Methods\Finitedifferences\StepCondition.cs" /> 
    197198    <Compile Include="Methods\Finitedifferences\TridiagonalOperator.cs" />