计算特定积分的预期值(通过 norm.pdf 和 integrate.quad)不适用于输入参数

Calculation of expected value for specific integral (through norm.pdf and integrate.quad) does not work with input parameters

我的问题:integrate.quad 不适用于我的输入参数(数据帧)。

我的目标:计算定义函数的预期值和每个产品积分的特定 mu、sigma、下限和上限

所需输出:2x2 数据帧

          1                      2
Product1  0.000000e+00           0.000000e+00
Product2  2.929601072372639e-40  1.6332471427821411e-52

我的做法:

完整代码如下:

import pandas as pd
import scipy.stats
import scipy.integrate as integrate

# Input parameters as dataframes
mu = pd.DataFrame({'1': [7, 12],
                   '2': [7.50, 16.97]},
                  index=["Product1", "Product2"])
sigma = pd.DataFrame({'1': [0.07, 0.6],
                      '2': [0.075, 0.848]},
                     index=["Product1", "Product2"])
input = pd.DataFrame({'1': [1, 2]},
                     index=["Product1", "Product2"])
ubIntegration = pd.DataFrame({'1': [2, 4]},
                             index=["Product1", "Product2"])

# Definition of function: y*pdf(y)
def function(y, mu, sigma):
    return y * scipy.stats.norm.pdf(y, mu, sigma)

# Calculation of expected value through integration of "function"
for i in mu.index.values:
    for k in mu.columns.values:
        lb = 0
        ub = ubIntegration.loc[i]
        EV, err = integrate.quad(function, lb, ub, args=(mu.loc[i,k], sigma.loc[i,k]))

你的ub包含两个值,这显然是非法的。简单地遍历它们:

# Calculation of expected value through integration of "function"
for i in mu.index.values:
    for k in mu.columns.values:
        lb = 0
        for ub in ubIntegration.loc[i]:
            EV, err = integrate.quad(function, lb, ub, args=(mu.loc[i,k], sigma.loc[i,k]))
            print(EV, err)
0.0 0.0
0.0 0.0
2.929601072372639e-40 1.424432202967594e-41
1.6332471427821411e-52 5.351027607956578e-55