Quantcast
Channel: Computer Aided Finance - Excel, Matlab, Theta Suite etc. » ThetaML
Viewing all articles
Browse latest Browse all 10

How can I create a Matlab Class for generating a Stochastic Process for ThetaML?

$
0
0

Since version 2008a, the Matlab m-language is extended to include some object orientation. This object-oriented programming style allows reuse, inheritance, encapsulation, and reference behavior.

Example

The following m-code is a generic abstract class for implementing classes which generate processes accessible in ThetaML.

classdef ThetaStepping < handle
 % abstract class which defines the required methods of a class
 % generating a stochastic process for usage in ThetaML with
 % Theta Suite
 % (c) 2011 Andreas J. Grau

   methods (Abstract)
     vars = GetModelVariables(ThetaStepping)
     Step(ThetaStepping, dt)
   end

   methods
     function SetValues(ThetaStepping, var, value)
       ThetaStepping.(var)=value;
     end

     function result = GetValues(ThetaStepping,str)
       result = ThetaStepping.(str);
     end
 end
end

The object following class generates a Geometric Brownian Motion.

classdef GBM_Object < ThetaStepping
 % Definition of a class with generates a Geometric Brownian Motion

   properties (SetAccess = private)
     sigma; r; gbm;
   end
   methods (Static)
     function vars = GetModelVariables()
     % Returns the im- and exports of th process
       vars.gbm.comment='Stock price';
       vars.gbm.Visibility='Export';
       vars.gbm.IsState= true;
       vars.gbm.Size= 1;

       vars.sigma=struct('comment','vola','Visibility','Import');
       vars.S0=struct('comment','S0','Visibility','Import');
       vars.r=struct('comment','r','Visibility','Import');
     end
   end

   methods
     function GBM_Object = GBM_Object(param)
       % Constructor
       if nargin>0
         GBM_Object.sigma = param.sigma;
         GBM_Object.r = param.r;
         GBM_Object.gbm = param.S0*ones(param.NoOfScenarios,1);
       end
     end

     function Step(GBM_Object, dt)
     % Step is called for each Theta (time) step of size "dt"
       GBM_Object.gbm = GBM_Object.gbm .* exp((GBM_Object.r-0.5.*GBM_Object.sigma.*GBM_Object.sigma)*dt + GBM_Object.sigma.*sqrt(dt).*randn(size(GBM_Object.gbm)));
     end
   end
end

This process can be accessed in ThetaML using a “call @matlab:GBM_Object”, e.g.

model testGBMObject
 export Sout

 call @matlab : GBM_Object
   export 0.05 to r
   export 0.4 to sigma
   export 100 to S0
   import S from gbm

   Sout = S
   loop 10
     Theta 1
     Sout = S
   end
end


Viewing all articles
Browse latest Browse all 10

Trending Articles