Home Creating a monte carlo simulation from a loop python
 I am attempting to calculate the probablility of a for loop returning a value lower than 10% of the initial value input using a monte-carlo simulation. for i in range(0, period): if i < 1: r=(rtn_daily[i]+sig_daily[i]*D[i]) stock = stock_initial * (1+r) elif i >=1: r=(rtn_daily[i]+sig_daily[i]*D[i]) stock = stock * (1+r) print(stock)  This is the for-loop that I wish to run a large number of times (200000 as a rough number) and calculate the probability that: stock < stock_initial * .9  I've found examples that define their initial loop as a function and then will use that function in the loop, so I have tried to define a function from my loop: def stock_value(period): for i in range(0, period): if i < 1: r=(rtn_daily[i]+sig_daily[i]*D[i]) stock = stock_initial * (1+r) elif i >=1: r=(rtn_daily[i]+sig_daily[i]*D[i]) stock = stock * (1+r) return(stock)  This produces values for 'stock' that don't seem to fit the same range as before being defined as a function. using this code I tried to run a monte-carlo simulation: # code to implement monte-carlo simulation number_of_loops = 200 # lower number to run quicker for stock_calc in range(1,period+1): moneyneeded = 0 for i in range(number_of_loops): stock=stock_value(stock_calc) if stock < stock_initial * 0.90: moneyneeded += 1 #print(stock) this is to check the value of stock being produced. stock_percentage = float(moneyneeded) / number_of_loops print(stock_percentage)  but this returns no results outside the 10% range even when looped 200000 times, it seems the range/spread of results gets hugely reduced in my defined function somehow. Can anyone see a problem in my defined function 'stock_value' or can see a way of implementing a monte-carlo simulation in a way I've not come across? My full code for reference: #import all modules required import numpy as np # using different notation for easier writting import scipy as sp import matplotlib.pyplot as plt #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #collect variables provided by the user stock_initial = float(12000) # can be input for variable price of stock initially. period = int(63) # can be edited to an input() command for variable periods. mrgn_dec = .10 # decimal value of 10%, can be manipulated to produce a 10% increase/decrease addmoremoney = stock_initial*(1-mrgn_dec) rtn_annual = np.repeat(np.arange(0.00,0.15,0.05), 31) sig_annual = np.repeat(np.arange(0.01,0.31,0.01), 3) #use .31 as python doesn't include the upper range value. #functions for variables of daily return and risk. rtn_daily = float((1/252))*rtn_annual sig_daily = float((1/(np.sqrt(252))))*sig_annual D=np.random.normal(size=period) # unsure of range to use for standard distribution #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # returns the final value of stock after 63rd day(possibly?) def stock_value(period): for i in range(0, period): if i < 1: r=(rtn_daily[i]+sig_daily[i]*D[i]) stock = stock_initial * (1+r) elif i >=1: r=(rtn_daily[i]+sig_daily[i]*D[i]) stock = stock * (1+r) return(stock) #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # code to implement monte-carlo simulation number_of_loops = 20000 for stock_calc in range(1,period+1): moneyneeded = 0 for i in range(number_of_loops): stock=stock_value(stock_calc) if stock < stock_initial * 0.90: moneyneeded += 1 print(stock) stock_percentage = float(moneyneeded) / number_of_loops print(stock_percentage) #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~