Utilizing Monte Carlo to Predict Revenue in Python

旧街凉风 提交于 2019-12-24 16:19:14

问题


I am trying to implement a Monte Carlo simulation into my python code that will help me determine the odds that we achieve various thresholds related to revenue targets. For example, what is the likelihood that we hit $6,000, $7,000, or $8,000 for each Fiscal Year. I'm able to calculate the expected value, but haven't had luck with coding a simulation. I've tried creating a function that runs 1000 simulations, but have not been able to get it (thanks to my very novice coding abilities). Ideally, I'd be able to return a mean and standard deviation for the total and each contract that could be used to graph them on a normal curve.

import pandas as pd

ID = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Revenue = [1000, 1200, 1300, 100 ,500, 0, 800, 950, 4321, 800, 1000, 1200, 1300, 100 ,500, 0, 800, 950, 4321, 800]
odds = [0.5, 0.6, 0.33, 0.1, 0.9, 0.87, 0.37, 0.55, 0.97, 0.09, 0.5, 0.6, 0.33, 0.1, 0.9, 0.87, 0.37, 0.55, 0.97, 0.09]
FY = [2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019]
d = {'ID': ID, 'Revenue': Revenue, 'Odds': odds, 'Fiscal Year': FY}
df = pd.DataFrame(d)
df['Expected Value'] = df['Revenue']*df['Odds']

print(df)

This is a little bit of the code that I had been writing, but I got lost along the way.

import pandas_montecarlo
mc = OtisPrediction_df['Realization Rate'].montecarlo(sims = 100)
mc.plot()
print(mc.stats)

Or

def win_loss_func(iterator):
    odds = random.randint(1,100)/100
    X = []
    Y = []
    i = 1
    while i <= iterator:
        if df['Odds'] >= odds:
            i+=1
            X.append(i)
            Y.append(OtisPrediction_df[''])
    print(odds)

I need to be able to run the Monte Carlo for each ID in each Fiscal Year. Is there a way to do this? I've created a function that will create an array for each entry, but I still need to filter based on the ID and Filter fields to fill each array with the 10,000 simulations. def monte_carlo_array(df): for _ in range(len(df)): yield []


回答1:


This solution is not very efficient as nothing is done in parallel but you can see clearly how the simulations are performed.

num_samples = 10000
revenue_2018 = []
revenue_2019 = []

filter_2018 = (df['Fiscal Year'] == 2018)
filter_2019 = (df['Fiscal Year'] == 2019)

for _ in range(num_samples):
    sample = df['Revenue'] * ( np.random.rand(20) < df['Odds'] )
    revenue_2018.append(sample.loc[filter_2018].sum())
    revenue_2019.append(sample.loc[filter_2019].sum())

# Plot simulation results.
n_bins = 10
plt.hist([revenue_2018, revenue_2019], bins=n_bins, label=["Revenue 2018", "Revenue 2019"])
plt.legend()
plt.title("{} simulations of yearly revenue".format(num_samples))

# Print statistics.
print("Mean for 2018 is {}. Standard Deviation is {}".format(np.mean(revenue_2018), np.std(revenue_2018)))
print("Mean for 2019 is {}. Standard Deviation is {}".format(np.mean(revenue_2019), np.std(revenue_2019)))


来源:https://stackoverflow.com/questions/52173863/utilizing-monte-carlo-to-predict-revenue-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!