利用 Monte Carlo 预测 Python 的收入

Utilizing Monte Carlo to Predict Revenue in Python

我正在尝试在我的 python 代码中实施 Monte Carlo 模拟,这将帮助我确定我们实现与收入目标相关的各种阈值的几率。例如,我们每个财政年度达到 6,000 美元、7,000 美元或 8,000 美元的可能性有多大。我能够计算出预期值,但在编写模拟代码时运气不佳。我已经尝试创建一个可以进行 运行s 1000 次模拟的函数,但一直无法实现(感谢我的编码能力非常新手)。理想情况下,我能够 return 总和每份合约的均值和标准差,可用于将它们绘制在正态曲线上。

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)

这是我一直在写的代码的一小部分,但我在这个过程中迷路了。

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

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)

我需要能够 运行 每个财政年度的每个 ID Monte Carlo。有没有办法做到这一点?我已经创建了一个函数,该函数将为每个条目创建一个数组,但我仍然需要根据 ID 和 Filter 字段进行过滤,以用 10,000 次模拟填充每个数组。 def monte_carlo_array(df): for _ in range(len(df)): yield []

此解决方案效率不高,因为没有并行执行任何操作,但您可以清楚地看到模拟是如何执行的。

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)))