我尝试根据先前的问题进行矢量化处理,但仍然无法正常工作。
import numpy as np
import math
S0 = 50
k_list = np.linspace(S0 * 0.6, S0 * 1.4, 50)
K=k_list
d1 = np.vectorize(math.log(S0 / K))
print(d1)
答案 0 :(得分:1)
In [141]: import math
...:
...:
...: S0 = 50
...:
...: k_list = np.linspace(S0 * 0.6, S0 * 1.4, 50)
...:
...: K=k_list
...:
...: d1 = np.vectorize(math.log(S0 / K))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-141-5d2a0f276bc5> in <module>
8 K=k_list
9
---> 10 d1 = np.vectorize(math.log(S0 / K))
TypeError: only size-1 arrays can be converted to Python scalars
np.vectorized
参数不是函数,并且实际上会产生错误:
In [142]: math.log(S0 / K)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-142-dedf1ab558ff> in <module>
----> 1 math.log(S0 / K)
TypeError: only size-1 arrays can be converted to Python scalars
log
参数是一个数组。 math.log
仅适用于1个数字,不适用于数组:
In [143]: S0 / K
Out[143]:
array([1.66666667, 1.62251656, 1.58064516, 1.5408805 , 1.50306748,
1.46706587, 1.43274854, 1.4 , 1.36871508, 1.33879781,
1.31016043, 1.28272251, 1.25641026, 1.23115578, 1.20689655,
1.18357488, 1.16113744, 1.13953488, 1.11872146, 1.09865471,
1.07929515, 1.06060606, 1.04255319, 1.0251046 , 1.00823045,
0.99190283, 0.97609562, 0.96078431, 0.94594595, 0.93155894,
0.917603 , 0.90405904, 0.89090909, 0.8781362 , 0.86572438,
0.85365854, 0.8419244 , 0.83050847, 0.81939799, 0.80858086,
0.7980456 , 0.78778135, 0.77777778, 0.76802508, 0.75851393,
0.74923547, 0.74018127, 0.73134328, 0.72271386, 0.71428571])
np.log
确实适用于数组输入:
In [145]: np.log(S0 / K)
Out[145]:
array([ 0.51082562, 0.48397837, 0.45783309, 0.43235401, 0.40750801,
0.3832644 , 0.35959465, 0.33647224, 0.3138724 , 0.29177206,
0.27014959, 0.24898478, 0.22825865, 0.20795339, 0.18805223,
0.16853942, 0.14940008, 0.13062018, 0.11218648, 0.09408644,
0.07630819, 0.0588405 , 0.0416727 , 0.02479466, 0.00819677,
-0.00813013, -0.02419473, -0.04000533, -0.05556985, -0.07089582,
-0.08599045, -0.10086061, -0.11551289, -0.12995357, -0.14418869,
-0.15822401, -0.17206506, -0.18571715, -0.19918536, -0.21247459,
-0.22558954, -0.2385347 , -0.25131443, -0.26393289, -0.27639411,
-0.28870196, -0.30086016, -0.31287232, -0.3247419 , -0.33647224])
使用vectorize
的正确方法(如果有这样的事情:))是:
In [146]: d1 = np.vectorize(lambda k: math.log(S0 / k))
In [147]: d1(K)
Out[147]:
array([ 0.51082562, 0.48397837, 0.45783309, 0.43235401, 0.40750801,
0.3832644 , 0.35959465, 0.33647224, 0.3138724 , 0.29177206,
0.27014959, 0.24898478, 0.22825865, 0.20795339, 0.18805223,
0.16853942, 0.14940008, 0.13062018, 0.11218648, 0.09408644,
0.07630819, 0.0588405 , 0.0416727 , 0.02479466, 0.00819677,
-0.00813013, -0.02419473, -0.04000533, -0.05556985, -0.07089582,
-0.08599045, -0.10086061, -0.11551289, -0.12995357, -0.14418869,
-0.15822401, -0.17206506, -0.18571715, -0.19918536, -0.21247459,
-0.22558954, -0.2385347 , -0.25131443, -0.26393289, -0.27639411,
-0.28870196, -0.30086016, -0.31287232, -0.3247419 , -0.33647224])