利用Monte Carlo预测Python的收入 [英] Utilizing Monte Carlo to Predict Revenue in Python

查看:87
本文介绍了利用Monte Carlo预测Python的收入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的python代码中实施蒙特卡洛模拟,这将帮助我确定实现与收入目标相关的各种阈值的几率.例如,每个财政年度我们达到$ 6,000,$ 7,000或$ 8,000的可能性是多少.我能够计算期望值,但是没有编写模拟代码的运气.我尝试创建一个可以运行1000次模拟的函数,但由于我的新手编码能力而无法获得它.理想情况下,我将能够返回总和每个合约的均值和标准差,这些均值和标准差可用于在正态曲线上绘制它们.

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)

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 []

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 []

推荐答案

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

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

这篇关于利用Monte Carlo预测Python的收入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆