exam2 + remaining files of the semester

This commit is contained in:
2022-01-08 23:53:49 +01:00
parent 5a31fc8f33
commit e4cf7b7910
32 changed files with 41546 additions and 1 deletions

View File

@@ -0,0 +1,112 @@
#------------------------------------------------------------------------------
"""
OlsGMFn(Y,X)
LS of Y on X; for one dependent variable, Gauss-Markov assumptions
# Usage
(b,u,Yhat,V,R2) = OlsGMFn(Y,X)
# Input
- `Y::Vector`: T-vector, the dependent variable
- `X::Matrix`: Txk matrix of regressors (including deterministic ones)
# Output
- `b::Vector`: k-vector, regression coefficients
- `u::Vector`: T-vector, residuals Y - yhat
- `Yhat::Vector`: T-vector, fitted values X*b
- `V::Matrix`: kxk matrix, covariance matrix of b
- `R2::Number`: scalar, R2 value
"""
function OlsGMFn(Y,X)
T = size(Y,1)
b = X\Y
Yhat = X*b
u = Y - Yhat
σ2 = var(u)
V = inv(X'X)*σ2
R2 = 1 - σ2/var(Y)
return b, u, Yhat, V, R2
end
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
"""
OlsNWFn(Y,X,m=0)
LS of Y on X; for one dependent variable, using Newey-West covariance matrix
# Usage
(b,u,Yhat,V,R2) = OlsNWFn(Y,X,m)
# Input
- `Y::Array`: Tx1, the dependent variable
- `X::Array`: Txk matrix of regressors (including deterministic ones)
- `m::Int`: scalar, bandwidth in Newey-West
# Output
- `b::Array`: kx1, regression coefficients
- `u::Array`: Tx1, residuals Y - Yhat
- `Yhat::Vector`: Tx1, fitted values X*b
- `V::Array`: kxk matrix, covariance matrix of b
- `R2::Number`: scalar, R2 value
"""
function OlsNWFn(Y,X,m=0)
T = size(Y,1)
b = X\Y
Yhat = X*b
u = Y - Yhat
S = CovNWFn(X.*u,m) #Newey-West covariance matrix
Sxx = X'X
V = inv(Sxx)'S*inv(Sxx) #covariance matrix of b
R2 = 1 - var(u)/var(Y)
return b, u, Yhat, V, R2
end
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
"""
CovNWFn(g0,m=0)
Calculates covariance matrix of sample average.
# Input
- `g0::Matrix`: Txq Matrix of q moment conditions
- `m:int`: scalar, number of lags to use
# Output
- `S::Matrix`: qxq covariance matrix(average g0)
"""
function CovNWFn(g0,m=0)
T = size(g0,1) #g0 is Txq
m = min(m,T-1) #number of lags
g = g0 .- mean(g0,dims=1) #normalizing to zero means
S = g'g #(qxT)*(Txq)
for s = 1:m
Λ_s = g[s+1:T,:]'g[1:T-s,:] #same as Sum[g_t*g_{t-s}',t=s+1,T]
S = S + (1 - s/(m+1))*(Λ_s + Λ_s')
end
return S
end
#------------------------------------------------------------------------------

View File

@@ -0,0 +1,104 @@
"""
BuildSTree(S,n,u,d)
Build binomial tree, starting at `S` and having `n` steps with up move `u` and down move `d`
# Output
- `STree:: Vector or vectors`: each (sub-)vector is for a time step. `STree[0] = [S]` and `STree[n]` is for time period n.
"""
function BuildSTree(S,n,u,d)
STree = [fill(NaN,i) for i = 1:n+1] #vector of vectors (of different lengths)
STree = OffsetArray(STree,0:n) #convert so the indices are 0:n
STree[0][1] = S #step 0 is in STree[0], element 1
for i = 1:n #move forward in time
STree[i][1:end-1] = u*STree[i-1] #up move from STree[i-1][1:end]
STree[i][end] = d*STree[i-1][end] #down move from STree[i-1][end]
end
return STree
end
"""
EuOptionPrice(STree,K,y,h,p,isPut=false)
Calculate price of European option from binomial model
# Output
- `Value:: Vector of vectors`: option values at different nodes, same structure as STree
"""
function EuOptionPrice(STree,K,y,h,p,isPut=false) #price of European option
Value = similar(STree) #tree for derivative, to fill
n = length(STree) - 1 #number of steps in STree
if isPut
Value[n] = max.(0,K.-STree[n]) #put, at last time node
else
Value[n] = max.(0,STree[n].-K) #call, at last time node
end
for i = n-1:-1:0 #move backward in time
Value[i] = exp(-y*h)*(p*Value[i+1][1:end-1] + (1-p)*Value[i+1][2:end])
end #p*up + (1-p)*down, discount
return Value
end
"""
AmOptionPrice(STree,K,y,h,p,isPut=false)
Calculate price of American option from binomial model
# Output
- `Value:: Vector of vectors`: option values at different nodes, same structure as STree
- `Exerc::` Vector of vectors`: true if early exercise at the node, same structure as STree
"""
function AmOptionPrice(STree,K,y,h,p,isPut=false) #price of American option
Value = similar(STree) #tree for derivative, to fill
n = length(STree) - 1
Exerc = similar(Value,BitArray) #same structure as STree, but BitArrays, empty
if isPut
Value[n] = max.(0,K.-STree[n]) #put, at last time node
else
Value[n] = max.(0,STree[n].-K) #call, at last time node
end
Exerc[n] = Value[n] .> 0 #exercise
for i = n-1:-1:0 #move backward in time
fa = exp(-y*h)*(p*Value[i+1][1:end-1] + (1-p)*Value[i+1][2:end])
if isPut
Value[i] = max.(K.-STree[i],fa) #put
else
Value[i] = max.(STree[i].-K,fa) #call
end
Exerc[i] = Value[i] .> fa #early exercise
end
return Value, Exerc
end
"""
Φ(x)
Calculate Pr(z<=x) for N(0,1) variable z
"""
function Φ(x)
Pr = cdf(Normal(0,1),x)
return Pr
end
"""
OptionBlackSPs(S,K,m,y,σ,δ=0,PutIt=false)
Calculate Black-Scholes European call or put option price, continuous dividends of δ
"""
function OptionBlackSPs(S,K,m,y,σ,δ=0,PutIt=false)
d1 = ( log(S/K) + (y-δ+0.5*σ^2)*m ) / (σ*sqrt(m))
d2 = d1 - σ*sqrt(m)
c = exp(-δ*m)*S*Φ(d1) - K*exp(-y*m)*Φ(d2)
if PutIt
price = c - exp(-δ*m)*S + exp(-y*m)*K
else
price = c
end
return price
end