0

Background Information:

Consider the Psedocode:

enter image description here

Question:

I am trying to implement the above in C++ but I don't really understand how to implement it correctly. Note, the ZZ is the Z_ij in the Pseudocode. We have S[0] = 50. Here is my code:

for(int j = 1; j <= N; j++){
    for(int i = 1; i <= n; i++){
        S[i] = S[i-1]*exp((mu - sigma/2)*(t[i] - t[i-1]) + sqrt(sigma)*sqrt(t[i] - t[i-1])*ZZ[i]);
    }
}

Big N = 10000 and n = 10. I know that my outer loop does nothing but heat up my cpu, but I am not sure how to use the outer loop from the Pseudocode above. Any suggestions are greatly appreciated.

Here is the time vector I created, we use 10 time steps t_0 = 0, t_1 = \Delta t, t_2 = 2\Delta t,...,t_10 = 10\Delta t = T. Note T = 1. Here is the code:

double t[n+1];
for(int i = 0; i <= n; i++){
    t[i] = (double)i*T/(n-1);
}
  • I don't understand this notation. Particles have a Boltzmann distribution in speed. If you want to simulate Brownian motion by simulating the larger particles explicitly and keeping the small ones implicit your problem is "shielding effects" from the larger particles, if you want to see anything interesting. – Malcolm McLean Mar 11 '17 at 17:50

2 Answers2

1

The 'Z_i,j' depend on both loops, I.e. the indexes I and j. Also you are missing the function M(Phi_i(j)). This function makes The depend on both indexes.

AchimGuetlein
  • 501
  • 3
  • 9
  • Yes I see your point but we generate the Z_ij from the Box Muller method so it will just be a single vector of standard normal random numbers. –  Mar 11 '17 at 17:21
  • Also, arrays in C++ go from 0 to n-1, so you'll likely get a memory exception when trying to access the last element in the loops. – user1118321 Mar 11 '17 at 17:21
  • @user1118321 I know I made the N + 1 and n + 1 and only use the first index 1 to N and 1 to n, I know its strange but I had to do this based on the time vector t –  Mar 11 '17 at 17:23
0

You need to change the t array (it is time, i suppose).

for(int j = 1; j < N; j++){
    for(int i = 1; i < n; i++){
        t[i] = t[i-1] + deltat;
        S[i] = S[i-1]*exp((mu - sigma/2)*(t[i] - t[i-1]) + sqrt(sigma)*sqrt(t[i] - t[i-1])*ZZ[i]);
    } }
user3811082
  • 218
  • 1
  • 7
  • How did you define deltat? I will add what I wrote for creating t, also I am not sure why you go from 1 to N-1 I beleive we have to go to 1 to N –  Mar 11 '17 at 17:43
  • Also note that your outer loop does nothing. –  Mar 11 '17 at 17:46
  • Very strange. This code works for me. What is your mu, sigma values? – user3811082 Mar 11 '17 at 18:23
  • mu is .1 sigma is .3. You didn't tell me what your deltat variable is, could you tell me what you defined deltat to be? –  Mar 11 '17 at 20:00
  • It does not matter. `deltat` a constant value which is greater than zero. I dont know why your code is not working, because i checked a similar one and get non-zero changing values – user3811082 Mar 11 '17 at 20:11
  • I am not saying my code or your code does not run I just don't think it is correct –  Mar 12 '17 at 20:09
  • See http://stackoverflow.com/questions/42753073/simulating-the-geometric-brownian-motion –  Mar 12 '17 at 21:00