0

In order to sample a non-central chi-square distribution to price a European call option, it seems most optimal to use mp.sum; however, the program I have put together gives the TypeError: 'mpf' object cannot be interpreted as an integer when run.

I sent the code to a friend who is able to run the exact same code and obtain an output without an error. I am uncertain as to what could be causing this. The problem should arise from the mp.gammainc and mp.gamma functions found in my ncx2_samp1 and ncx2_samp2.

import math
import mpmath as mp

S_0 = 100
X = 105
sigma = 0.25
t = 1
T = 2
tau = T - t
r = 0.1
q = 0
beta = 1
S_t = S_0 * math.exp(r * t)
delta2 = sigma ** 2 * S_0 ** (2 - beta)

k = 2 * (r - q) / (delta2 * (2 - beta) * (math.exp((r - q) * (2 - beta) * tau) - 1))
x = k * S_t ** (2 - beta) * math.exp((r - q) * (2 - beta) * tau)
y = k * X ** (2 - beta)

v = 10

omega_samp1 = 2 * x
omega_samp2 = 2 * y
lambda_samp1 = 2 * y
lambda_samp2 = 2 * x


mp.dps = 25
mp.pretty = True
ncx2_samp1 = mp.nsum(lambda i: (lambda_samp1 / 2) ** i * math.exp(-lambda_samp1 / 2) / math.factorial(i) * (1 - (mp.gammainc(v / 2 + i, omega_samp1 / 2))) / (mp.gamma(v / 2 + i)), [0, math.inf])
ncx2_samp2 = mp.nsum(lambda i: (lambda_samp2 / 2) ** i * math.exp(-lambda_samp2 / 2) / math.factorial(i) * (1 - (mp.gammainc(v / 2 + i, omega_samp2 / 2))) / (mp.gamma(v / 2 + i)), [0, math.inf])

c_t = max(S_t * math.exp(-q * tau) * ncx2_samp1 - X * math.exp(-r * tau) * ncx2_samp2, 0)

print(c_t)

Any thoughts on what could be causing the problem? It should save as an mpf and be interpreted as a string; is the problem caused due to memory?

Thank you

ikeagle
  • 1
  • 1

1 Answers1

0

Swapping the interpreter to Python 3.7 instead of 3.8 solved the issue. Assuming it has something to do with the storage of the mpf file in Python 3.8

ikeagle
  • 1
  • 1