总结直到在Python中达到时间条件

时间:2018-02-26 10:00:38

标签: python sum conditional

我想在动态模型中总结某个但是滚动的时期。正式代表如下

The picture shows the discounted sum from <code>t</code> to <code>t+(mi-a)</code>

运行等式的简单代码段是:

import numpy as np
import pandas as pd
import operator 

year = np.arange(50)
m_ = [50, 30, 15]
a = [25, 15, 7.5]
ARC_ = [38, 255, 837]
r = 0.03

我尝试在a中按m_从另一篇文章中删除列表list(map(operator.sub, m_, a)))

我失败的尝试看起来像这样:

for t in year:
    for i in range(0, 3):
        while t < t+(list(map(operator.sub, m_, a))):
            L_[t] = sum(ARC_[i] / (1+r) ** t)

1 个答案:

答案 0 :(得分:0)

我完全不确定我是否理解,我试着将答案基于等式。即使它仍然是你期望的结果,它可能会帮助你解决你的问题。

我创建一个结果列表来存储L [t]的每个值,即50个值。然后我计算每对(t,i)的和的开始/停止并计算它。

import numpy as np

years = np.arange(50)
m_ = [50, 30, 15]
a = [25, 15, 7.5]
ARC_ = [38, 255, 837]
r = 0.03

result = []

for t in years:
    s = 0

    for i in range(3):
        t0 = t
        tf = t + m_[i]-a[i]
        for k in range(int(t0), int(tf+1)):
            s += ARC_[i] / (1+r) ** t

    result.append(s)

如果您想要做的是在m和a之间计算元素差异,那么一个简单的解决方案就是:

[m_[i] - a[i] for i in range(len(m_))]

希望它有所帮助。