无循环迭代二项式更新 [英] Iterative Binomial Update without Loop
问题描述
这可以不循环吗?
import numpy as np
n = 10
x = np.random.random(n+1)
a, b = 0.45, 0.55
for i in range(n):
x = a*x[:-1] + b*x[1:]
我在二项式期权定价模型有关,但我不太理解这个话题.诚实的.我只是对公式和x
的迭代更新/收缩感到着迷,想知道是否可以不循环而完成.但是我无法解决这个问题,我不确定这是否可能.
I came across this setup in another question. There it was a covered by a little obscure nomenclature. I guess it is related to Binomial options pricing model but don't quite understand the topic to be honest. I just was intrigued by the formula and this iterative update / shrinking of x
and wondered if it can be done without a loop. But I can not wrap my head around it and I am not sure if this is even possible.
让我认为可能有用的是这种变化
What makes me think that it might work is that this vatiaton
n = 10
a, b = 0.301201, 0.59692
x0 = 123
x = x0
for i in range(n):
x = a*x + b*x
# ~42
实际上只是x0*(a + b)**n
print(np.allclose(x, x0*(a + b)**n))
# True
推荐答案
您正在计算:
sum( a ** (n - i) * b ** i * x[i] * choose(n, i) for 0 <= i <= n)
[这是伪代码,而不是Python.]我不确定将其转换为Numpy的最佳方法.
[That's meant to be pseudocode, not Python.] I'm not sure of the best way to convert that into Numpy.
choose(n, i)
是n!/ (i! (n-i)!)
,而不是numpy选择功能.
choose(n, i)
is n!/ (i! (n-i)!)
, not the numpy choose function.
使用@mathfux
的评论,一个人可以做
Using @mathfux
's comment, one can do
import numpy as np
from scipy.stats import binom
binomial = binom(p=p, n=n)
pmf = binomial(np.arange(n+1))
res = np.sum(x * pmf)
所以
res = x.copy()
for i in range(n):
res = p*res[1:] + (p-1)*res[:-1]
只是二项式分布随机变量x的期望值.
is just the expected value of a binomial distributed random variable x.
这篇关于无循环迭代二项式更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!