Changeset 131
- Timestamp:
- 04/27/08 13:54:35 (7 months ago)
- Files:
-
- trunk/QLNet/EquityOption/EquityOption.cs (modified) (1 diff)
- trunk/QLNet/QLNet/Instruments/BarrierOption.cs (modified) (1 diff)
- trunk/QLNet/QLNet/Instruments/Instrument.cs (modified) (4 diffs)
- trunk/QLNet/QLNet/Instruments/MultiAssetOption.cs (modified) (1 diff)
- trunk/QLNet/QLNet/Instruments/OneAssetOption.cs (modified) (1 diff)
- trunk/QLNet/QLNet/Instruments/VanillaSwap.cs (modified) (2 diffs)
- trunk/QLNet/QLNet/Math/SampledCurve.cs (modified) (1 diff)
- trunk/QLNet/QLNet/Math/transformedgrid.cs (added)
- trunk/QLNet/QLNet/Methods/Finitedifferences/BoundaryCondition.cs (modified) (5 diffs)
- trunk/QLNet/QLNet/Methods/Finitedifferences/OperatorFactory.cs (modified) (1 diff)
- trunk/QLNet/QLNet/Methods/Finitedifferences/bsmoperator.cs (added)
- trunk/QLNet/QLNet/Methods/Finitedifferences/cranknicolson.cs (modified) (1 diff)
- trunk/QLNet/QLNet/Methods/Finitedifferences/finitedifferencemodel.cs (modified) (1 diff)
- trunk/QLNet/QLNet/Methods/Finitedifferences/mixedscheme.cs (modified) (1 diff)
- trunk/QLNet/QLNet/Methods/Finitedifferences/pde.cs (added)
- trunk/QLNet/QLNet/Methods/Finitedifferences/pdebsm.cs (added)
- trunk/QLNet/QLNet/Methods/Finitedifferences/pdeshortrate.cs (added)
- trunk/QLNet/QLNet/PricingEngine.cs (modified) (2 diffs)
- trunk/QLNet/QLNet/Pricingengines/vanilla/FDEuropeanEngine.cs (modified) (2 diffs)
- trunk/QLNet/QLNet/Pricingengines/vanilla/FDVanillaEngine.cs (modified) (3 diffs)
- trunk/QLNet/QLNet/QLNet.csproj (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/QLNet/EquityOption/EquityOption.cs
r126 r131 131 131 int timeSteps = 801; 132 132 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)); 135 134 //bermudanOption.setPricingEngine(boost::shared_ptr<PricingEngine>( 136 135 // new FDBermudanEngine(bsmProcess,timeSteps,timeSteps-1))); trunk/QLNet/QLNet/Instruments/BarrierOption.cs
r130 r131 42 42 public double? rebate; 43 43 44 public void validate()44 public override void validate() 45 45 { 46 46 base.validate(); trunk/QLNet/QLNet/Instruments/Instrument.cs
r130 r131 29 29 // The value of these attributes and any other that derived classes might declare must be set during calculation. 30 30 protected double? NPV_, errorEstimate_; 31 protected Dictionary<string, double> additionalResults_ = new Dictionary<string,double>();31 protected Dictionary<string, object> additionalResults_ = new Dictionary<string, object>(); 32 32 protected IPricingEngine engine_; 33 33 … … 108 108 109 109 // returns any additional result returned by the pricing engine. 110 public doubleresult(string tag)110 public object result(string tag) 111 111 { 112 112 calculate(); … … 122 122 123 123 // 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_; } 125 125 #endregion 126 126 … … 140 140 public double? value; 141 141 public double? errorEstimate; 142 public Dictionary<string, double> additionalResults = new Dictionary<string, double>();142 public Dictionary<string, object> additionalResults = new Dictionary<string, object>(); 143 143 144 144 public virtual void reset() trunk/QLNet/QLNet/Instruments/MultiAssetOption.cs
r130 r131 96 96 } 97 97 98 public void setupArguments(IPricingEngineArguments args)98 public override void setupArguments(IPricingEngineArguments args) 99 99 { 100 100 MultiAssetOption.Arguments arguments = args as MultiAssetOption.Arguments; trunk/QLNet/QLNet/Instruments/OneAssetOption.cs
r128 r131 144 144 145 145 146 147 146 //! %Results from single-asset option calculation 148 147 new public class Results : Instrument.Results { trunk/QLNet/QLNet/Instruments/VanillaSwap.cs
r130 r131 248 248 } 249 249 250 public void validate() {250 public override void validate() { 251 251 base.validate(); 252 252 … … 276 276 fairRate = fairSpread = null; 277 277 } 278 };279 280 new class Engine : GenericEngine<VanillaSwap.Arguments, VanillaSwap.Results> {281 public override void calculate() { throw new NotImplementedException(); }282 278 } 283 279 } trunk/QLNet/QLNet/Math/SampledCurve.cs
r110 r131 75 75 76 76 /*! \todo replace or complement with a more general function firstDerivativeAt(spot) */ 77 public double irstDerivativeAtCenter() {77 public double firstDerivativeAtCenter() { 78 78 if (!(size() >= 3)) 79 79 throw new ApplicationException("the size of the curve must be at least 3"); trunk/QLNet/QLNet/Methods/Finitedifferences/BoundaryCondition.cs
r126 r131 32 32 applied to an array \f$ u \f$ so that \f$ v = Lu \f$ will 33 33 satisfy the given condition. */ 34 public virtual void applyBeforeApplying( Operator o) { throw new NotSupportedException(); }34 public virtual void applyBeforeApplying(IOperator o) { throw new NotSupportedException(); } 35 35 36 36 /*! This method modifies an array \f$ u \f$ so that it satisfies the given condition. */ … … 40 40 system \f$ Lu' = u \f$ is solved so that \f$ u' \f$ will 41 41 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(); } 43 43 44 44 /*! This method modifies an array \f$ u \f$ so that it satisfies the given condition. */ … … 61 61 \ingroup findiff 62 62 */ 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> { 64 65 private double value_; 65 66 private Side side_; … … 71 72 72 73 // interface 73 public override void applyBeforeApplying(TridiagonalOperator L) { 74 public override void applyBeforeApplying(IOperator o) { 75 TridiagonalOperator L = o as TridiagonalOperator; 74 76 switch (side_) { 75 77 case Side.Lower: … … 97 99 } 98 100 99 public override void applyBeforeSolving(TridiagonalOperator L, Vector rhs) { 101 public override void applyBeforeSolving(IOperator o, Vector rhs) { 102 TridiagonalOperator L = o as TridiagonalOperator; 100 103 switch (side_) { 101 104 case Side.Lower: trunk/QLNet/QLNet/Methods/Finitedifferences/OperatorFactory.cs
r117 r131 32 32 double residualTime, bool timeDependent) { 33 33 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); 36 40 else 37 throw new NotImplementedException(); 38 //return BSMOperator(grid, process, residualTime); 41 return new BSMOperator(grid, process, residualTime); 39 42 } 40 43 public static TridiagonalOperator getOperator(OneFactorModel.ShortRateDynamics process, Vector grid) { 41 44 throw new NotImplementedException(); 42 //return OneFactorOperator(grid, process); 45 //! Interest-rate single factor model differential operator 46 //return new PdeOperator<PdeShortRate>(grid, process); 43 47 } 44 48 } trunk/QLNet/QLNet/Methods/Finitedifferences/cranknicolson.cs
r127 r131 60 60 class CrankNicolson<Operator> : MixedScheme<Operator>, ISchemeFactory where Operator : IOperator, new() { 61 61 // constructors 62 public CrankNicolson() { } // required for generics 62 63 public CrankNicolson(Operator L, List<BoundaryCondition<IOperator>> bcs) 63 64 : base(L, 0.5, bcs) { } trunk/QLNet/QLNet/Methods/Finitedifferences/finitedifferencemodel.cs
r127 r131 30 30 31 31 // 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>()) { } 34 34 public FiniteDifferenceModel(IOperator L, List<BoundaryCondition<IOperator>> bcs, List<double> stoppingTimes) { 35 35 evolver_ = (Evolver)new Evolver().factory(L, bcs); trunk/QLNet/QLNet/Methods/Finitedifferences/mixedscheme.cs
r127 r131 78 78 79 79 // constructors 80 public MixedScheme() { } // required for generics 80 81 public MixedScheme(Operator L, double theta, List<BoundaryCondition<IOperator>> bcs) { 81 82 L_ = L; trunk/QLNet/QLNet/PricingEngine.cs
r130 r131 43 43 } 44 44 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 45 77 //! abstract class for pricing engines 46 78 //public abstract class PricingEngine … … 73 105 // } 74 106 //} 75 76 // template base class for option pricing engines77 // 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 & Observable93 // observable interface94 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 #endregion106 };107 107 } trunk/QLNet/QLNet/Pricingengines/vanilla/FDEuropeanEngine.cs
r128 r131 29 29 checking it against analytic results. 30 30 */ 31 public class FDEuropeanEngine : OneAssetOption.Engine{31 public class FDEuropeanEngine : FDVanillaEngine, IGenericEngine<OneAssetOption.Arguments, OneAssetOption.Results> { 32 32 private SampledCurve prices_; 33 private FDVanillaEngine engine_;34 33 35 34 //public FDEuropeanEngine(GeneralizedBlackScholesProcess process, 36 35 // 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) { 39 40 prices_ = new SampledCurve(gridPoints); 40 41 … … 42 43 } 43 44 44 //new privatevoid 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(); 50 51 51 //var model = new FiniteDifferenceModel<CrankNicolson<TridiagonalOperator>>(finiteDifferenceOperator_, BCs_);52 var model = new FiniteDifferenceModel<CrankNicolson<TridiagonalOperator>>(finiteDifferenceOperator_, BCs_); 52 53 53 //prices_ = intrinsicValues_;54 prices_ = intrinsicValues_; 54 55 55 //model.rollback(prices_.values(), getResidualTime(), 0, timeSteps_);56 model.rollback(prices_.values(), getResidualTime(), 0, timeSteps_); 56 57 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 66 91 } 67 92 } trunk/QLNet/QLNet/Pricingengines/vanilla/FDVanillaEngine.cs
r130 r131 40 40 41 41 // typedef BoundaryCondition<TridiagonalOperator> bc_type; 42 protected List<BoundaryCondition< TridiagonalOperator>> BCs_;42 protected List<BoundaryCondition<IOperator>> BCs_; 43 43 // temporaries 44 44 protected double sMin_, center_, sMax_; … … 57 57 timeDependent_ = timeDependent; 58 58 intrinsicValues_ = new SampledCurve(gridPoints); 59 BCs_ = new InitializedList<BoundaryCondition< TridiagonalOperator>>(2);59 BCs_ = new InitializedList<BoundaryCondition<IOperator>>(2); 60 60 } 61 61 … … 123 123 124 124 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) - 127 127 intrinsicValues_.value(intrinsicValues_.size()-2), 128 128 NeumannBC.Side.Upper); trunk/QLNet/QLNet/QLNet.csproj
r128 r131 159 159 </Compile> 160 160 <Compile Include="Math\Solvers1d\Secant.cs" /> 161 <Compile Include="Math\transformedgrid.cs" /> 161 162 <Compile Include="Math\Vector.cs" /> 162 163 <Compile Include="Methods\Finitedifferences\BoundaryCondition.cs" /> 164 <Compile Include="Methods\Finitedifferences\bsmoperator.cs" /> 163 165 <Compile Include="Methods\Finitedifferences\cranknicolson.cs" /> 164 166 <Compile Include="Methods\Finitedifferences\finitedifferencemodel.cs" /> 165 167 <Compile Include="Methods\Finitedifferences\mixedscheme.cs" /> 166 168 <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" /> 167 172 <Compile Include="Methods\Finitedifferences\StepCondition.cs" /> 168 173 <Compile Include="Methods\Finitedifferences\TridiagonalOperator.cs" />