从累计总增长计算每月增长百分比

Calculating monthly growth percentage from cumulative total growth

我正在尝试根据 Python 中的年增长率(目标)计算月度增长率常数。

我的问题与this question在算术上有相似之处,但没有完全回答。

例如,如果 2018 年的年度总销售额为 5,600,000.00 美元,我预计明年的销售额将增长 30%,那么我预计 2019 年的年度总销售额为 7,280,000.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

...等等等等

Month_01_2019到Month_12_2019的累计需要等于EV_2019.

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

在此先感谢您,请随时要求进一步说明。

更多说明:

# get beginning value (BV)
BV = 522000.00

# get desired end value (EV)
EV = 7280000.00

我们试图通过计算 [12] 个月总计的累积总和来从 BV 获得 EV(这是一个累积总和)。每个月的总计将比上个月增加 %,并且跨月保持不变。我要解决的就是这个增幅。

请记住,BV 是上一年的最后一个月。我们的预测(即第 1 至 12 个月)将从 BV 计算得出。所以,我认为从 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

我不确定这是否有效(请告诉我是否有效)但试试这个。

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)

r = 1 + monthly_rate。那么,我们要解决的问题就是

r + ... + r**12 = EV/BV。我们可以使用 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. here.

  4. 所述,上述多项式有根式解

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

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 的帮助下,我们可以使这个 运行 更快。