0

I am trying to build a simulation of an investment portfolio where there is flexibility to adjust a couple of manual inputs. I have the returns simulation running fine, but I can't figure out how to loop the ending value for previous period to equal the beginning value for the next period.

def mcs(n_years = 10, n_scenarios=10, mu=0.07, sigma=0.15, steps_per_year=12, balance=100, 
expenses=10, personal_income=5):

dt = 1/steps_per_year
n_steps = int(n_years*steps_per_year) + 1
beginning_value=balance
ending_value=balance

for i in range(n_steps):
    rets_mcs = np.random.normal(loc=(1+mu)**dt-1, scale=(sigma*np.sqrt(dt)), size=(n_steps, n_scenarios))
    ending_value = balance + balance*rets_mcs - expenses + personal_income 
    df = pd.DataFrame(data=ending_value,index=range(n_steps))
    
df.iloc[0]=balance
return df

I keep ending up results based off the original value. Any code help or resources would be appreciated.

  • Assigning to `df` inside the for loop replaces the previous contents of that variable. So when the loop is done, `df` contains only the final calculation. – John Gordon Jul 16 '22 at 01:00
  • Thanks. Managed to borrow some code ideas from this previous post and solve the issue. https://stackoverflow.com/questions/61842520/dataframe-with-monte-carlo-simulation-calculation-next-row-problem – Clueless Cobra Jul 19 '22 at 00:56

1 Answers1

0

Managed to solve this problem using the below code. This previous post was helpful (Dataframe with Monte Carlo Simulation calculation next row Problem)

n_years = 10
n_scenarios=2 
mu=0.07
sigma=0.15 
steps_per_year=12 
s_0=100
expenses=10 
personal_income=5 
inflation=0
wage_growth=0
output = []

dt = 1/steps_per_year
n_steps = int(n_years*steps_per_year) + 1
for i in range(n_steps):
     rets_ngbm = np.random.normal(loc=(1+mu)**dt-1, scale=(sigma*np.sqrt(dt)), size=n_scenarios)
     ending_value = s_0 + s_0*rets_ngbm - expenses + personal_income
     output.append(ending_value)
     s_0 = ending_value

 df = pd.DataFrame(data=output,index=range(n_steps),columns=range(n_scenarios))