无法向量化此函数 - 使用常量但返回 ValueError 操作数无法一起广播

问题描述 投票:0回答:1

我编写了一个 python 函数,希望能够使用 np.where 和 np.maximum 进行矢量化。但是,当尝试通过传递数据帧列来调用该函数时,我收到错误“ValueError:操作数无法一起广播...”

该函数在传递常量时工作正常。我坚持如何重写它以允许矢量化,即接受 pd.Series 作为参数。我想计算 pandas.DataFrame 每一行的平均收益,即 (2,1)

的数组

更新 2:稍微更新了代码以修复一些问题

import numpy as np
import pandas as pd

np.random.seed(5)
times, dt = np.linspace(0, 1, 251, retstep=True)
B = np.random.normal(0, np.sqrt(dt), size=(100, 250)).T
S = np.exp((0 - .15 ** 2/ 2) * dt + .15 * B)
S = 4.5 * S.cumprod(axis=0)
df = pd.DataFrame({'days': [230,250], 'otype': ['c','p'], 'strike': [4.8,4.3], 'ko': [None, 'do'], 'b': [5,4.55]})

def montecarlo_payouts(montecarlo, j, opt, k, kotype = None, b = 0, i=1):
    #adjust daycount for index starting at 0
    i = i - 1
    j = j - 1
    #deal wih argument types
    opt = opt.str.lower() if isinstance(opt, pd.Series) else opt.lower()
    k = k.to_numpy() if isinstance(k, pd.Series) else k
    k = k[:,None] if isinstance(k, (pd.Series, np.ndarray)) else k

    #vanilla option payoffs for call and put
    conditions = [np.logical_or(opt == 'c',opt == 'call')]
    payoff = np.where(conditions, np.maximum(montecarlo[j] - k, 0), np.maximum(k - montecarlo[j], 0))
    return payoff.mean(axis=1)

df = pd.DataFrame({'days': [230,250], 'otype': ['c','p'], 'strike': [4.8,4.3]})
payout = montecarlo_payouts(S, 250, df['otype'], 5)
python arrays numpy vectorization array-broadcasting
1个回答
0
投票

我能够让它在第一部分工作 - 它现在接受 pandas 系列作为所有必需的参数。仍在尝试弄清楚是否可以通过添加维度来完成,因此不要使用切片 montecarlo[j],而是使用切片 i:j 的最大值。

工作代码:

def montecarlo_payouts(montecarlo, j, opt, k, kotype = None, b = 0, i=1):
    #adjust daycount for index starting at 0
    i = i - 1
    j = j - 1
    #deal wih argument types
    j = j.to_numpy() if isinstance(j, pd.Series) else j
    opt = opt.str.lower().to_numpy() if isinstance(opt, pd.Series) else opt.lower()
    k = k.to_numpy() if isinstance(k, pd.Series) else k
    k = k[:,None] if isinstance(k, (pd.Series, np.ndarray)) else k

    #vanilla option payoffs for call and put
    itm = montecarlo[j] - k
    conditions = [np.logical_or(opt == 'c',opt == 'call')]
    callorput = np.where(conditions, 1, -1)
    payoff = np.maximum(itm * callorput.transpose(), 0)
    return payoff.mean(axis=1)
© www.soinside.com 2019 - 2024. All rights reserved.