mardi 5 juillet 2016

How do I vectorize the following loop in Numpy?


"""Some simulations to predict the future portfolio value based on past distribution. x is a numpy array that contains past returns.The interpolated_returns are the returns generated from the cdf of the past returns to simulate future returns. The portfolio starts with a value of 100. portfolio_value is filled up progressively as the program goes through every loop. The value is multiplied by the returns in that period and a dollar is removed.""" portfolio_final = [] for i in range(10000): portfolio_value = [100] rand_values = np.random.rand(600) interpolated_returns = np.interp(rand_values,cdf_values,x) interpolated_returns = np.add(interpolated_returns,1) for j in range(1,len(interpolated_returns)+1): portfolio_value.append(interpolated_returns[j-1]*portfolio[j-1]) portfolio_value[j] = portfolio_value[j]-1 portfolio_final.append(portfolio_value[-1]) print (np.mean(portfolio_final)) I couldn't find a way to write this code using numpy. I was having a look at iterations using nditer but I was unable to move ahead with that.

Aucun commentaire:

Enregistrer un commentaire