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

时间:2018-12-04 23:45:54

标签: python finance cumulative-sum

我正在尝试根据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

...等等,依此类推

2019年1月1日至2019年12月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

2 个答案:

答案 0 :(得分:2)

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所述。


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

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,我们可以使运行速度更快。

答案 1 :(得分:1)

我不确定这是否行得通(如果不行,请告诉我),但是请尝试

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)
相关问题