Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[Matlab] Simulation of Stochastic Process

  1. May 28, 2015 #1
    Hi all,
    I have this dynamic:
    is a Mean Reverting process. I want to simulate the sde with matlab but I am a beginner and I have some problems. I show you the code that I have created:

    Code (Text):

    %% Simulazione prezzo Geometric Ornstein-Ulenbeck

    clear all

    mu = 0.5;
    sigma = 0.12;
    eta = 1;
    T = 2;
    N_t = 300; %Temporal Intervals
    N = 1000; %Number of Simulations
    t=linspace(0,T,N_t); %Temporal line

    dt=T/N_t; %Temporal increments
    P_t(:,1)=20; %First Price

    for i=1:N
        for j=2:N_t
            dW = randn;
            P_t(i,j) = P_t(i,j-1) + eta*(mu-log(P_t(i,j-1)))*P_t(i,j-1)*dt+sigma*P_t(i,j-1)*dW;

    But I don't know if it is correct. Can you help me?
  2. jcsd
  3. May 28, 2015 #2


    User Avatar
    Gold Member

    The code seems to run fine and the plot looks reasonable. Is there some reason that you think it is wrong?
  4. May 28, 2015 #3
    Because is my first work alone and I'm not sure of the cicle. Do you think it's ok?
  5. May 28, 2015 #4
    It is not OK. The discretization of the Wiener increment is wrong. Here is the first link that I could find that explains how to do it correctly in Matlab:


    To summarize: [itex]dW \approx \sqrt(dt)N(0,1)[/itex], so you forgot to multiply your dW by the square root of dt.

    Also, the wikipedia entry for the first order scheme has some example code for exactly this problem:

    Note that this is a first order discretization, there are also higher order methods that you can find in the literature, e.g. in the book of Kloeden and Platen. I highly recommend this book if you want to numerically solve stochastic differential equations. The basic idea of these methods comes from Ito calculus and the idea of stochastic Taylor expansion.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: [Matlab] Simulation of Stochastic Process