根据累计总增长计算月度增长百分比



我正试图根据Python中的年增长率(目标(计算一个月到月的增长率常数。

我的问题与这个问题在算术上有相似之处,但没有得到完全的回答。

例如,如果2018年的年总销售额为5600000.00美元,而我预计明年将增长30%,那么我预计2019年的年销售额将为7280000.00美元。

BV_2018 = 5600000.00
Annual_GR = 0.3
EV_2019 = (BV * 0.3) + BV

我使用2018年的最后一个月来预测2019年的第一个月

Last_Month_2018 = 522000.00  
Month_01_2019 = (Last_Month_2018 * CONSTANT) + Last_Month_2018

2019年的第二个月,我将使用

Month_02_2019 = (Month_01_2019 * CONSTANT) + Month_01_2019

等等

2019年1月至2019年12月的累计总和需要等于EV_2019。

有人知道如何用Python计算常数吗?我熟悉np.cumsum函数,所以这个部分不是问题。我的问题是我无法解决我需要的常数。

提前感谢您,请毫不犹豫地要求进一步澄清。

更多澄清:

# get beginning value (BV)
BV = 522000.00
# get desired end value (EV)
EV = 7280000.00

我们试图通过计算[12]个月总数的累积和,从BV到EV(这是一个累积和(。每个月的总额将比前一个月增加%,在几个月内保持不变。我想解决的就是这个%的增长。

请记住,BV是前一年的最后一个月。BV将计算我们的预测(即第1个月至第12个月(。所以,我认为从BV到EV再加上BV是有意义的。然后,只需从列表中删除BV及其值,就可以将EV作为第1个月到第12个月的累计总数。

我想象在这样的函数中使用这个常数:

def supplier_forecast_calculator(sales_at_cost_prior_year, sales_at_cost_prior_month, year_pct_growth_expected):
"""
Calculates monthly supplier forecast
Example:
monthly_forecast = supplier_forecast_calculator(sales_at_cost_prior_year = 5600000,
sales_at_cost_prior_month = 522000,
year_pct_growth_expected = 0.30)
monthly_forecast.all_metrics
"""
# get monthly growth rate
monthly_growth_expected = CONSTANT
# get first month sales at cost
month1_sales_at_cost = (sales_at_cost_prior_month*monthly_growth_expected)+sales_at_cost_prior_month
# instantiate lists
month_list = ['Month 1'] # for months
sales_at_cost_list = [month1_sales_at_cost] # for sales at cost
# start loop
for i in list(range(2,13)):
# Append month to list
month_list.append(str('Month ') + str(i))
# get sales at cost and append to list
month1_sales_at_cost = (month1_sales_at_cost*monthly_growth_expected)+month1_sales_at_cost
# append month1_sales_at_cost to sales at cost list
sales_at_cost_list.append(month1_sales_at_cost)
# add total to the end of month_list
month_list.insert(len(month_list), 'Total')
# add the total to the end of sales_at_cost_list
sales_at_cost_list.insert(len(sales_at_cost_list), np.sum(sales_at_cost_list))
# put the metrics into a df
all_metrics = pd.DataFrame({'Month': month_list,
'Sales at Cost': sales_at_cost_list}).round(2)
# return the df
return all_metrics

r = 1 + monthly_rate。然后,我们试图解决的问题是

CCD_ 2。我们可以使用numpy来获得数字解决方案。这在实践中应该相对较快。我们正在求解一个多项式r + ... + r**12 - EV/BV = 0,并从r中恢复月速率。会有十二个复杂的根源,但只有一个真正积极的根源——这就是我们想要的。

import numpy as np
# get beginning value (BV)
BV = 522000.00
# get desired end value (EV)
EV = 7280000.00

def get_monthly(BV, EV):
coefs = np.ones(13)
coefs[-1] -= EV / BV + 1
# there will be a unique positive real root
roots = np.roots(coefs)
return roots[(roots.imag == 0) & (roots.real > 0)][0].real - 1

rate = get_monthly(BV, EV)
print(rate)
# 0.022913299846925694

一些评论:

  1. roots.imag == 0在某些情况下可能会有问题,因为根使用数字算法。作为一种选择,我们可以在所有实部为正的根中选择虚部(绝对值(最小的根。

  2. 我们可以使用相同的方法来获得其他时间间隔的费率。例如,对于每周费率,我们可以将13 == 12 + 1替换为52 + 1

  3. 如上所述,上述多项式有一个由自由基组成的解。


性能更新。我们也可以将其定义为不动点问题,即寻找函数的不动点

x = EV/BV * x ** 13 - EV/BV + 1

固定点x将等于(1 + rate)**13

下面的纯Python实现比我机器上的上述numpy版本快大约四倍。

def get_monthly_fix(BV, EV, periods=12):
ratio = EV / BV
r = guess = ratio
while True:
r = ratio * r ** (1 / periods) - ratio + 1
if abs(r - guess) < TOLERANCE:
return r ** (1 / periods) - 1
guess = r

numba.jit的帮助下,我们可以使这次跑步更快。

我不确定这是否有效(如果无效,请告诉我(,但请尝试一下。

def get_value(start, end, times, trials=100, _amount=None, _last=-1, _increase=None):
#don't call with _amount, _last, or _increase! Only start, end and times
if _amount is None:
_amount = start / times
if _increase is None:
_increase = start / times
attempt = 1
for n in range(times):
attempt = (attempt * _amount) + attempt
if attempt > end:
if _last != 0:
_increase /= 2
_last = 0
_amount -= _increase
elif attempt < end:
if _last != 1:
_increase /= 2
_last = 1
_amount += _increase
else:
return _amount
if trials <= 0:
return _amount
return get_value(start, end, times, trials=trials-1,
_amount=_amount, _last=_last, _increase=_increase)

告诉我它是否有效。

像这样使用:

get_value(522000.00, 7280000.00, 12)

最新更新