vendredi 23 février 2018

Mysterious Bug Within Python Loop?

So, I was working on some basic finance, trying to simulate a number of random stock price paths, when I encountered this inscrutable bug in the code.

My code so far:

# Imports.

%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from math import *

# Error-producing section - randnorm holds a 10x10 vector of normally distributed 
# random numbers, used in calculating my price path.

sims = 5
intervals = 5
r=.02
T=1
sigma=.15

randnorm = [[np.random.normal() for x in range(10)] for y in range(10)]
# print(randnorm)

for i in range(sims):
    for j in range(1,intervals):
        paths[i][j] = paths[i][j-1] * exp((r - .5*sigma**2)*(T/intervals) + sqrt(T/intervals)*randnorm[i][j-1])
        print(paths[0][1])

I knew something was wrong with my previous code, so I tried debugging by printing in each loop, and I get this,

0.5937221173702402
0.5937221173702402
0.5937221173702402
0.5937221173702402
1.4849274521869171
1.4849274521869171
1.4849274521869171
1.4849274521869171
0.860018700453769
0.860018700453769
0.860018700453769
0.860018700453769
1.0709782525755074
1.0709782525755074
1.0709782525755074
1.0709782525755074
0.7184834195858915
0.7184834195858915
0.7184834195858915
0.7184834195858915

What gives? I seriously do not know where I could be wrong here. It's not my random numbers, which are all different, or an index issue.




Aucun commentaire:

Enregistrer un commentaire