【文件属性】:
文件名称:使用_numpy_向量计算-3gpp-23501-g10(中文版)
文件大小:28.14MB
文件格式:PDF
更新时间:2021-07-11 14:01:36
python 量化交易
2.2 使用 numpy 向量计算
numpy 的内置数学函数可以天然的运用于向量:
sample = np.linspace(1.0,100.0,5)
np.exp(sample)
array([ 2.71828183e+00, 1.52434373e+11, 8.54813429e+21,
4.79357761e+32, 2.68811714e+43])
利用 numpy 的数学函数,我们可以重写原先的计算公式
call_option_pricer ,使得它接受向量参数。
# 使用numpy的向量函数重写Black - Scholes公式
def call_option_pricer_nunmpy(spot, strike, maturity, r, vol):
d1 = (np.log(spot/strike) + (r + 0.5 * vol *vol) * maturity)
/ vol / np.sqrt(maturity)
d2 = d1 - vol * np.sqrt(maturity)
price = spot * norm.cdf(d1) - strike * np.exp(-r*maturity) *
norm.cdf(d2)
return price
量化分析师的Python日记【第7天:Q Quant 之初出江湖】
90