Assembla home | Assembla project page
 

Changeset 164

Show
Ignore:
Timestamp:
05/13/08 20:14:42 (4 months ago)
Author:
snovik
Message:

Fix: American Option Finite Difference calc finally runs ok on the first test

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/QLNet/QLNet/Methods/Finitedifferences/ParallelEvolver.cs

    r159 r164  
    2323 
    2424namespace QLNet { 
    25     //! Parallel evolver for multiple arrays 
     25    /*! \brief Parallel evolver for multiple arrays 
     26 
     27        This class takes the evolver class and creates a new class which evolves 
     28        each of the evolvers in parallel.  Part of what this does is to take the  
     29        types for each evolver class and then wrapper them so that they create 
     30        new types which are sets of the old types. 
     31 
     32        This class is intended to be run in situations where there are parallel 
     33        differential equations such as with some convertible bond models. 
     34    */ 
    2635    /*! \ingroup findiff */ 
    2736 
     
    2938        where array_type : Vector { 
    3039        public void applyTo(object o, double t) { 
    31             List<IStepCondition<array_type>> a = o as List<IStepCondition<array_type>>
     40            List<array_type> a = (List<array_type>)o
    3241            for (int i=0; i < Count; i++) { 
    3342                this[i].applyTo(a[i], t); 
     
    5160 
    5261        public void step(ref object o, double t) { 
    53             List<Vector> a = o as List<Vector>
     62            List<Vector> a = (List<Vector>)o
    5463            for (int i=0; i < evolvers_.Count; i++) { 
    5564                object temp = a[i]; 
  • trunk/QLNet/QLNet/Methods/Finitedifferences/finitedifferencemodel.cs

    r159 r164  
    5555        } 
    5656 
    57         private void rollbackImpl(ref object a, double from, double to, int steps, IStepCondition<Vector> condition) { 
     57        private void rollbackImpl(ref object o, double from, double to, int steps, IStepCondition<Vector> condition) { 
    5858 
    5959            if (!(from >= to)) throw new ApplicationException("trying to roll back from " + from + " to " + to); 
     
    7272                        // perform a small step to stoppingTimes_[j]... 
    7373                        evolver_.setStep(now-stoppingTimes_[j]); 
    74                         evolver_.step(ref a, now); 
     74                        evolver_.step(ref o, now); 
    7575                        if (condition != null) 
    76                             condition.applyTo(a,stoppingTimes_[j]); 
     76                            condition.applyTo(o,stoppingTimes_[j]); 
    7777                        // ...and continue the cycle 
    7878                        now = stoppingTimes_[j]; 
     
    8585                    if (now > next) { 
    8686                        evolver_.setStep(now - next); 
    87                         evolver_.step(ref a,now); 
     87                        evolver_.step(ref o,now); 
    8888                        if (condition != null) 
    89                             condition.applyTo(a,next); 
     89                            condition.applyTo(o,next); 
    9090                    } 
    9191                    // ...and in any case, we have to reset the 
     
    9595                    // if we didn't, the evolver is already set to the 
    9696                    // default step, which is ok for us. 
    97                     evolver_.step(ref a,now); 
     97                    evolver_.step(ref o,now); 
    9898                    if (condition != null) 
    99                         condition.applyTo(a, next); 
     99                        condition.applyTo(o, next); 
    100100                } 
    101101            } 
  • trunk/QLNet/QLNet/Pricingengines/vanilla/FDMultiPeriodEngine.cs

    r162 r164  
    5151        protected virtual void executeIntermediateStep(int step) { throw new NotSupportedException(); } 
    5252 
    53         protected override void initializeStepCondition() { 
     53        protected void initializeStepConditionImpl() { 
    5454            stepCondition_ = new NullCondition<Vector>(); 
    5555        } 
  • trunk/QLNet/QLNet/Pricingengines/vanilla/FDStepConditionEngine.cs

    r162 r164  
    8585            arraySet = (List<Vector>)temp; 
    8686 
    87             prices_.setGrid(arraySet[0]); 
     87            prices_.setValues(arraySet[0]); 
    8888            controlPrices_.setValues(arraySet[1]); 
    8989 
  • trunk/QLNet/QLNet/Pricingengines/vanilla/FDVanillaEngine.cs

    r162 r164  
    3636        protected Payoff payoff_; 
    3737        protected TridiagonalOperator finiteDifferenceOperator_; 
    38         protected SampledCurve intrinsicValues_; 
     38        public SampledCurve intrinsicValues_; 
    3939 
    4040        // typedef BoundaryCondition<TridiagonalOperator> bc_type; 
     
    4949        // required for generics and template iheritance 
    5050        public FDVanillaEngine() { } 
    51         // this should be overridden in each deriving class using template iheritance in order to return a proper class to wrap 
     51        // this should be defined as new in each deriving class which use template iheritance  
     52        // in order to return a proper class to wrap 
    5253        public virtual FDVanillaEngine factory(GeneralizedBlackScholesProcess process, 
    53                                                int timeSteps, int gridPoints, bool timeDependent) { 
     54                                       int timeSteps, int gridPoints, bool timeDependent) { 
    5455            return new FDVanillaEngine(process, timeSteps, gridPoints, timeDependent); 
    5556        } 
     
    155156    public class FDEngineAdapter<Base, Engine, ArgumentsType, ResultsType> 
    156157        : FDVanillaEngine, IGenericEngine<ArgumentsType, ResultsType> 
    157         where Base : FDVanillaEngine, new() 
     158        where Base : FDConditionEngineTemplate, new() 
    158159        where Engine : IGenericEngine<ArgumentsType, ResultsType> 
    159160        where ArgumentsType : IPricingEngineArguments, new() 
  • trunk/QLNet/QLNet/Pricingengines/vanilla/fdconditions.cs

    r161 r164  
    2828        protected IStepCondition<Vector> stepCondition_; 
    2929        protected SampledCurve prices_; 
    30         protected virtual void initializeStepCondition() { throw new NotSupportedException(); } 
     30        protected void initializeStepCondition() { 
     31            if (stepConditionImpl_ == null) 
     32                throw new NotSupportedException(); 
     33            else 
     34                stepCondition_ = stepConditionImpl_(); 
     35        } 
     36 
     37        public delegate IStepCondition<Vector> stepConditionImpl(); 
     38        protected stepConditionImpl stepConditionImpl_; 
     39        public void setStepCondition(stepConditionImpl impl) { 
     40            stepConditionImpl_ = impl; 
     41        } 
    3142        #endregion 
    3243 
     
    3647        public FDConditionEngineTemplate(GeneralizedBlackScholesProcess process, int timeSteps, int gridPoints, bool timeDependent) 
    3748            : base(process, timeSteps, gridPoints, timeDependent) { } 
     49 
    3850    } 
    3951 
    4052    // this is template version to serve as base for FDAmericanCondition and FDShoutCondition 
    41     public class FDConditionTemplate<baseEngine> : FDConditionEngineTemplate where baseEngine : FDVanillaEngine, new() { 
     53    public class FDConditionTemplate<baseEngine> : FDConditionEngineTemplate 
     54            where baseEngine : FDConditionEngineTemplate, new() { 
    4255        #region Common definitions for deriving classes 
    4356        protected baseEngine engine_; 
    44         public override FDVanillaEngine factory(GeneralizedBlackScholesProcess process, 
    45                                             int timeSteps, int gridPoints, bool timeDependent) { 
    46             engine_ = (baseEngine)new baseEngine().factory(process, timeSteps, gridPoints, timeDependent); 
    47             return engine_; 
    48         } 
    4957 
    5058        // below is a wrap-up of baseEngine instead of c++ template inheritance 
     
    5967            : base(process, timeSteps, gridPoints, timeDependent) { 
    6068            // init engine 
    61             factory(process, timeSteps, gridPoints, timeDependent); 
     69            engine_ = (baseEngine)new baseEngine().factory(process, timeSteps, gridPoints, timeDependent); 
    6270        } 
    6371    } 
     
    6573 
    6674    public class FDAmericanCondition<baseEngine> : FDConditionTemplate<baseEngine> 
    67             where baseEngine : FDVanillaEngine, new() { 
     75            where baseEngine : FDConditionEngineTemplate, new() { 
    6876 
    6977        // required for generics 
    7078        public FDAmericanCondition() { } 
     79        // required for template inheritance 
     80        public override FDVanillaEngine factory(GeneralizedBlackScholesProcess process, 
     81                                                int timeSteps, int gridPoints, bool timeDependent) { 
     82            return new FDAmericanCondition<baseEngine>(process, timeSteps, gridPoints, timeDependent); 
     83        } 
    7184 
    7285        //public FDAmericanCondition(GeneralizedBlackScholesProcess process, 
    7386        //     int timeSteps = 100, int gridPoints = 100, bool timeDependent = false) 
    7487        public FDAmericanCondition(GeneralizedBlackScholesProcess process, int timeSteps, int gridPoints, bool timeDependent) 
    75             : base(process, timeSteps, gridPoints, timeDependent) { } 
     88            : base(process, timeSteps, gridPoints, timeDependent) { 
     89            engine_.setStepCondition(initializeStepConditionImpl); 
     90        } 
    7691 
    77         protected override void initializeStepCondition() { 
    78             stepCondition_ = new AmericanCondition(intrinsicValues_.values()); 
     92        //protected override void initializeStepCondition() { 
     93        //    stepCondition_ = new AmericanCondition(intrinsicValues_.values()); 
     94        //} 
     95        protected IStepCondition<Vector> initializeStepConditionImpl() { 
     96            return new AmericanCondition(engine_.intrinsicValues_.values()); 
    7997        } 
    8098    } 
     
    82100 
    83101    public class FDShoutCondition<baseEngine> : FDConditionTemplate<baseEngine> 
    84             where baseEngine : FDVanillaEngine, new() { 
     102            where baseEngine : FDConditionEngineTemplate, new() { 
    85103 
    86104        // required for generics 
    87105        public FDShoutCondition() { } 
     106        // required for template inheritance 
     107        new public FDVanillaEngine factory(GeneralizedBlackScholesProcess process, 
     108                                           int timeSteps, int gridPoints, bool timeDependent) { 
     109            return new FDShoutCondition<baseEngine>(process, timeSteps, gridPoints, timeDependent); 
     110        } 
    88111 
    89112        //public FDShoutCondition(GeneralizedBlackScholesProcess process, 
     
    91114        public FDShoutCondition(GeneralizedBlackScholesProcess process, int timeSteps, int gridPoints, bool timeDependent) 
    92115            : base(process, timeSteps, gridPoints, timeDependent) { } 
    93          
    94         protected override void initializeStepCondition() { 
     116 
     117        //protected override void initializeStepCondition() { 
     118        //    double residualTime = getResidualTime(); 
     119        //    double riskFreeRate = process_.riskFreeRate().link.zeroRate(residualTime, Compounding.Continuous).rate(); 
     120        //    stepCondition_ = new ShoutCondition(intrinsicValues_.values(), residualTime, riskFreeRate); 
     121        //} 
     122        protected IStepCondition<Vector> initializeStepConditionImpl() { 
    95123            double residualTime = getResidualTime(); 
    96124            double riskFreeRate = process_.riskFreeRate().link.zeroRate(residualTime, Compounding.Continuous).rate(); 
    97125 
    98             stepCondition_ = new ShoutCondition(intrinsicValues_.values(), residualTime, riskFreeRate); 
     126            //stepCondition_ = new ShoutCondition(intrinsicValues_.values(), residualTime, riskFreeRate); 
     127            return new ShoutCondition(intrinsicValues_.values(), residualTime, riskFreeRate); 
    99128        } 
    100129    }