Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
213 views
in Technique[技术] by (71.8m points)

python - obtain weird result when solving the integration problem

enter image description here

The problem is to solve gamma first, and then plug gamma into the second equation to get k*, but I could not figure out why my K* is always 1.0. Any help would be appreciated, and if this problem could be solved in python, that would be great also.

Here is my attempt,

T = 1;
sigma = 0.3;
r = 0.09;
a = r - sigma^2/2;
K = 100;
S_0 = 100;
M = 3;

syms gamma Kstar t; 
eqa = int(S_0*exp(exp(3*t*(T-t/2)*gamma*sigma/T^2 + a*t + sigma^2*(t- 3*t^2*(T - t/2)^2/T^3)/2)), t, 0, T) == T*K;
Y = vpasolve(eqa,gamma);
K_s = solve((log(log(Kstar)) - log(M))*T/sigma == Y, Kstar);
display(K_s);

And here is my attempt by python

import scipy.integrate as integrate
import scipy.special as special
from scipy.integrate import quad
from numpy import exp, log
from sympy.solvers import solve
from sympy import Symbol
T = 1
sigma = 0.3
r = 0.09
a = r - sigma**2/2
K = 100
S_0 = 100
M_2 = S_0 * exp(r*T/2 - sigma**2*T/12)

t = Symbol("t")
Kstar = Symbol("Kstar")
gamma = Symbol("gamma")

def integrand(t, gamma):
    return S_0*exp(exp(3*t*(T-t/2)*gamma*sigma/T**2 + a*t + sigma**2*(t- 3*t**2*(T - t/2)**2/T**3)/2))

eqa = quad(integrand, 0, T, args(gamma)) == K*T

result = solve(eqa, gamma)
print(result)

And this bug occured

NameError                                 Traceback (most recent call last)
<ipython-input-10-e24a1238ff27> in <module>
     20     return S_0*exp(exp(3*t*(T-t/2)*gamma*sigma/T**2 + a*t + sigma**2*(t- 3*t**2*(T - t/2)**2/T**3)/2))
     21 
---> 22 eqa = quad(integrand, 0, T, args(gamma)) == K*T
     23 
     24 result = solve(eqa, gamma)

NameError: name 'args' is not defined

Thanks a lot

question from:https://stackoverflow.com/questions/65867091/obtain-weird-result-when-solving-the-integration-problem

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

why did you call args here, thats not a function args(gamma) please explain what you want it to do


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...