-1

My problem: integrate.quad does not work with my input parameters (dataFrames).

My goal: Calculating expected value for a defined function and specific mu, sigma, lower bound and upper bound of integral per product

Desired output: 2x2 dataFrame

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

My approach:

  • Definition of function to be integrated: y*pdf(y)
  • Integration of the the function with lower bound = 0 and upper bound = ubIntegration

Here is the full code:

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]))
Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
MUCSP_90
  • 23
  • 5

1 Answers1

1

Your ub contains two values, which is obviously illegal. Simply loop over them:

# 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
Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249