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

· Programming
Authors

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

3 Comments

Comments RSS
  1. My Homepage

    It’s not that I want to duplicate your web site, but I truly like the style. Could you tell me which design are you utilizing? Or was it custom created? 758423

    • computeraidedfinance

      Sorry for the late reply: Your message was stuck in my spam filter. It’s the Annotum Base Design: Quite new and free.

  2. Dolly

    Clear, infromaitve, simple. Could I send you some e-hugs?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.