hey i’m using streamlit to developp my application, i work with package scipy.optimise function minimize that optimize an objective function with constraints . this function is working in python an it returns a correct results while when i use this function into streamlit code i receive as a result the initial guess that i gived him . here is my code (info : i tried to remove 2 constraints and keep 1 and the function works but the results isn’t those i want )
code of the optimisation function
import numpy as np
from scipy.optimize import fsolve
import scipy.optimize as sci_opt
from scipy.stats import norm
from scipy import optimize
s=10.6
rd=0.06
rf=0.05
T1=1
sigma1=0.20
n=norm.cdf
def BS_CALL(s, k, T1, rd , rf, sigma1):
d1 = (np.log(s/k) + ((rd-rf) + sigma1**2/2)*T1) / (sigma1*np.sqrt(T1))
d2 = d1 - sigma1 * np.sqrt(T1)
Call = s * n(d1) *np.exp(-rf*T1)- k* np.exp(-rd * T1)* n(d2)
return Call
def BS_PUT(s, k, T1, rd , rf, sigma1):
d1 = (np.log(s/k) + ((rd-rf) + sigma1**2/2)*T1) / (sigma1 *np.sqrt(T1))
d2 = d1 - sigma1* np.sqrt(T1)
Put = k*np.exp(-rd * T1)*n(-d2) - s*n(-d1)*np.exp(-rf*T1)
return Put
def opti(s,T1,rd,rf,sigma1) :
def constrainte(k):
k1 = k[0]
k2 = k[1]
print(k[0])
return k2 - k1 - 0.0000000000000000000000000000000000000000000001
def constrainte1(k):
k2 = k[1]
return k2 - s - 0.0000000000000000000000000000000000000000000001
def constrainte2(k):
k1 = k[0]
return s - k1 - 0.0000000000000000000000000000000000000000000001
def objective(k):
k1 = k[0]
k2 = k[1]
return (BS_PUT(s, k2, T1, rd, rf, sigma1) - BS_CALL(s, k1, T1, rd, rf, sigma1)) ** 2
k0=[s,s]
cons=({'type':'ineq','fun': constrainte} , {'type':'ineq','fun': constrainte1 }, {'type':'ineq','fun': constrainte2})
opt= sci_opt.minimize(objective,k0,method='BFGS',constraints=cons,options={'disp':True})
return opt
>calling the function into stremlit
j=opti(s,T1,rd,rf,sigma1)
st.markdown(j)
v= j.message
st.markdown(v)
p = j.x[0]
m = j.x[1]
the message i received
fun: 0.015634769628704602 jac: array([-1.68501236e-03, -4.65661287e-10]) message: 'Optimization terminated successfully' nfev: 3 nit: 1 njev: 1 status: 0 success: True x: array([10.8, 10.8])