-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNLLS_Func copy.py
40 lines (23 loc) · 929 Bytes
/
NLLS_Func copy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import math
import numpy as np
from numpy import exp
#%% NLLS_Min Function - For fmin only
def NLLS_Min(vec,Price,Maturity,CashFlow):
J, PPhat = NLLS(vec, Price, Maturity, CashFlow)
return J
#%% NLLS Function
def NLLS(vec,Price,Maturity,CashFlow):
# Assign variables
th0 = vec[0]
th1 = vec[1]
th2 = vec[2]
la = vec[3]
T = np.maximum(Maturity,1e-10) # there are some zeros that do not make the computation below possible. Such cases are automatically eliminated when we multiply for zero cash flows
RR = th0+(th1+th2)*(1-exp(-T/la))/(T/la)-th2*exp(-T/la)
# Discount
ZZhat = exp(-RR*T)
# Prices
PPhat = np.sum(CashFlow*ZZhat,axis=1)
# Compute the squared distance between actual prices and theoretical prices
J = np.sum((Price - PPhat)**2); #<-- this is the function we want to minimize!
return J, PPhat