Changeset 155
- Timestamp:
- 05/08/08 19:29:30 (2 months ago)
- Files:
-
- trunk/QLNet/EquityOption/EquityOption.cs (modified) (2 diffs)
- trunk/QLNet/QLNet/Math/SampledCurve.cs (modified) (2 diffs)
- trunk/QLNet/QLNet/Methods/Finitedifferences/TridiagonalOperator.cs (modified) (3 diffs)
- trunk/QLNet/QLNet/Methods/Finitedifferences/bsmoperator.cs (modified) (1 diff)
- trunk/QLNet/QLNet/Methods/Finitedifferences/finitedifferencemodel.cs (modified) (4 diffs)
- trunk/QLNet/QLNet/Methods/Finitedifferences/mixedscheme.cs (modified) (7 diffs)
- trunk/QLNet/QLNet/Pricingengines/vanilla/FDEuropeanEngine.cs (modified) (1 diff)
- trunk/QLNet/QLNet/QLNet.csproj (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/QLNet/EquityOption/EquityOption.cs
r131 r155 32 32 Calendar calendar = new TARGET(); 33 33 Date todaysDate = new Date(15, Month.May, 1998); 34 Date settlementDate = new Date (17, Month.May, 1998);34 Date settlementDate = new Date(17, Month.May, 1998); 35 35 Settings.setEvaluationDate(todaysDate); 36 36 … … 86 86 VanillaOption bermudanOption = new VanillaOption(payoff, bermudanExercise); 87 87 VanillaOption americanOption = new VanillaOption(payoff, americanExercise); 88 88 89 89 90 // Analytic formulas: trunk/QLNet/QLNet/Math/SampledCurve.cs
r142 r155 39 39 40 40 public SampledCurve(Vector grid) { 41 grid_ = grid;41 grid_ = (Vector)grid.Clone(); 42 42 values_ = new Vector(grid.Count); 43 43 } … … 55 55 //! \name modifiers 56 56 public void setGrid(Vector g) { grid_ = (Vector)g.Clone(); } 57 public void setValues( Arrayg) { values_ = (Vector)g.Clone(); }57 public void setValues(Vector g) { values_ = (Vector)g.Clone(); } 58 58 59 59 public void sample(Func<double, double> f) { trunk/QLNet/QLNet/Methods/Finitedifferences/TridiagonalOperator.cs
r126 r155 24 24 25 25 namespace QLNet { 26 public interface IOperator {26 public interface IOperator : ICloneable { 27 27 int size(); 28 28 IOperator identity(int size); … … 84 84 // TridiagonalOperator(const Disposable<TridiagonalOperator>&); 85 85 // TridiagonalOperator& operator=(const Disposable<TridiagonalOperator>&); 86 public object Clone() { return this.MemberwiseClone(); } 86 87 87 88 public IOperator multiply(double a, IOperator o) { … … 126 127 // transform(InputIterator1 start1, InputIterator1 finish1, InputIterator2 start2, OutputIterator result, 127 128 // BinaryOperation binary_op) 128 for (int i = 0; i < diagonal_.Count; i++) 129 result[i] = diagonal_[i] * v[i]; 129 result = Utils.DirectMultiply(diagonal_, v); 130 130 131 131 // matricial product trunk/QLNet/QLNet/Methods/Finitedifferences/bsmoperator.cs
r131 r155 40 40 //PdeBSM::grid_type logGrid(grid); 41 41 LogGrid logGrid = new LogGrid(grid); 42 PdeConstantCoeff<PdeBSM> cc = 43 new PdeConstantCoeff<PdeBSM>(process, residualTime, process.stateVariable().link.value()); 42 var cc = new PdeConstantCoeff<PdeBSM>(process, residualTime, process.stateVariable().link.value()); 44 43 cc.generateOperator(residualTime, logGrid, this); 45 44 } trunk/QLNet/QLNet/Methods/Finitedifferences/finitedifferencemodel.cs
r131 r155 50 50 /*! solves the problem between the given times, applying a condition at every step. 51 51 \warning being this a rollback, <tt>from</tt> must be a later time than <tt>to</tt>. */ 52 public void rollback( Vector a, double from, double to, int steps) { rollbackImpl(a, from, to, steps, null); }53 public void rollback( Vector a, double from, double to, int steps, StepCondition condition) {54 rollbackImpl( a,from,to,steps, condition);52 public void rollback(ref Vector a, double from, double to, int steps) { rollbackImpl(ref a, from, to, steps, null); } 53 public void rollback(ref Vector a, double from, double to, int steps, StepCondition condition) { 54 rollbackImpl(ref a,from,to,steps, condition); 55 55 } 56 56 57 private void rollbackImpl( Vector a, double from, double to, int steps, StepCondition condition) {57 private void rollbackImpl(ref Vector a, double from, double to, int steps, StepCondition 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( a,now);74 evolver_.step(ref a,now); 75 75 if (condition != null) 76 76 condition.applyTo(a,stoppingTimes_[j]); … … 85 85 if (now > next) { 86 86 evolver_.setStep(now - next); 87 evolver_.step( a,now);87 evolver_.step(ref a,now); 88 88 if (condition != null) 89 89 condition.applyTo(a,next); … … 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( a,now);97 evolver_.step(ref a,now); 98 98 if (condition != null) 99 99 condition.applyTo(a, next); trunk/QLNet/QLNet/Methods/Finitedifferences/mixedscheme.cs
r131 r155 28 28 29 29 public interface IMixedScheme { 30 void step( Vector a, double t);30 void step(ref Vector a, double t); 31 31 void setStep(double dt); 32 32 } … … 40 40 typedef ... array_type; 41 41 42 // copy constructor/assignment43 // (these will be provided by the compiler if none is defined)44 Operator(const Operator&);45 Operator& operator=(const Operator&);46 47 // inspectors48 Size size();49 50 // modifiers51 void setTime(Time t);52 53 // operator interface54 array_type applyTo(const array_type&);55 array_type solveFor(const array_type&);56 static Operator identity(Size size);57 58 // operator algebra59 Operator operator*(Real, const Operator&);60 Operator operator+(const Operator&, const Operator&);61 Operator operator+(const Operator&, const Operator&);62 \endcode63 64 \warning The differential operator must be linear for65 this evolver to work.66 67 \todo68 - derive variable theta schemes69 - introduce multi time-level schemes.70 71 42 \ingroup findiff 72 43 */ 73 public class MixedScheme<Operator> : IMixedScheme where Operator : IOperator , new(){44 public class MixedScheme<Operator> : IMixedScheme where Operator : IOperator { 74 45 protected Operator L_, I_, explicitPart_, implicitPart_; 75 46 protected double dt_; … … 80 51 public MixedScheme() { } // required for generics 81 52 public MixedScheme(Operator L, double theta, List<BoundaryCondition<IOperator>> bcs) { 82 L_ = L;83 I_ = (Operator) (new Operator().identity(L.size()));53 L_ = (Operator)L.Clone(); 54 I_ = (Operator)L.identity(L.size()); 84 55 dt_ = 0.0; 85 56 theta_ = theta; … … 87 58 } 88 59 89 public void step( Vector a, double t) {60 public void step(ref Vector a, double t) { 90 61 int i; 91 62 for (i=0; i<bcs_.Count; i++) … … 94 65 if (L_.isTimeDependent()) { 95 66 L_.setTime(t); 96 explicitPart_ = (Operator) new Operator().subtract(I_, new Operator().multiply((1.0 - theta_) * dt_, L_));67 explicitPart_ = (Operator)L_.subtract(I_, L_.multiply((1.0 - theta_) * dt_, L_)); 97 68 } 98 69 for (i = 0; i < bcs_.Count; i++) … … 105 76 if (L_.isTimeDependent()) { 106 77 L_.setTime(t-dt_); 107 implicitPart_ = (Operator) new Operator().add(I_, new Operator().multiply(theta_ * dt_, L_));78 implicitPart_ = (Operator)L_.add(I_, L_.multiply(theta_ * dt_, L_)); 108 79 } 109 80 for (i = 0; i < bcs_.Count; i++) … … 118 89 dt_ = dt; 119 90 if (theta_!=1.0) // there is an explicit part 120 explicitPart_ = (Operator) new Operator().subtract(I_, new Operator().multiply((1.0 - theta_) * dt_, L_));91 explicitPart_ = (Operator)L_.subtract(I_, L_.multiply((1.0 - theta_) * dt_, L_)); 121 92 if (theta_!=0.0) // there is an implicit part 122 implicitPart_ = (Operator) new Operator().add(I_, new Operator().multiply(theta_ * dt_, L_));93 implicitPart_ = (Operator)L_.add(I_, L_.multiply(theta_ * dt_, L_)); 123 94 } 124 95 } trunk/QLNet/QLNet/Pricingengines/vanilla/FDEuropeanEngine.cs
r131 r155 52 52 var model = new FiniteDifferenceModel<CrankNicolson<TridiagonalOperator>>(finiteDifferenceOperator_, BCs_); 53 53 54 prices_ = intrinsicValues_;54 prices_ = (SampledCurve)intrinsicValues_.Clone(); 55 55 56 model.rollback(prices_.values(), getResidualTime(), 0, timeSteps_); 56 // this is a workaround for pointers to avoid unsafe code 57 // in the grid calculation Vector temp goes through many operations 58 Vector temp = prices_.values(); 59 model.rollback(ref temp, getResidualTime(), 0, timeSteps_); 60 prices_.setValues(temp); 57 61 58 62 results_.value = prices_.valueAtCenter(); trunk/QLNet/QLNet/QLNet.csproj
r145 r155 25 25 <ErrorReport>prompt</ErrorReport> 26 26 <WarningLevel>4</WarningLevel> 27 <AllowUnsafeBlocks>true</AllowUnsafeBlocks> 27 28 </PropertyGroup> 28 29 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">