Changeset 164
- Timestamp:
- 05/13/08 20:14:42 (4 months ago)
- Files:
-
- trunk/QLNet/QLNet/Methods/Finitedifferences/ParallelEvolver.cs (modified) (3 diffs)
- trunk/QLNet/QLNet/Methods/Finitedifferences/finitedifferencemodel.cs (modified) (4 diffs)
- trunk/QLNet/QLNet/Pricingengines/vanilla/FDMultiPeriodEngine.cs (modified) (1 diff)
- trunk/QLNet/QLNet/Pricingengines/vanilla/FDStepConditionEngine.cs (modified) (1 diff)
- trunk/QLNet/QLNet/Pricingengines/vanilla/FDVanillaEngine.cs (modified) (3 diffs)
- trunk/QLNet/QLNet/Pricingengines/vanilla/fdconditions.cs (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/QLNet/QLNet/Methods/Finitedifferences/ParallelEvolver.cs
r159 r164 23 23 24 24 namespace 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 */ 26 35 /*! \ingroup findiff */ 27 36 … … 29 38 where array_type : Vector { 30 39 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; 32 41 for (int i=0; i < Count; i++) { 33 42 this[i].applyTo(a[i], t); … … 51 60 52 61 public void step(ref object o, double t) { 53 List<Vector> a = o as List<Vector>;62 List<Vector> a = (List<Vector>)o; 54 63 for (int i=0; i < evolvers_.Count; i++) { 55 64 object temp = a[i]; trunk/QLNet/QLNet/Methods/Finitedifferences/finitedifferencemodel.cs
r159 r164 55 55 } 56 56 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) { 58 58 59 59 if (!(from >= to)) throw new ApplicationException("trying to roll back from " + from + " to " + to); … … 72 72 // perform a small step to stoppingTimes_[j]... 73 73 evolver_.setStep(now-stoppingTimes_[j]); 74 evolver_.step(ref a, now);74 evolver_.step(ref o, now); 75 75 if (condition != null) 76 condition.applyTo( a,stoppingTimes_[j]);76 condition.applyTo(o,stoppingTimes_[j]); 77 77 // ...and continue the cycle 78 78 now = stoppingTimes_[j]; … … 85 85 if (now > next) { 86 86 evolver_.setStep(now - next); 87 evolver_.step(ref a,now);87 evolver_.step(ref o,now); 88 88 if (condition != null) 89 condition.applyTo( a,next);89 condition.applyTo(o,next); 90 90 } 91 91 // ...and in any case, we have to reset the … … 95 95 // if we didn't, the evolver is already set to the 96 96 // default step, which is ok for us. 97 evolver_.step(ref a,now);97 evolver_.step(ref o,now); 98 98 if (condition != null) 99 condition.applyTo( a, next);99 condition.applyTo(o, next); 100 100 } 101 101 } trunk/QLNet/QLNet/Pricingengines/vanilla/FDMultiPeriodEngine.cs
r162 r164 51 51 protected virtual void executeIntermediateStep(int step) { throw new NotSupportedException(); } 52 52 53 protected override void initializeStepCondition() {53 protected void initializeStepConditionImpl() { 54 54 stepCondition_ = new NullCondition<Vector>(); 55 55 } trunk/QLNet/QLNet/Pricingengines/vanilla/FDStepConditionEngine.cs
r162 r164 85 85 arraySet = (List<Vector>)temp; 86 86 87 prices_.set Grid(arraySet[0]);87 prices_.setValues(arraySet[0]); 88 88 controlPrices_.setValues(arraySet[1]); 89 89 trunk/QLNet/QLNet/Pricingengines/vanilla/FDVanillaEngine.cs
r162 r164 36 36 protected Payoff payoff_; 37 37 protected TridiagonalOperator finiteDifferenceOperator_; 38 p rotectedSampledCurve intrinsicValues_;38 public SampledCurve intrinsicValues_; 39 39 40 40 // typedef BoundaryCondition<TridiagonalOperator> bc_type; … … 49 49 // required for generics and template iheritance 50 50 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 52 53 public virtual FDVanillaEngine factory(GeneralizedBlackScholesProcess process, 53 int timeSteps, int gridPoints, bool timeDependent) {54 int timeSteps, int gridPoints, bool timeDependent) { 54 55 return new FDVanillaEngine(process, timeSteps, gridPoints, timeDependent); 55 56 } … … 155 156 public class FDEngineAdapter<Base, Engine, ArgumentsType, ResultsType> 156 157 : FDVanillaEngine, IGenericEngine<ArgumentsType, ResultsType> 157 where Base : FD VanillaEngine, new()158 where Base : FDConditionEngineTemplate, new() 158 159 where Engine : IGenericEngine<ArgumentsType, ResultsType> 159 160 where ArgumentsType : IPricingEngineArguments, new() trunk/QLNet/QLNet/Pricingengines/vanilla/fdconditions.cs
r161 r164 28 28 protected IStepCondition<Vector> stepCondition_; 29 29 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 } 31 42 #endregion 32 43 … … 36 47 public FDConditionEngineTemplate(GeneralizedBlackScholesProcess process, int timeSteps, int gridPoints, bool timeDependent) 37 48 : base(process, timeSteps, gridPoints, timeDependent) { } 49 38 50 } 39 51 40 52 // 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() { 42 55 #region Common definitions for deriving classes 43 56 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 }49 57 50 58 // below is a wrap-up of baseEngine instead of c++ template inheritance … … 59 67 : base(process, timeSteps, gridPoints, timeDependent) { 60 68 // init engine 61 factory(process, timeSteps, gridPoints, timeDependent);69 engine_ = (baseEngine)new baseEngine().factory(process, timeSteps, gridPoints, timeDependent); 62 70 } 63 71 } … … 65 73 66 74 public class FDAmericanCondition<baseEngine> : FDConditionTemplate<baseEngine> 67 where baseEngine : FD VanillaEngine, new() {75 where baseEngine : FDConditionEngineTemplate, new() { 68 76 69 77 // required for generics 70 78 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 } 71 84 72 85 //public FDAmericanCondition(GeneralizedBlackScholesProcess process, 73 86 // int timeSteps = 100, int gridPoints = 100, bool timeDependent = false) 74 87 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 } 76 91 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()); 79 97 } 80 98 } … … 82 100 83 101 public class FDShoutCondition<baseEngine> : FDConditionTemplate<baseEngine> 84 where baseEngine : FD VanillaEngine, new() {102 where baseEngine : FDConditionEngineTemplate, new() { 85 103 86 104 // required for generics 87 105 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 } 88 111 89 112 //public FDShoutCondition(GeneralizedBlackScholesProcess process, … … 91 114 public FDShoutCondition(GeneralizedBlackScholesProcess process, int timeSteps, int gridPoints, bool timeDependent) 92 115 : 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() { 95 123 double residualTime = getResidualTime(); 96 124 double riskFreeRate = process_.riskFreeRate().link.zeroRate(residualTime, Compounding.Continuous).rate(); 97 125 98 stepCondition_ = new ShoutCondition(intrinsicValues_.values(), residualTime, riskFreeRate); 126 //stepCondition_ = new ShoutCondition(intrinsicValues_.values(), residualTime, riskFreeRate); 127 return new ShoutCondition(intrinsicValues_.values(), residualTime, riskFreeRate); 99 128 } 100 129 }