Assembla home | Assembla project page
 

Changeset 155

Show
Ignore:
Timestamp:
05/08/08 19:29:30 (2 months ago)
Author:
snovik
Message:

Fix: Finite Differences for European Option (I cracked it!!!)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/QLNet/EquityOption/EquityOption.cs

    r131 r155  
    3232            Calendar calendar = new TARGET(); 
    3333            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); 
    3535            Settings.setEvaluationDate(todaysDate); 
    3636 
     
    8686            VanillaOption bermudanOption = new VanillaOption(payoff, bermudanExercise); 
    8787            VanillaOption americanOption = new VanillaOption(payoff, americanExercise); 
     88 
    8889 
    8990            // Analytic formulas: 
  • trunk/QLNet/QLNet/Math/SampledCurve.cs

    r142 r155  
    3939 
    4040        public SampledCurve(Vector grid) { 
    41             grid_ = grid
     41            grid_ = (Vector)grid.Clone()
    4242            values_ = new Vector(grid.Count); 
    4343        } 
     
    5555        //! \name modifiers 
    5656        public void setGrid(Vector g) { grid_ = (Vector)g.Clone(); } 
    57         public void setValues(Array g) { values_ = (Vector)g.Clone(); } 
     57        public void setValues(Vector g) { values_ = (Vector)g.Clone(); } 
    5858 
    5959        public void sample(Func<double, double> f) { 
  • trunk/QLNet/QLNet/Methods/Finitedifferences/TridiagonalOperator.cs

    r126 r155  
    2424 
    2525namespace QLNet { 
    26     public interface IOperator
     26    public interface IOperator : ICloneable
    2727        int size(); 
    2828        IOperator identity(int size); 
     
    8484        // TridiagonalOperator(const Disposable<TridiagonalOperator>&); 
    8585        // TridiagonalOperator& operator=(const Disposable<TridiagonalOperator>&); 
     86        public object Clone() { return this.MemberwiseClone(); } 
    8687 
    8788        public IOperator multiply(double a, IOperator o) { 
     
    126127            // transform(InputIterator1 start1, InputIterator1 finish1, InputIterator2 start2, OutputIterator result, 
    127128            // 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); 
    130130 
    131131            // matricial product 
  • trunk/QLNet/QLNet/Methods/Finitedifferences/bsmoperator.cs

    r131 r155  
    4040            //PdeBSM::grid_type  logGrid(grid); 
    4141            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()); 
    4443            cc.generateOperator(residualTime, logGrid, this); 
    4544        } 
  • trunk/QLNet/QLNet/Methods/Finitedifferences/finitedifferencemodel.cs

    r131 r155  
    5050        /*! solves the problem between the given times, applying a condition at every step. 
    5151            \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); 
    5555        } 
    5656 
    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) { 
    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(a,now); 
     74                        evolver_.step(ref a,now); 
    7575                        if (condition != null) 
    7676                            condition.applyTo(a,stoppingTimes_[j]); 
     
    8585                    if (now > next) { 
    8686                        evolver_.setStep(now - next); 
    87                         evolver_.step(a,now); 
     87                        evolver_.step(ref a,now); 
    8888                        if (condition != null) 
    8989                            condition.applyTo(a,next); 
     
    9595                    // if we didn't, the evolver is already set to the 
    9696                    // default step, which is ok for us. 
    97                     evolver_.step(a,now); 
     97                    evolver_.step(ref a,now); 
    9898                    if (condition != null) 
    9999                        condition.applyTo(a, next); 
  • trunk/QLNet/QLNet/Methods/Finitedifferences/mixedscheme.cs

    r131 r155  
    2828 
    2929    public interface IMixedScheme { 
    30         void step(Vector a, double t); 
     30        void step(ref Vector a, double t); 
    3131        void setStep(double dt); 
    3232    } 
     
    4040        typedef ... array_type; 
    4141 
    42         // copy constructor/assignment 
    43         // (these will be provided by the compiler if none is defined) 
    44         Operator(const Operator&); 
    45         Operator& operator=(const Operator&); 
    46  
    47         // inspectors 
    48         Size size(); 
    49  
    50         // modifiers 
    51         void setTime(Time t); 
    52  
    53         // operator interface 
    54         array_type applyTo(const array_type&); 
    55         array_type solveFor(const array_type&); 
    56         static Operator identity(Size size); 
    57  
    58         // operator algebra 
    59         Operator operator*(Real, const Operator&); 
    60         Operator operator+(const Operator&, const Operator&); 
    61         Operator operator+(const Operator&, const Operator&); 
    62         \endcode 
    63  
    64         \warning The differential operator must be linear for 
    65                  this evolver to work. 
    66  
    67         \todo 
    68         - derive variable theta schemes 
    69         - introduce multi time-level schemes. 
    70  
    7142        \ingroup findiff 
    7243    */ 
    73     public class MixedScheme<Operator> : IMixedScheme where Operator : IOperator, new()
     44    public class MixedScheme<Operator> : IMixedScheme where Operator : IOperator
    7445        protected Operator L_, I_, explicitPart_, implicitPart_; 
    7546        protected double dt_; 
     
    8051        public MixedScheme() { }  // required for generics 
    8152        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()); 
    8455            dt_ = 0.0; 
    8556            theta_ = theta; 
     
    8758        } 
    8859 
    89         public void step(Vector a, double t) { 
     60        public void step(ref Vector a, double t) { 
    9061            int i; 
    9162            for (i=0; i<bcs_.Count; i++) 
     
    9465                if (L_.isTimeDependent()) { 
    9566                    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_)); 
    9768                } 
    9869                for (i = 0; i < bcs_.Count; i++) 
     
    10576                if (L_.isTimeDependent()) { 
    10677                    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_)); 
    10879                } 
    10980                for (i = 0; i < bcs_.Count; i++) 
     
    11889            dt_ = dt; 
    11990            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_)); 
    12192            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_)); 
    12394        } 
    12495    } 
  • trunk/QLNet/QLNet/Pricingengines/vanilla/FDEuropeanEngine.cs

    r131 r155  
    5252            var model = new FiniteDifferenceModel<CrankNicolson<TridiagonalOperator>>(finiteDifferenceOperator_, BCs_); 
    5353 
    54             prices_ = intrinsicValues_
     54            prices_ = (SampledCurve)intrinsicValues_.Clone()
    5555 
    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); 
    5761 
    5862            results_.value = prices_.valueAtCenter(); 
  • trunk/QLNet/QLNet/QLNet.csproj

    r145 r155  
    2525    <ErrorReport>prompt</ErrorReport> 
    2626    <WarningLevel>4</WarningLevel> 
     27    <AllowUnsafeBlocks>true</AllowUnsafeBlocks> 
    2728  </PropertyGroup> 
    2829  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">