exam2 + remaining files of the semester
This commit is contained in:
parent
5a31fc8f33
commit
e4cf7b7910
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
||||||
/.DS_Store
|
/.DS_Store
|
||||||
/.ipynb_checkpoints
|
/.ipynb_checkpoints
|
||||||
|
.DS_Store
|
2405
Exam1/Exam1_Solution.ipynb
Normal file
2405
Exam1/Exam1_Solution.ipynb
Normal file
File diff suppressed because one or more lines are too long
22
Exam1/Exam2021_1_Feedback.jl
Normal file
22
Exam1/Exam2021_1_Feedback.jl
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
|
||||||
|
using Printf
|
||||||
|
include("jlFiles/printmat.jl")
|
||||||
|
|
||||||
|
println("Three ways of doing the same thing")
|
||||||
|
|
||||||
|
fp = [1 2;
|
||||||
|
1.1 2.1]
|
||||||
|
|
||||||
|
x1 = fp*10 #easiest and fastest
|
||||||
|
printmat(x1)
|
||||||
|
|
||||||
|
x2 = map(i->fp[:,i]*10,1:2) #cumbersome
|
||||||
|
x2 = hcat(x2...)
|
||||||
|
printmat(x2)
|
||||||
|
|
||||||
|
x3 = zeros(size(fp)) #even more cumbersome
|
||||||
|
for i=1:size(fp,1),j=1:size(fp,2)
|
||||||
|
x3[i,j] = fp[i,j]*10
|
||||||
|
end
|
||||||
|
printmat(x3)
|
||||||
|
|
1147
Exam2/Exam2.ipynb
Normal file
1147
Exam2/Exam2.ipynb
Normal file
File diff suppressed because one or more lines are too long
BIN
Exam2/Exam2_FakeResults.pdf
Normal file
BIN
Exam2/Exam2_FakeResults.pdf
Normal file
Binary file not shown.
246
Exam2/jlFiles/printmat.jl
Normal file
246
Exam2/jlFiles/printmat.jl
Normal file
|
@ -0,0 +1,246 @@
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
"""
|
||||||
|
printmat([fh::IO],x...;colNames=[],rowNames=[],
|
||||||
|
width=10,prec=3,NoPrinting=false,StringFmt="",cell00="")
|
||||||
|
|
||||||
|
Print all elements of a matrix (or several) with predefined formatting. It can also handle
|
||||||
|
OffsetArrays. StringFmt = "csv" prints using a csv format.
|
||||||
|
|
||||||
|
# Input
|
||||||
|
- `fh::IO`: (optional) file handle. If not supplied, prints to screen
|
||||||
|
- `x::Array(s)`: (of numbers, dates, strings, ...) to print
|
||||||
|
- `colNames::Array`: of strings with column headers
|
||||||
|
- `rowNames::Array`: of strings with row labels
|
||||||
|
- `width::Int`: (keyword) scalar, minimum width of printed cells
|
||||||
|
- `prec::Int`: (keyword) scalar, precision of printed cells
|
||||||
|
- `NoPrinting::Bool`: (keyword) bool, true: no printing, just return formatted string [false]
|
||||||
|
- `StringFmt::String`: (keyword) string, "", "csv"
|
||||||
|
- `cell00::String`: (keyword) string, for row 0, column 0
|
||||||
|
|
||||||
|
# Output
|
||||||
|
- str (if NoPrinting) string, (otherwise nothing)
|
||||||
|
|
||||||
|
# Examples
|
||||||
|
```
|
||||||
|
x = [11 12;21 22]
|
||||||
|
printmat(x)
|
||||||
|
```
|
||||||
|
```
|
||||||
|
x = [1 "ab"; Date(2018,10,7) 3.14]
|
||||||
|
printmat(x,width=20,colNames=["col 1","col 2"])
|
||||||
|
```
|
||||||
|
```
|
||||||
|
printmat([11,12],[21,22])
|
||||||
|
```
|
||||||
|
Can also call as
|
||||||
|
```
|
||||||
|
opt = Dict(:rowNames=>["1";"4"],:width=>10,:prec=>3,:NoPrinting=>false,:StringFmt=>"")
|
||||||
|
printmat(x;colNames=["a","b"],opt...) #notice ; and ...
|
||||||
|
```
|
||||||
|
(not all keywords are needed)
|
||||||
|
|
||||||
|
# Requires
|
||||||
|
- fmtNumPs
|
||||||
|
|
||||||
|
# Notice
|
||||||
|
- The prefixN and suffixN could potentially be made function inputs. This would allow
|
||||||
|
a fairly flexible way to format tables.
|
||||||
|
|
||||||
|
|
||||||
|
Paul.Soderlind@unisg.ch
|
||||||
|
|
||||||
|
"""
|
||||||
|
function printmat(fh::IO,x...;colNames=[],rowNames=[],
|
||||||
|
width=10,prec=3,NoPrinting=false,StringFmt="",cell00="")
|
||||||
|
|
||||||
|
isempty(x) && return nothing #do nothing is isempty(x)
|
||||||
|
|
||||||
|
typeTestQ = any(!=(eltype(x[1])),[eltype(z) for z in x]) #test if eltype(x[i]) differs
|
||||||
|
if typeTestQ #create matrix from tuple created by x...
|
||||||
|
x = hcat(Matrix{Any}(hcat(x[1])),x[2:end]...) #preserving types of x[i]
|
||||||
|
else
|
||||||
|
x = hcat(x...)
|
||||||
|
end
|
||||||
|
|
||||||
|
(m,n) = (size(x,1),size(x,2))
|
||||||
|
|
||||||
|
(length(rowNames) == 1 < m) && (rowNames = [string(rowNames[1],i) for i = 1:m]) #"ri"
|
||||||
|
(length(colNames) == 1 < n) && (colNames = [string(colNames[1],i) for i = 1:n]) #"ci"
|
||||||
|
|
||||||
|
if StringFmt == "csv"
|
||||||
|
(prefixN,suffixN) = (fill("",n),vcat(fill(",",n-1),"")) #prefix and suffix for column 1:n
|
||||||
|
(prefixC0,suffixC0) = ("",",") #prefix and suffix for column 0
|
||||||
|
else
|
||||||
|
(prefixN,suffixN) = (fill("",n),fill("",n))
|
||||||
|
(prefixC0,suffixC0) = ("","")
|
||||||
|
end
|
||||||
|
|
||||||
|
if length(rowNames) == 0 #width of column 0 (cell00 and rowNames)
|
||||||
|
col0Width = 0
|
||||||
|
else
|
||||||
|
col0Width = maximum(length,vcat(cell00,rowNames)) + length(prefixC0) + length(suffixC0)
|
||||||
|
end
|
||||||
|
|
||||||
|
colWidth = [width + length(prefixN[j]) + length(suffixN[j]) for j=1:n] #widths of column 1:n
|
||||||
|
|
||||||
|
iob = IOBuffer()
|
||||||
|
|
||||||
|
if !isempty(colNames) #print (cell00,colNames), if any
|
||||||
|
!isempty(cell00) ? txt0 = string(prefixC0,cell00,suffixC0) : txt0 = ""
|
||||||
|
print(iob,rpad(txt0,col0Width))
|
||||||
|
for j = 1:n #loop over columns
|
||||||
|
print(iob,lpad(string(prefixN[j],colNames[j],suffixN[j]),colWidth[j]))
|
||||||
|
end
|
||||||
|
print(iob,"\n")
|
||||||
|
end
|
||||||
|
#print rowNames and x
|
||||||
|
(i0,j0) = (1 - first(axes(x,1)),1 - first(axes(x,2))) #i+i0,j+j0 give traditional indices
|
||||||
|
for i in axes(x,1) #loop over rows
|
||||||
|
!isempty(rowNames) && print(iob,rpad(string(prefixC0,rowNames[i+i0],suffixC0),col0Width))
|
||||||
|
for j in axes(x,2) #loop over columns
|
||||||
|
print(iob,fmtNumPs(x[i,j],width,prec,"right",prefix=prefixN[j+j0],suffix=suffixN[j+j0]))
|
||||||
|
end
|
||||||
|
print(iob,"\n")
|
||||||
|
end
|
||||||
|
str = String(take!(iob))
|
||||||
|
|
||||||
|
if NoPrinting #no printing, just return str
|
||||||
|
return str
|
||||||
|
else #print, return nothing
|
||||||
|
print(fh,str,"\n")
|
||||||
|
return nothing
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
#when fh is not supplied: printing to screen
|
||||||
|
printmat(x...;colNames=[],rowNames=[],width=10,prec=3,NoPrinting=false,StringFmt="",cell00="") =
|
||||||
|
printmat(stdout::IO,x...;colNames,rowNames,width,prec,NoPrinting,StringFmt,cell00)
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
"""
|
||||||
|
printlnPs([fh::IO],z...;width=10,prec=3)
|
||||||
|
|
||||||
|
Subsitute for println, with predefined formatting.
|
||||||
|
|
||||||
|
|
||||||
|
# Input
|
||||||
|
- `fh::IO`: (optional) file handle. If not supplied, prints to screen
|
||||||
|
- `z::String`: string, numbers and arrays to print
|
||||||
|
|
||||||
|
Paul.Soderlind@unisg.ch
|
||||||
|
|
||||||
|
"""
|
||||||
|
function printlnPs(fh::IO,z...;width=10,prec=3)
|
||||||
|
|
||||||
|
for x in z #loop over inputs in z...
|
||||||
|
if isa(x,AbstractArray)
|
||||||
|
iob = IOBuffer()
|
||||||
|
for i = 1:length(x)
|
||||||
|
print(iob,fmtNumPs(x[i],width,prec,"right"))
|
||||||
|
end
|
||||||
|
print(fh,String(take!(iob)))
|
||||||
|
else
|
||||||
|
print(fh,fmtNumPs(x,width,prec,"right"))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
print(fh,"\n")
|
||||||
|
|
||||||
|
end
|
||||||
|
#when fh is not supplied: printing to screen
|
||||||
|
printlnPs(z...;width=10,prec=3) = printlnPs(stdout::IO,z...;width,prec)
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
"""
|
||||||
|
fmtNumPs(z,width=10,prec=2,justify="right";prefix="",suffix="")
|
||||||
|
|
||||||
|
Create a formatted string of a float (eg, "%10.4f"), nothing (""),
|
||||||
|
while other values are passed through. Strings are right (or left) justified
|
||||||
|
and can optionally be given prefix and suffix (eg, ",")
|
||||||
|
|
||||||
|
# Notice
|
||||||
|
- With prec > 0 and isa(z,Integer), then the string is padded with 1+prec spaces
|
||||||
|
to align with the printing of floats with the same prec.
|
||||||
|
|
||||||
|
# Requires
|
||||||
|
- Printf (for 1.6-), fmtNumPsC (for < 1.6)
|
||||||
|
|
||||||
|
"""
|
||||||
|
function fmtNumPs(z,width=10,prec=2,justify="right";prefix="",suffix="")
|
||||||
|
|
||||||
|
isa(z,Bool) && (z = convert(Int,z)) #Bool -> Int
|
||||||
|
|
||||||
|
if isa(z,AbstractFloat) #example: 101.0234, prec=3
|
||||||
|
if VERSION < v"1.6-"
|
||||||
|
fmt = "%$(width).$(prec)f"
|
||||||
|
zRound = round(z,digits=prec)
|
||||||
|
strLR = fmtNumPsC(fmt,zRound) #C fallback solution
|
||||||
|
else
|
||||||
|
fmt = Printf.Format("%$(width).$(prec)f")
|
||||||
|
strLR = Printf.format(fmt,z)
|
||||||
|
end
|
||||||
|
elseif isa(z,Nothing)
|
||||||
|
strLR = ""
|
||||||
|
elseif isa(z,Integer) && prec > 0 #integer followed by (1+prec spaces)
|
||||||
|
strLR = string(z," "^(1+prec))
|
||||||
|
else #Int, String, Date, Missing, etc
|
||||||
|
strLR = string(z)
|
||||||
|
end
|
||||||
|
|
||||||
|
strLR = string(prefix,strLR,suffix)
|
||||||
|
|
||||||
|
if justify == "left" #justification
|
||||||
|
strLR = rpad(strLR,width+length(prefix)+length(suffix))
|
||||||
|
else
|
||||||
|
strLR = lpad(strLR,width+length(prefix)+length(suffix))
|
||||||
|
end
|
||||||
|
|
||||||
|
return strLR
|
||||||
|
|
||||||
|
end
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
"""
|
||||||
|
fmtNumPsC(fmt,z)
|
||||||
|
|
||||||
|
c fallback solution for formatting of floating point number. Used if VERSION < v"1.6-"
|
||||||
|
"""
|
||||||
|
function fmtNumPsC(fmt,z) #c fallback solution
|
||||||
|
if ismissing(z) || isnan(z) || isinf(z) #asprintf does not work for these cases
|
||||||
|
str = string(z)
|
||||||
|
else
|
||||||
|
strp = Ref{Ptr{Cchar}}(0)
|
||||||
|
len = ccall(:asprintf,Cint,(Ptr{Ptr{Cchar}},Cstring,Cdouble...),strp,fmt,z)
|
||||||
|
str = unsafe_string(strp[],len)
|
||||||
|
Libc.free(strp[])
|
||||||
|
end
|
||||||
|
return str
|
||||||
|
end
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
function printblue(x...)
|
||||||
|
foreach(z->printstyled(z,color=:blue,bold=true),x)
|
||||||
|
print("\n")
|
||||||
|
end
|
||||||
|
function printred(x...)
|
||||||
|
foreach(z->printstyled(z,color=:red,bold=true),x)
|
||||||
|
print("\n")
|
||||||
|
end
|
||||||
|
function printmagenta(x...)
|
||||||
|
foreach(z->printstyled(z,color=:magenta,bold=true),x)
|
||||||
|
print("\n")
|
||||||
|
end
|
||||||
|
function printyellow(x...)
|
||||||
|
foreach(z->printstyled(z,color=:yellow,bold=true),x)
|
||||||
|
print("\n")
|
||||||
|
end
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
|
BIN
Problemsets/.DS_Store
vendored
BIN
Problemsets/.DS_Store
vendored
Binary file not shown.
389
Problemsets/Data/FFmFactorsPs.csv
Normal file
389
Problemsets/Data/FFmFactorsPs.csv
Normal file
|
@ -0,0 +1,389 @@
|
||||||
|
date,Mkt-RF,SMB,HML,RF,Mom,ST_Rev,LT_Rev
|
||||||
|
197901,4.18,3.69,2.28,0.77,-1.26,5.49,-0.75
|
||||||
|
197902,-3.41,0.47,1.17,0.73,-1.08,1.28,0.94
|
||||||
|
197903,5.75,3.19,-0.67,0.81,2.91,1.38,-0.35
|
||||||
|
197904,0.05,2.18,1.06,0.8,0.81,-0.37,-0.4
|
||||||
|
197905,-2.18,0.34,1.62,0.82,-0.36,0.99,0.75
|
||||||
|
197906,3.88,1.17,1.42,0.81,0.84,1.2,-2.71
|
||||||
|
197907,0.73,1.31,1.75,0.77,-1.07,0.57,-2.02
|
||||||
|
197908,5.7,2.07,-1.53,0.77,-0.24,0.81,-1.74
|
||||||
|
197909,-0.69,-0.29,-0.91,0.83,5.33,-1.56,0.04
|
||||||
|
197910,-8.14,-3.33,-1.87,0.87,2.15,-1.96,1.52
|
||||||
|
197911,5.37,2.75,-3.34,0.99,7.95,1.37,-2.28
|
||||||
|
197912,1.87,4.24,-2.04,0.95,4.76,-1.93,-0.34
|
||||||
|
198001,5.76,1.68,1.78,0.8,7.47,-3.38,-0.85
|
||||||
|
198002,-0.79,-1.79,0.58,0.89,7.9,-4.94,-0.09
|
||||||
|
198003,-13.23,-6.64,-1.02,1.21,-9.61,8.38,2.61
|
||||||
|
198004,3.97,0.99,1.09,1.26,-0.43,1.36,-0.13
|
||||||
|
198005,5.2,2.13,0.38,0.81,-1.13,2.87,0.78
|
||||||
|
198006,3.16,1.69,-0.91,0.61,1.59,2.15,-2.92
|
||||||
|
198007,6.41,4.21,-6.27,0.53,0.36,2.93,-1.74
|
||||||
|
198008,1.72,3.91,-2.69,0.64,3.2,-0.99,-1.55
|
||||||
|
198009,2.2,0.87,-4.71,0.75,5.44,-1.44,-1.79
|
||||||
|
198010,1.06,2.45,-2.75,0.95,7.33,-1.99,-2.83
|
||||||
|
198011,9.54,-3.46,-8.52,0.96,15.21,-9.86,-7.79
|
||||||
|
198012,-4.75,-0.23,2.67,1.31,-6.63,6.36,3.43
|
||||||
|
198101,-5.05,3.02,6.88,1.04,-7.91,-2.29,7.51
|
||||||
|
198102,0.48,-0.29,1,1.07,-1.39,-2.22,2.65
|
||||||
|
198103,3.41,3.61,0.74,1.21,0.78,1.62,1.59
|
||||||
|
198104,-2.21,4.4,2.28,1.08,-0.96,-1.53,2.83
|
||||||
|
198105,0.21,2.05,-0.45,1.15,3.73,-3.74,1.55
|
||||||
|
198106,-2.37,-0.86,5.13,1.35,-5.9,4.53,1.01
|
||||||
|
198107,-1.55,-2.26,-0.53,1.24,-2.53,-0.58,-3.51
|
||||||
|
198108,-6.91,-1.93,4.78,1.28,-1.11,-0.67,1.27
|
||||||
|
198109,-7.62,-2.64,5.19,1.24,1.93,-1.11,5.23
|
||||||
|
198110,4.81,2.24,-4.36,1.21,4.06,4.62,-4.92
|
||||||
|
198111,3.51,-0.93,1.86,1.07,-0.26,2.58,0.62
|
||||||
|
198112,-3.68,1.2,0.72,0.87,1.32,1.63,1.4
|
||||||
|
198201,-3.42,-1.24,3.07,0.8,1.71,-1.22,6.42
|
||||||
|
198202,-6.03,0.5,6.03,0.92,4.94,-4.36,5.93
|
||||||
|
198203,-1.99,-0.22,3.86,0.98,2.98,-1.77,3.97
|
||||||
|
198204,3.2,1.46,-2.72,1.13,-0.41,1.62,-1.67
|
||||||
|
198205,-3.88,0.5,1.8,1.06,2.53,1.93,1.58
|
||||||
|
198206,-3.35,-0.39,1.57,0.96,4.99,0.16,1.94
|
||||||
|
198207,-3.1,0.89,0.23,1.05,4.47,-2.89,2.35
|
||||||
|
198208,11.14,-4.13,1.17,0.76,-3.52,1.1,-0.97
|
||||||
|
198209,1.17,2.94,0.34,0.51,4.18,4.3,-0.47
|
||||||
|
198210,11.27,2.34,-3.72,0.59,0.05,2.83,-0.95
|
||||||
|
198211,4.56,4.77,-1.93,0.63,5.89,-4.48,1.05
|
||||||
|
198212,0.78,-0.17,0.04,0.67,0.03,3.78,1.07
|
||||||
|
198301,3.5,2.66,-0.87,0.69,-1.67,3.17,0.06
|
||||||
|
198302,2.4,3.26,0.7,0.62,3.8,2.72,-0.46
|
||||||
|
198303,2.84,1.76,2.08,0.63,0.92,0.35,0.9
|
||||||
|
198304,6.71,0.53,0.58,0.71,1.8,-0.54,3.19
|
||||||
|
198305,0.63,6.12,-1.4,0.69,-1.48,2.76,-2.1
|
||||||
|
198306,3.11,0.92,-3.85,0.67,1.78,0.2,-0.91
|
||||||
|
198307,-3.9,1.51,5.65,0.74,-3.12,2.02,1.01
|
||||||
|
198308,-0.41,-4.3,5.46,0.76,-5.44,-0.61,0.09
|
||||||
|
198309,0.85,0.55,1.05,0.76,-0.11,2.33,0.82
|
||||||
|
198310,-3.56,-3.61,4.94,0.76,-4.56,-1.68,1.15
|
||||||
|
198311,2.26,2.03,-0.63,0.7,-0.11,4.88,-1.33
|
||||||
|
198312,-1.78,-0.29,1.64,0.73,0.79,1.9,1.36
|
||||||
|
198401,-2.06,-0.4,7.56,0.76,-2.52,0.67,4.24
|
||||||
|
198402,-4.62,-1.72,3.39,0.71,0.17,-2.02,0.22
|
||||||
|
198403,0.61,0.14,0.52,0.73,1.1,2.42,0.71
|
||||||
|
198404,-0.56,-1.2,1.28,0.81,2.08,1.54,-1.56
|
||||||
|
198405,-6.01,0.08,0.2,0.78,1.53,0.84,-0.22
|
||||||
|
198406,1.59,-0.27,-2.59,0.75,-0.7,0.94,-3.6
|
||||||
|
198407,-2.88,-2.28,0.28,0.82,2.93,-1.49,-1.63
|
||||||
|
198408,10.44,-0.22,-1.75,0.83,-5.74,7.28,-1.69
|
||||||
|
198409,-0.82,0.24,5.33,0.86,3.66,4.35,1.78
|
||||||
|
198410,-1.01,-1.17,0.46,1,3.23,-1.26,-1.79
|
||||||
|
198411,-1.8,-0.63,4.01,0.73,1.7,-0.66,1.02
|
||||||
|
198412,1.73,-0.57,-0.11,0.64,1.5,0.56,-3.1
|
||||||
|
198501,7.92,3.27,-5.35,0.65,-6.93,2.34,-1.93
|
||||||
|
198502,1.11,0.79,-0.23,0.58,1.82,2.42,-0.12
|
||||||
|
198503,-0.79,-1.12,3.98,0.62,1.69,-1.3,-0.69
|
||||||
|
198504,-0.94,0.18,3.72,0.72,3.05,-2.46,0.15
|
||||||
|
198505,4.92,-2.28,-0.91,0.66,3.97,-0.51,-4
|
||||||
|
198506,1.16,0.49,0.42,0.55,3.63,-0.63,-2.35
|
||||||
|
198507,-0.65,2.85,-1.63,0.62,-3.92,3.62,1.67
|
||||||
|
198508,-1.03,-0.37,2.2,0.55,1.79,1.82,0.11
|
||||||
|
198509,-4.58,-1.61,1.25,0.6,1.48,-1.81,-0.8
|
||||||
|
198510,3.79,-1.58,0.72,0.65,4.87,0.72,-2.46
|
||||||
|
198511,6.31,0.24,-2.87,0.61,-0.39,2.31,-2.39
|
||||||
|
198512,3.66,-0.46,-1.52,0.65,-0.13,-0.63,-3.13
|
||||||
|
198601,0.42,1.21,0.53,0.56,2.97,0.72,-2.83
|
||||||
|
198602,6.72,-0.59,-0.84,0.53,2.76,-1.64,-3.67
|
||||||
|
198603,4.79,-0.48,-0.44,0.6,2.45,-0.21,-2.24
|
||||||
|
198604,-1.31,2.82,-2.91,0.52,-0.5,2.83,-1.17
|
||||||
|
198605,4.59,-1.3,-0.11,0.49,2.03,0.97,-1.35
|
||||||
|
198606,0.9,-0.89,1.38,0.52,5.15,-2.36,-6.44
|
||||||
|
198607,-6.49,-3.43,4.71,0.52,1.8,-3.05,-4.05
|
||||||
|
198608,6.16,-4.17,3.53,0.46,-5.02,1.16,4.82
|
||||||
|
198609,-8.35,2.3,3.22,0.45,-5.86,-0.3,3.83
|
||||||
|
198610,4.47,-2.59,-1.48,0.46,2.7,3.55,0.64
|
||||||
|
198611,1.12,-1.96,-0.16,0.39,-0.31,0.66,0.12
|
||||||
|
198612,-3.13,0.07,0.36,0.49,0.37,0.56,0.53
|
||||||
|
198701,12.43,-1.73,-3.15,0.42,2.07,0.45,3.12
|
||||||
|
198702,4.36,3.45,-5.95,0.43,-2.16,-1.06,-0.16
|
||||||
|
198703,1.9,0.44,1.65,0.47,1.6,1.82,3.92
|
||||||
|
198704,-2.14,-1.69,-0.29,0.44,0.26,-1.89,3.92
|
||||||
|
198705,0.13,-0.56,0.17,0.38,-0.69,-0.28,2.22
|
||||||
|
198706,3.89,-2.1,1.13,0.48,-0.2,0.11,-1.42
|
||||||
|
198707,3.96,-0.68,0.67,0.46,2.69,-0.15,2.33
|
||||||
|
198708,3.24,-0.77,-0.94,0.47,-0.89,2.55,-3.05
|
||||||
|
198709,-2.53,0.62,0.28,0.45,0.61,-0.38,3.08
|
||||||
|
198710,-23.14,-8.41,4.17,0.6,-7.92,0.1,-6.26
|
||||||
|
198711,-7.58,2.61,3.18,0.35,-1.17,-0.77,-0.42
|
||||||
|
198712,6.64,0.18,-4.48,0.39,5.78,5.96,2.87
|
||||||
|
198801,4.2,-0.66,5.22,0.29,-7.63,7.9,-1.63
|
||||||
|
198802,4.71,3.34,-1.66,0.46,-1.54,4.35,1.53
|
||||||
|
198803,-2.1,6.15,0.77,0.44,0.64,-0.9,1.21
|
||||||
|
198804,0.64,0.96,1.73,0.46,2.25,0.33,0.45
|
||||||
|
198805,-0.47,-2.62,2.31,0.51,0.5,0.57,-0.13
|
||||||
|
198806,4.66,2.12,-1.14,0.49,-2.91,1.83,1.75
|
||||||
|
198807,-1.24,-0.19,2.28,0.51,0.64,3.4,-1.35
|
||||||
|
198808,-3.39,0.08,2.09,0.59,0.32,-1.95,-2.03
|
||||||
|
198809,3.1,-1.3,-0.74,0.62,0.24,-0.15,-1.95
|
||||||
|
198810,1.15,-2.94,1.66,0.61,1.32,-0.92,-0.12
|
||||||
|
198811,-2.21,-1.74,1.26,0.57,0.41,-0.67,-0.84
|
||||||
|
198812,1.48,1.93,-1.55,0.63,0.28,2.29,0.79
|
||||||
|
198901,6.06,-2.15,0.52,0.55,-0.14,1.12,2.81
|
||||||
|
198902,-2.25,2.74,0.87,0.61,0.87,1.06,0.9
|
||||||
|
198903,1.48,0.7,0.47,0.67,3.56,-0.87,0.15
|
||||||
|
198904,4.15,-0.53,-1.38,0.67,1.7,-0.72,-1.1
|
||||||
|
198905,3.14,-0.03,-0.84,0.79,1.54,-2.74,-1.7
|
||||||
|
198906,-1.2,-1.01,2.18,0.71,0.65,2.26,-0.71
|
||||||
|
198907,7.01,-3.98,-2.8,0.7,5.39,1.3,-1.8
|
||||||
|
198908,1.47,0.5,0.67,0.74,-0.14,1.89,0.17
|
||||||
|
198909,-0.8,0.34,-1.3,0.65,3.39,1.18,-2.83
|
||||||
|
198910,-3.61,-3.23,-0.98,0.68,1.43,-4.26,-3.55
|
||||||
|
198911,1.09,-1.26,-1.12,0.69,2.57,-1.77,-1.25
|
||||||
|
198912,1.22,-2.35,0.29,0.61,2.78,-2.54,-0.83
|
||||||
|
199001,-7.58,-1.25,0.87,0.57,-3.36,1.1,3.23
|
||||||
|
199002,0.92,1.15,0.66,0.57,-0.53,0.43,1.9
|
||||||
|
199003,1.77,1.5,-2.86,0.64,1.68,0.21,-0.17
|
||||||
|
199004,-3.52,-0.44,-2.48,0.69,2.47,-2.04,-1.12
|
||||||
|
199005,8.21,-2.48,-3.63,0.68,3.02,-0.44,0.93
|
||||||
|
199006,-1.05,1.37,-2.11,0.63,2.41,-0.17,-1.47
|
||||||
|
199007,-1.62,-3.18,-0.05,0.68,5.95,-0.56,-1.79
|
||||||
|
199008,-9.85,-3.57,1.49,0.66,1.79,-3.51,1.62
|
||||||
|
199009,-5.98,-3.63,0.67,0.6,5.52,-6,0.71
|
||||||
|
199010,-1.93,-5.56,0.26,0.68,6.73,-2.64,-3.5
|
||||||
|
199011,6,0.32,-2.96,0.57,-5.69,5.91,-0.64
|
||||||
|
199012,2.35,0.78,-1.49,0.6,0.01,-2.98,-2.39
|
||||||
|
199101,4.39,3.85,-1.73,0.52,-6.52,-2.04,6.15
|
||||||
|
199102,7.1,3.9,-0.58,0.48,-4.83,1.97,1.12
|
||||||
|
199103,2.45,3.92,-1.19,0.44,2.73,0.49,-0.23
|
||||||
|
199104,-0.2,0.53,1.43,0.53,-2.42,1.6,-1.19
|
||||||
|
199105,3.6,-0.31,-0.56,0.47,-0.12,0.35,1.4
|
||||||
|
199106,-4.82,0.06,1.23,0.42,0.4,2.14,-1.69
|
||||||
|
199107,4.19,-0.97,-1.29,0.49,4.34,2.06,0.82
|
||||||
|
199108,2.22,1.59,-0.8,0.46,1.59,-2.92,-0.43
|
||||||
|
199109,-1.56,1.68,-1.06,0.46,1.74,-0.81,-0.82
|
||||||
|
199110,1.36,0.85,-0.39,0.42,3.21,-1.28,2.25
|
||||||
|
199111,-4.12,-0.55,-1.77,0.39,1.26,-0.4,-2.67
|
||||||
|
199112,10.3,-2.26,-4.02,0.38,8.3,1.02,-0.85
|
||||||
|
199201,-0.46,8.43,4.63,0.34,-2.47,5.29,9.48
|
||||||
|
199202,1.06,0.87,6.37,0.28,-0.66,-2.69,5.41
|
||||||
|
199203,-2.71,-1.08,3.72,0.34,-0.35,-1.09,1.41
|
||||||
|
199204,1.02,-6.08,4.33,0.32,-2.6,-3.37,0.71
|
||||||
|
199205,0.36,0.42,1.17,0.28,0.11,-0.18,-0.47
|
||||||
|
199206,-2.25,-3.07,3.32,0.32,-0.61,0.86,1.32
|
||||||
|
199207,3.68,-0.39,-0.48,0.31,1.43,2.05,-1.26
|
||||||
|
199208,-2.34,-0.11,-1.09,0.26,-0.51,-1.57,-4.1
|
||||||
|
199209,0.98,0.58,-0.2,0.26,1.44,-1.12,-0.02
|
||||||
|
199210,0.87,2.07,-2.05,0.23,2.74,-0.22,-1.06
|
||||||
|
199211,3.79,3.63,-1.53,0.23,-0.32,-1.07,0.27
|
||||||
|
199212,1.5,1.63,2.53,0.28,4.47,1.21,0.8
|
||||||
|
199301,1.03,2.03,5.83,0.23,4.82,-0.52,5.54
|
||||||
|
199302,0.32,-3.36,6.5,0.22,3.11,-1.41,4.23
|
||||||
|
199303,2.26,0.23,1.23,0.25,3.74,-0.92,1.74
|
||||||
|
199304,-2.78,-0.64,2.65,0.24,0.33,-1.77,2.46
|
||||||
|
199305,2.74,1.97,-3.45,0.22,0.28,1.45,1.29
|
||||||
|
199306,0.29,-0.31,2.61,0.25,4.59,2.25,1.64
|
||||||
|
199307,-0.32,0.95,3.24,0.24,3.24,-2.7,0.99
|
||||||
|
199308,3.7,0.22,-0.4,0.25,2.58,1.3,1.23
|
||||||
|
199309,-0.2,3.08,-0.45,0.26,3.31,0.67,0.5
|
||||||
|
199310,1.59,1.45,-1.59,0.22,-2.7,1.96,1.78
|
||||||
|
199311,-2.01,-1.41,-0.31,0.25,-4.71,-1.37,1.49
|
||||||
|
199312,1.72,1.24,0.6,0.23,2.37,1.15,0.48
|
||||||
|
199401,2.9,0.11,2.15,0.25,0.09,1.75,3.78
|
||||||
|
199402,-2.63,2.72,-1.37,0.21,-0.27,0.42,-0.18
|
||||||
|
199403,-4.85,-0.9,1.29,0.27,-1.31,0.13,0.54
|
||||||
|
199404,0.68,-0.88,1.66,0.27,0.39,-0.35,0.17
|
||||||
|
199405,0.62,-2.05,0.12,0.32,-2.22,0.07,1.64
|
||||||
|
199406,-3.1,-0.49,1.71,0.31,-0.83,0.18,0.46
|
||||||
|
199407,2.78,-1.78,0.98,0.28,0.18,1.4,0.5
|
||||||
|
199408,3.89,1.45,-3.47,0.37,1.54,1.47,2.35
|
||||||
|
199409,-2.21,2.7,-1.83,0.37,1.29,-0.74,1.71
|
||||||
|
199410,1.07,-2.21,-2.35,0.38,1.48,-0.25,0.54
|
||||||
|
199411,-4.09,-0.16,-0.07,0.37,-0.19,-2.1,-1.63
|
||||||
|
199412,0.82,0.04,0.22,0.44,3.51,0.82,0.1
|
||||||
|
199501,1.62,-2.95,1.67,0.42,-1.82,4.05,-0.65
|
||||||
|
199502,3.56,-0.33,0.37,0.4,-0.33,2.38,-1.01
|
||||||
|
199503,2.24,-0.35,-2.04,0.46,0.38,-0.13,1.11
|
||||||
|
199504,2.06,-0.41,1.73,0.44,1.85,-0.58,0.36
|
||||||
|
199505,2.86,-2.24,1.97,0.54,-0.43,-0.44,-1.13
|
||||||
|
199506,2.65,3.07,-2.99,0.47,2.92,1.21,-1.85
|
||||||
|
199507,3.63,2.22,-2.15,0.45,2.57,-2.24,-0.97
|
||||||
|
199508,0.46,1.84,1.89,0.47,-0.02,1.32,-1.14
|
||||||
|
199509,3.21,-2.02,-0.92,0.43,2.63,-0.86,-1.01
|
||||||
|
199510,-1.6,-3.99,-0.08,0.47,4.14,-1.05,0.51
|
||||||
|
199511,3.85,-0.81,0.34,0.42,-0.6,0.56,0.55
|
||||||
|
199512,1.03,0.42,1.41,0.49,2.56,-0.95,3.96
|
||||||
|
199601,2.38,-2.44,0.36,0.43,0.55,1.08,1.34
|
||||||
|
199602,1.24,2.06,-2.32,0.39,0.58,2.27,-1.39
|
||||||
|
199603,0.7,1.31,1.23,0.39,-1.89,-0.08,-0.24
|
||||||
|
199604,2.09,4.89,-3.98,0.46,-0.93,0.7,-0.78
|
||||||
|
199605,2.26,3.22,-1.38,0.42,1.55,-1.08,1.16
|
||||||
|
199606,-1.23,-3.68,1.93,0.4,0.95,-0.22,-0.13
|
||||||
|
199607,-5.83,-3.57,4.39,0.45,-0.14,-2.48,0
|
||||||
|
199608,2.84,2.31,-0.59,0.41,-0.12,2.55,-1.47
|
||||||
|
199609,4.86,-0.82,-3.83,0.44,2.7,0.54,-2.12
|
||||||
|
199610,0.95,-4.11,4.81,0.42,3.82,-1.1,0.06
|
||||||
|
199611,6.15,-3.6,0.21,0.41,-2.39,0.17,-2.49
|
||||||
|
199612,-1.6,3.15,0.94,0.46,0.6,0.54,-0.57
|
||||||
|
199701,4.9,-1.51,-2.3,0.45,1.95,2.71,-1.58
|
||||||
|
199702,-0.5,-2.57,4.72,0.39,-2.02,1.27,0.84
|
||||||
|
199703,-4.92,-0.3,3.88,0.43,0.98,0.89,2.11
|
||||||
|
199704,3.81,-5.14,-1,0.43,4.81,2.39,-4.09
|
||||||
|
199705,6.67,4.79,-4.35,0.49,-5.2,2.83,-2.18
|
||||||
|
199706,4.04,1.52,0.7,0.37,2.67,0.81,0.19
|
||||||
|
199707,7.22,-2.42,-0.38,0.43,3.84,3.81,-1.16
|
||||||
|
199708,-4.04,7.48,1.03,0.41,-2.53,-0.07,3.18
|
||||||
|
199709,5.41,2.68,-0.28,0.44,1.46,1.68,-0.4
|
||||||
|
199710,-3.86,-0.81,2.31,0.42,-0.44,-0.97,0.35
|
||||||
|
199711,2.65,-5.05,1.03,0.39,0.29,-2.01,1.01
|
||||||
|
199712,1.3,-2.34,3.82,0.48,3.84,-3.05,0.12
|
||||||
|
199801,0.02,-1.01,-1.82,0.43,0.08,4.98,-1.11
|
||||||
|
199802,6.94,0.21,-0.72,0.39,-1.1,1.96,-1.18
|
||||||
|
199803,4.74,-1.18,1.67,0.39,2.14,1.84,2.3
|
||||||
|
199804,0.66,0.46,0.31,0.43,0.78,1.72,-0.93
|
||||||
|
199805,-2.97,-3.58,4.27,0.4,1.86,0.7,1.49
|
||||||
|
199806,2.78,-3.28,-1.84,0.41,7.26,-1.48,-0.75
|
||||||
|
199807,-2.74,-4.88,-1.08,0.4,3.69,-1.99,0.46
|
||||||
|
199808,-16.21,-5.7,5.14,0.43,1.91,-2.66,4.47
|
||||||
|
199809,5.92,-0.16,-3.96,0.46,-0.61,2.82,0.24
|
||||||
|
199810,7.12,-3.19,-2.71,0.32,-5.36,9.86,-4.23
|
||||||
|
199811,5.89,1.12,-3.29,0.31,1.18,1.22,-0.38
|
||||||
|
199812,5.93,-0.27,-4.8,0.38,9.03,-1.63,-1
|
||||||
|
199901,3.5,0.67,-5.61,0.35,3.02,-2.28,1.09
|
||||||
|
199902,-4.16,-5.68,1.71,0.35,-0.13,2.34,0.37
|
||||||
|
199903,3.36,-3.89,-2.88,0.43,-1.36,0.21,-1.09
|
||||||
|
199904,4.54,3.37,2.45,0.37,-9.14,2.36,5.14
|
||||||
|
199905,-2.41,3.69,2.69,0.34,-5.25,1.12,3.39
|
||||||
|
199906,4.68,3.37,-4.3,0.4,4.96,1.58,0.36
|
||||||
|
199907,-3.45,2.23,0.54,0.38,1.6,1.4,1.93
|
||||||
|
199908,-1.39,-1.24,-0.99,0.39,3.06,-3.51,-1.55
|
||||||
|
199909,-2.67,3.25,-3.08,0.39,6.49,-0.82,0.23
|
||||||
|
199910,5.82,-6.78,-3.26,0.39,5.48,-1.18,-2.76
|
||||||
|
199911,3.32,7.66,-7.99,0.36,5.57,0.09,5.14
|
||||||
|
199912,7.94,6.82,-9.19,0.44,13.2,-14.05,5.66
|
||||||
|
200001,-4.37,4.39,0.23,0.41,1.9,2.71,1.87
|
||||||
|
200002,2.75,22.06,-12.87,0.43,18.4,-13.35,7.59
|
||||||
|
200003,4.88,-16.62,7.91,0.47,-6.85,10.08,-5.17
|
||||||
|
200004,-6.41,-7.64,9.26,0.46,-8.47,-1.95,0.35
|
||||||
|
200005,-4.4,-4.66,3.8,0.5,-9.1,-8.73,-1.71
|
||||||
|
200006,4.76,13.74,-9.92,0.4,16.55,12.34,2.36
|
||||||
|
200007,-2.19,-2.79,8.45,0.48,-0.08,6.48,-3.27
|
||||||
|
200008,7.09,-0.9,-1.29,0.5,5.77,0.62,1.04
|
||||||
|
200009,-5.62,-1.84,6.87,0.51,2.18,2.49,-2.64
|
||||||
|
200010,-3.02,-3.63,4.8,0.56,-4.7,-4.74,-1.1
|
||||||
|
200011,-10.76,-3.07,12.39,0.51,-2.49,-14.52,0.35
|
||||||
|
200012,1.54,1.59,6.13,0.5,6.83,-6.63,2.91
|
||||||
|
200101,3.41,6.98,-5.67,0.54,-25.01,16.29,-2.61
|
||||||
|
200102,-10.32,-1.14,13.88,0.39,12.56,12.34,11
|
||||||
|
200103,-7.47,0.53,6.37,0.44,8.4,-7.67,3.9
|
||||||
|
200104,7.99,0.28,-4.39,0.39,-8.12,9.97,-3.79
|
||||||
|
200105,0.74,3.01,2.82,0.32,2.13,3.67,3.85
|
||||||
|
200106,-2.03,6.39,-2.11,0.28,0.3,0.46,2.08
|
||||||
|
200107,-2.13,-4.18,5.61,0.3,5.58,1.07,2.8
|
||||||
|
200108,-6.21,2.18,3.32,0.31,5.61,-3.96,5.92
|
||||||
|
200109,-9.43,-6.53,1.63,0.28,11.54,-4.9,0.05
|
||||||
|
200110,2.56,6.83,-6.99,0.22,-8.42,14.85,-1.66
|
||||||
|
200111,7.71,0.39,0.82,0.17,-8.62,-3.11,0.24
|
||||||
|
200112,1.64,5.12,0.39,0.15,0.01,1.68,1.26
|
||||||
|
200201,-1.74,1.15,3.46,0.14,3.73,1.59,3.52
|
||||||
|
200202,-2.3,-1.67,3.92,0.13,6.81,-0.11,4.09
|
||||||
|
200203,4.34,4.34,1.14,0.13,-1.68,2.85,2.11
|
||||||
|
200204,-5.11,5.84,4.21,0.15,7.92,2.08,6.12
|
||||||
|
200205,-1.19,-3.68,2.46,0.14,3.05,-2.7,2.4
|
||||||
|
200206,-7.16,3.55,1.47,0.13,6.19,-3.38,2.15
|
||||||
|
200207,-8.26,-5.16,-3.65,0.15,3.4,1.48,-2.94
|
||||||
|
200208,0.66,-2.19,2.17,0.14,1.73,1.39,-0.59
|
||||||
|
200209,-10.14,2.69,1.16,0.14,9.14,-1.8,-0.59
|
||||||
|
200210,7.35,-3.01,-6.51,0.14,-5.49,7.53,-0.38
|
||||||
|
200211,6.01,3.18,-1.54,0.12,-16.31,0.27,4.05
|
||||||
|
200212,-5.44,-0.52,3.86,0.11,9.65,10.58,2.26
|
||||||
|
200301,-2.44,1.4,-0.9,0.1,1.56,0.25,0.41
|
||||||
|
200302,-1.63,-0.27,-1.46,0.09,1.22,-0.8,-1.9
|
||||||
|
200303,0.93,0.83,-1.73,0.1,1.51,-0.22,-2.66
|
||||||
|
200304,8.18,1.11,-0.02,0.1,-9.44,3.17,-0.92
|
||||||
|
200305,6.26,4.69,0.16,0.09,-10.77,-1.44,3.47
|
||||||
|
200306,1.53,1.48,0.66,0.1,-0.97,-0.18,1.38
|
||||||
|
200307,2.24,5.62,-2.08,0.07,-0.29,2.59,0.37
|
||||||
|
200308,2.43,2.65,1.79,0.07,-0.54,-1.47,-0.35
|
||||||
|
200309,-0.99,0.57,0.92,0.08,-0.07,4.59,1.09
|
||||||
|
200310,5.96,2.87,1.84,0.07,3.88,2.04,2.02
|
||||||
|
200311,1.59,2.2,1.46,0.07,1.59,-1.44,1.93
|
||||||
|
200312,4.47,-2.78,2.64,0.08,-5.69,1.7,2.06
|
||||||
|
200401,2.23,2.61,1.66,0.07,2.58,2.24,1.91
|
||||||
|
200402,1.49,-1.15,0.34,0.06,-1.13,1.45,-2.95
|
||||||
|
200403,-1.16,1.87,-0.01,0.09,0.19,-2.17,-1.7
|
||||||
|
200404,-2.5,-2.56,-1.7,0.08,-5.38,-0.22,-2.36
|
||||||
|
200405,1.35,-0.15,-0.26,0.06,1.65,2.27,0.89
|
||||||
|
200406,2.08,2.28,1.68,0.08,2.08,0.31,1.79
|
||||||
|
200407,-3.87,-3.82,4.39,0.1,-2.31,-1.37,-2.51
|
||||||
|
200408,0.16,-1.54,1.12,0.11,-1.52,-2.41,-2.77
|
||||||
|
200409,1.94,2.85,0.34,0.11,5.26,3.05,-1.25
|
||||||
|
200410,1.67,0.42,-0.89,0.11,-1.5,0.14,0.98
|
||||||
|
200411,4.67,4.12,1.91,0.15,3.22,1.46,-1.16
|
||||||
|
200412,3.36,0.19,-0.35,0.16,-2.83,0.92,0.87
|
||||||
|
200501,-2.82,-1.62,2.49,0.16,3.12,1.57,-2.9
|
||||||
|
200502,2.11,-0.75,2.78,0.16,3.24,-2.33,-1.49
|
||||||
|
200503,-1.9,-1.3,1.65,0.21,0.92,0.16,-0.72
|
||||||
|
200504,-2.73,-3.96,-0.48,0.21,-0.83,-1.82,-1.48
|
||||||
|
200505,3.56,2.95,-1.17,0.24,0.43,3.18,0.48
|
||||||
|
200506,0.92,2.56,2.75,0.23,2.06,0.11,-1.49
|
||||||
|
200507,4.09,2.76,-0.46,0.24,0.04,-0.17,0.99
|
||||||
|
200508,-0.89,-0.89,1.42,0.3,2.21,-1.46,0.35
|
||||||
|
200509,0.77,-0.63,1.17,0.29,3.47,-3.7,-0.05
|
||||||
|
200510,-2.35,-1.04,-0.7,0.27,-1.29,1.56,-1.03
|
||||||
|
200511,3.73,1,-1.81,0.31,0.37,0.15,0.28
|
||||||
|
200512,0.03,-0.48,0.48,0.32,0.76,1.94,-0.86
|
||||||
|
200601,3.65,5.39,1.14,0.35,2.75,-1.09,1.16
|
||||||
|
200602,-0.5,-0.36,-0.83,0.34,-1.8,3.3,1.26
|
||||||
|
200603,1.54,3.54,-0.07,0.37,1.23,0.06,0.44
|
||||||
|
200604,0.94,-1.23,3.08,0.36,0.66,-0.34,-0.42
|
||||||
|
200605,-3.53,-2.99,2.78,0.43,-3.72,0.02,-0.16
|
||||||
|
200606,-0.44,-0.49,1.5,0.4,1.48,-0.55,-1.11
|
||||||
|
200607,-0.59,-3.94,3.3,0.4,-2.18,0.25,1.37
|
||||||
|
200608,2.09,0.81,-1.73,0.42,-3.46,1.74,2.92
|
||||||
|
200609,1.53,-1.21,-0.43,0.41,-0.94,0.74,1.65
|
||||||
|
200610,3.3,1.65,0.48,0.41,-0.23,-0.21,-0.46
|
||||||
|
200611,1.95,0.73,0.51,0.42,-1.01,-0.95,-1.93
|
||||||
|
200612,0.68,-0.89,2.54,0.4,0.83,2.38,1.93
|
||||||
|
200701,1.5,0.06,-0.1,0.44,0.26,1.07,-0.68
|
||||||
|
200702,-1.78,1.43,0.24,0.38,-1.32,0.3,-0.64
|
||||||
|
200703,0.87,-0.23,0.43,0.43,2.49,-2.02,-1.18
|
||||||
|
200704,3.55,-2.09,-0.96,0.44,-0.17,-0.28,0.28
|
||||||
|
200705,3.48,-0.06,-0.09,0.41,-0.32,-1.09,-2.48
|
||||||
|
200706,-1.87,0.67,-1.03,0.4,0.25,-0.84,-0.29
|
||||||
|
200707,-3.57,-2.7,-2.93,0.4,2.79,-3.35,-2.06
|
||||||
|
200708,0.75,-0.14,-2.37,0.42,0.11,-2.57,-0.24
|
||||||
|
200709,3.77,-2.47,-2.13,0.32,4.61,0.31,-2.78
|
||||||
|
200710,2.26,0.09,-1.92,0.32,4.89,-3.92,-1.58
|
||||||
|
200711,-5.27,-2.73,-1.02,0.34,0.89,-1.73,0.45
|
||||||
|
200712,-0.7,0.07,-0.05,0.27,6.51,-2.09,-2.28
|
||||||
|
200801,-6.44,-0.76,3.01,0.21,-7.84,6.97,1.76
|
||||||
|
200802,-2.33,-0.55,0.05,0.13,6.14,5.05,-2.18
|
||||||
|
200803,-1.21,0.86,0.17,0.17,4.14,-0.31,2.73
|
||||||
|
200804,4.94,-1.55,0.07,0.18,-0.21,3.13,-4.53
|
||||||
|
200805,2.21,2.84,-0.26,0.18,3.2,-2.31,-4.52
|
||||||
|
200806,-8.03,1.05,-1.06,0.17,12.52,-7.09,-3.18
|
||||||
|
200807,-1.47,3.74,3.59,0.15,-5.15,8.21,7.84
|
||||||
|
200808,0.99,3.79,1.48,0.13,-3.81,-0.63,1.06
|
||||||
|
200809,-9.97,-0.23,4.42,0.15,0.39,-3.74,5.8
|
||||||
|
200810,-18.55,-2.17,-3.07,0.08,7.81,-9.47,2.45
|
||||||
|
200811,-8.56,-3.57,-4.99,0.03,7.18,-10.22,-2.4
|
||||||
|
200812,2.06,3.99,-1.18,0.09,-5.03,5.3,0.3
|
||||||
|
200901,-7.75,-0.9,-9.93,0,-1.92,0.49,-7.12
|
||||||
|
200902,-10.12,-0.41,-6.75,0.01,4.25,-6.69,1.15
|
||||||
|
200903,8.75,0.75,2.57,0.02,-11.51,3.35,3.92
|
||||||
|
200904,11.04,5.16,5.68,0.01,-34.75,-6.94,-0.35
|
||||||
|
200905,6.73,-2.64,0.45,0,-12.49,-2.82,-1.97
|
||||||
|
200906,-0.28,2.65,-2.45,0.01,5.37,5.07,1.32
|
||||||
|
200907,8.23,2.47,4.82,0.01,-5.51,4.39,5.04
|
||||||
|
200908,3.18,-0.56,7.68,0.01,-8.87,-2.1,5.48
|
||||||
|
200909,4.52,2.33,1.47,0.01,-4.97,1.23,-1.78
|
||||||
|
200910,-2.84,-4.28,-4.37,0,2.65,2.12,-4.76
|
||||||
|
200911,5.74,-2.84,0.13,0,0.41,0.38,1.44
|
||||||
|
200912,2.91,5.89,0.72,0.01,2.94,1.57,0.7
|
||||||
|
201001,-3.71,0.43,0.57,0,-5.34,3.66,1.64
|
||||||
|
201002,3.53,1.39,2.75,0,3.61,1.83,2.58
|
||||||
|
201003,6.44,1.57,2.01,0.01,3.72,0.87,4.3
|
||||||
|
201004,2.02,5.01,3.14,0,3.2,-1.81,4.8
|
||||||
|
201005,-8,-0.01,-2.36,0.01,-0.26,0.55,-3.03
|
||||||
|
201006,-5.21,-2.08,-4.31,0.01,-2.88,-4.88,-5.34
|
||||||
|
201007,7.1,0.13,0.23,0.01,1.95,4.05,0.65
|
||||||
|
201008,-4.4,-2.91,-1.71,0.01,-0.12,1.06,-3.82
|
||||||
|
201009,9.24,3.96,-3.05,0.01,1.35,3.2,1.97
|
||||||
|
201010,3.88,0.94,-2.21,0.01,1.59,-1.37,-2.23
|
||||||
|
201011,0.56,3.78,-0.66,0.01,2.5,-0.91,-0.73
|
||||||
|
201012,6.77,0.78,3.53,0.01,-3.18,2.34,4.8
|
||||||
|
201101,2.01,-2.46,0.96,0.01,-0.22,0.02,1.03
|
||||||
|
201102,3.85,1.79,1.38,0.01,1.99,-0.99,1.29
|
||||||
|
201103,0.28,2.73,-1.31,0.01,3.55,-0.89,-1.91
|
||||||
|
201104,2.82,-0.23,-2.31,0,0.07,-1,-2.15
|
|
13953
Problemsets/Data/Options_prices_US_Canada.csv
Normal file
13953
Problemsets/Data/Options_prices_US_Canada.csv
Normal file
File diff suppressed because it is too large
Load Diff
BIN
Problemsets/Data/PS08_Data.xlsx
Normal file
BIN
Problemsets/Data/PS08_Data.xlsx
Normal file
Binary file not shown.
420
Problemsets/PS07_Optimisation.ipynb
Normal file
420
Problemsets/PS07_Optimisation.ipynb
Normal file
File diff suppressed because one or more lines are too long
579
Problemsets/PS08_Volatility.ipynb
Normal file
579
Problemsets/PS08_Volatility.ipynb
Normal file
File diff suppressed because one or more lines are too long
271
Problemsets/PS09a_PyCall.ipynb
Normal file
271
Problemsets/PS09a_PyCall.ipynb
Normal file
|
@ -0,0 +1,271 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Python\n",
|
||||||
|
"\n",
|
||||||
|
"using the [PyCall.jl](https://github.com/JuliaPy/PyCall.jl) package."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"CovNWFn"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"using Printf, DelimitedFiles, LinearAlgebra, Statistics\n",
|
||||||
|
"\n",
|
||||||
|
"include(\"jlFiles/printmat.jl\")\n",
|
||||||
|
"include(\"jlFiles/OlsNW.jl\") #functions for OLS"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Sample size: (388,)\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"x = readdlm(\"Data/FFmFactorsPs.csv\",',',skipstart=1)\n",
|
||||||
|
"\n",
|
||||||
|
" #yearmonth, market, small minus big, high minus low\n",
|
||||||
|
"(ym,Rme,RSMB,RHML) = (x[:,1],x[:,2]/100,x[:,3]/100,x[:,4]/100) \n",
|
||||||
|
"x = nothing\n",
|
||||||
|
"\n",
|
||||||
|
"printlnPs(\"Sample size:\",size(Rme))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Do OLS (in Julia)\n",
|
||||||
|
"\n",
|
||||||
|
"use the function sin the file OlsNW.jl to do OLS. Report point estimates and standard errors."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\u001b[34m\u001b[1mOLS Results (assuming iid residuals):\u001b[22m\u001b[39m\n",
|
||||||
|
"\n",
|
||||||
|
" b std_iid\n",
|
||||||
|
"c 0.007 0.002\n",
|
||||||
|
"SMB 0.217 0.073\n",
|
||||||
|
"HML -0.429 0.074\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"Y = Rme\n",
|
||||||
|
"T = size(Y,1)\n",
|
||||||
|
"X = [ones(T) RSMB RHML]\n",
|
||||||
|
"\n",
|
||||||
|
"(b,u,Yhat,V,R2) = OlsGMFn(Y,X)\n",
|
||||||
|
"std_iid = sqrt.(diag(V))\n",
|
||||||
|
"\n",
|
||||||
|
"printblue(\"OLS Results (assuming iid residuals):\\n\")\n",
|
||||||
|
"xNames = [\"c\",\"SMB\",\"HML\"]\n",
|
||||||
|
"printmat([b std_iid],colNames=[\"b\",\"std_iid\"],rowNames=xNames)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Getting Started with PyCall"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"using PyCall\n",
|
||||||
|
"sm = pyimport(\"statsmodels.api\"); #activate this package"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"PyObject <class 'statsmodels.iolib.summary.Summary'>\n",
|
||||||
|
"\"\"\"\n",
|
||||||
|
" OLS Regression Results \n",
|
||||||
|
"==============================================================================\n",
|
||||||
|
"Dep. Variable: y R-squared: 0.134\n",
|
||||||
|
"Model: OLS Adj. R-squared: 0.130\n",
|
||||||
|
"Method: Least Squares F-statistic: 29.85\n",
|
||||||
|
"Date: Mon, 08 Nov 2021 Prob (F-statistic): 8.88e-13\n",
|
||||||
|
"Time: 16:17:13 Log-Likelihood: 672.28\n",
|
||||||
|
"No. Observations: 388 AIC: -1339.\n",
|
||||||
|
"Df Residuals: 385 BIC: -1327.\n",
|
||||||
|
"Df Model: 2 \n",
|
||||||
|
"Covariance Type: nonrobust \n",
|
||||||
|
"==============================================================================\n",
|
||||||
|
" coef std err t P>|t| [0.025 0.975]\n",
|
||||||
|
"------------------------------------------------------------------------------\n",
|
||||||
|
"const 0.0070 0.002 3.167 0.002 0.003 0.011\n",
|
||||||
|
"x1 0.2170 0.074 2.949 0.003 0.072 0.362\n",
|
||||||
|
"x2 -0.4291 0.074 -5.821 0.000 -0.574 -0.284\n",
|
||||||
|
"==============================================================================\n",
|
||||||
|
"Omnibus: 58.863 Durbin-Watson: 1.849\n",
|
||||||
|
"Prob(Omnibus): 0.000 Jarque-Bera (JB): 146.539\n",
|
||||||
|
"Skew: -0.749 Prob(JB): 1.51e-32\n",
|
||||||
|
"Kurtosis: 5.612 Cond. No. 38.8\n",
|
||||||
|
"==============================================================================\n",
|
||||||
|
"\n",
|
||||||
|
"Notes:\n",
|
||||||
|
"[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n",
|
||||||
|
"\"\"\"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"resultsP = sm.OLS(Y, X).fit() #can use Python functions directly\n",
|
||||||
|
"\n",
|
||||||
|
"println(resultsP.summary())"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[:HC0_se, :HC1_se, :HC2_se, :HC3_se, :_HCCM, :__class__, :__delattr__, :__dict__, :__dir__, :__doc__, :__eq__, :__format__, :__ge__, :__getattribute__, :__gt__, :__hash__, :__init__, :__init_subclass__, :__le__, :__lt__, :__module__, :__ne__, :__new__, :__reduce__, :__reduce_ex__, :__repr__, :__setattr__, :__sizeof__, :__str__, :__subclasshook__, :__weakref__, :_abat_diagonal, :_cache, :_data_attr, :_data_in_cache, :_get_robustcov_results, :_is_nested, :_use_t, :_wexog_singular_values, :aic, :bic, :bse, :centered_tss, :compare_f_test, :compare_lm_test, :compare_lr_test, :condition_number, :conf_int, :conf_int_el, :cov_HC0, :cov_HC1, :cov_HC2, :cov_HC3, :cov_kwds, :cov_params, :cov_type, :df_model, :df_resid, :diagn, :eigenvals, :el_test, :ess, :f_pvalue, :f_test, :fittedvalues, :fvalue, :get_influence, :get_prediction, :get_robustcov_results, :info_criteria, :initialize, :k_constant, :llf, :load, :model, :mse_model, :mse_resid, :mse_total, :nobs, :normalized_cov_params, :outlier_test, :params, :predict, :pvalues, :remove_data, :resid, :resid_pearson, :rsquared, :rsquared_adj, :save, :scale, :ssr, :summary, :summary2, :t_test, :t_test_pairwise, :tvalues, :uncentered_tss, :use_t, :wald_test, :wald_test_terms, :wresid]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"println(keys(resultsP)) #print all keys (field names)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Task 1\n",
|
||||||
|
"\n",
|
||||||
|
"Print the Julia and Python estimates (of the coefficients) in a table so we can compare directly."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Task 2\n",
|
||||||
|
"\n",
|
||||||
|
"Print the smallest and largest values of the difference between the residuals according to Julia and those according to Python."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# OLS (in Julia) with Robust Standard Errors\n",
|
||||||
|
"\n",
|
||||||
|
"Use standard errors that are robust to heteroskedastcity and autocorrelation (2 lags)."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 9,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\u001b[34m\u001b[1mOLS Results (robust std):\u001b[22m\u001b[39m\n",
|
||||||
|
"\n",
|
||||||
|
" b std_nw\n",
|
||||||
|
"c 0.007 0.002\n",
|
||||||
|
"SMB 0.217 0.129\n",
|
||||||
|
"HML -0.429 0.118\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"(b,u,Yhat,V,R2) = OlsNWFn(Y,X,2)\n",
|
||||||
|
"std_nw = sqrt.(diag(V))\n",
|
||||||
|
"\n",
|
||||||
|
"printblue(\"OLS Results (robust std):\\n\")\n",
|
||||||
|
"xNames = [\"c\",\"SMB\",\"HML\"]\n",
|
||||||
|
"printmat([b std_nw],colNames=[\"b\",\"std_nw\"],rowNames=xNames)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Task 3 \n",
|
||||||
|
"\n",
|
||||||
|
"Now redo the Python estimation with the same sort of robust standard errors. Hint: `resultsP.get_robustcov_results()`"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"@webio": {
|
||||||
|
"lastCommId": null,
|
||||||
|
"lastKernelId": null
|
||||||
|
},
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Julia 1.6.3",
|
||||||
|
"language": "julia",
|
||||||
|
"name": "julia-1.6"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"file_extension": ".jl",
|
||||||
|
"mimetype": "application/julia",
|
||||||
|
"name": "julia",
|
||||||
|
"version": "1.6.3"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 4
|
||||||
|
}
|
284
Problemsets/PS09b_RCall.ipynb
Normal file
284
Problemsets/PS09b_RCall.ipynb
Normal file
|
@ -0,0 +1,284 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# R\n",
|
||||||
|
"\n",
|
||||||
|
"using the [RCall.jl](https://juliainterop.github.io/RCall.jl/stable/) package."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"CovNWFn"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"using Printf, DelimitedFiles, LinearAlgebra, Statistics\n",
|
||||||
|
"\n",
|
||||||
|
"include(\"jlFiles/printmat.jl\")\n",
|
||||||
|
"include(\"jlFiles/OlsNW.jl\") #functions for OLS"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Sample size: (388,)\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"x = readdlm(\"Data/FFmFactorsPs.csv\",',',skipstart=1)\n",
|
||||||
|
"\n",
|
||||||
|
" #yearmonth, market, small minus big, high minus low\n",
|
||||||
|
"(ym,Rme,RSMB,RHML) = (x[:,1],x[:,2]/100,x[:,3]/100,x[:,4]/100) \n",
|
||||||
|
"x = nothing\n",
|
||||||
|
"\n",
|
||||||
|
"printlnPs(\"Sample size:\",size(Rme))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Do OLS (in Julia)\n",
|
||||||
|
"\n",
|
||||||
|
"use the function sin the file OlsNW.jl to do OLS. Report point estimates and standard errors."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\u001b[34m\u001b[1mOLS Results (assuming iid residuals):\u001b[22m\u001b[39m\n",
|
||||||
|
"\n",
|
||||||
|
" b std_iid\n",
|
||||||
|
"c 0.007 0.002\n",
|
||||||
|
"SMB 0.217 0.073\n",
|
||||||
|
"HML -0.429 0.074\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"Y = Rme\n",
|
||||||
|
"T = size(Y,1)\n",
|
||||||
|
"X = [ones(T) RSMB RHML]\n",
|
||||||
|
"\n",
|
||||||
|
"(b,u,Yhat,V,R2) = OlsGMFn(Y,X)\n",
|
||||||
|
"std_iid = sqrt.(diag(V))\n",
|
||||||
|
"\n",
|
||||||
|
"printblue(\"OLS Results (assuming iid residuals):\\n\")\n",
|
||||||
|
"xNames = [\"c\",\"SMB\",\"HML\"]\n",
|
||||||
|
"printmat([b std_iid],colNames=[\"b\",\"std_iid\"],rowNames=xNames)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Getting Started with RCall"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"using RCall"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"RObject{VecSxp}\n",
|
||||||
|
"\n",
|
||||||
|
"Call:\n",
|
||||||
|
"lm(formula = Y ~ X + 0)\n",
|
||||||
|
"\n",
|
||||||
|
"Residuals:\n",
|
||||||
|
" Min 1Q Median 3Q Max \n",
|
||||||
|
"-0.20224 -0.02477 0.00335 0.02663 0.11840 \n",
|
||||||
|
"\n",
|
||||||
|
"Coefficients:\n",
|
||||||
|
" Estimate Std. Error t value Pr(>|t|) \n",
|
||||||
|
"X1 0.006983 0.002205 3.167 0.00166 ** \n",
|
||||||
|
"X2 0.216968 0.073565 2.949 0.00338 ** \n",
|
||||||
|
"X3 -0.429088 0.073710 -5.821 1.23e-08 ***\n",
|
||||||
|
"---\n",
|
||||||
|
"Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n",
|
||||||
|
"\n",
|
||||||
|
"Residual standard error: 0.04295 on 385 degrees of freedom\n",
|
||||||
|
"Multiple R-squared: 0.1488,\tAdjusted R-squared: 0.1422 \n",
|
||||||
|
"F-statistic: 22.44 on 3 and 385 DF, p-value: 2.07e-13\n",
|
||||||
|
"\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"RObject{VecSxp}\n",
|
||||||
|
"\n",
|
||||||
|
"Call:\n",
|
||||||
|
"lm(formula = Y ~ X + 0)\n",
|
||||||
|
"\n",
|
||||||
|
"Coefficients:\n",
|
||||||
|
" X1 X2 X3 \n",
|
||||||
|
" 0.006983 0.216968 -0.429088 \n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"@rput X Y\n",
|
||||||
|
"\n",
|
||||||
|
"ResultsR = reval(\"summary(mod <- lm(Y ~ X+0))\") #print summary of regression\n",
|
||||||
|
"println(ResultsR)\n",
|
||||||
|
"\n",
|
||||||
|
"resultsR = reval(\"mod <- lm(Y ~ X+0)\") #get all output"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[:coefficients, :residuals, :effects, :rank, Symbol(\"fitted.values\"), :assign, :qr, Symbol(\"df.residual\"), :xlevels, :call, :terms, :model]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"println(names(resultsR)) #print all keys (field names)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Task 1\n",
|
||||||
|
"\n",
|
||||||
|
"Print the Julia and Python estimates (of the coefficients) in a table so we can compare directly."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Task 2\n",
|
||||||
|
"\n",
|
||||||
|
"Print the smallest and largest values of the difference between the residuals according to Julia and those according to Python."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# OLS (in Julia) with Robust Standard Errors\n",
|
||||||
|
"\n",
|
||||||
|
"Use standard errors that are robust to heteroskedastcity and autocorrelation (2 lags)."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 9,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\u001b[34m\u001b[1mOLS Results (robust std):\u001b[22m\u001b[39m\n",
|
||||||
|
"\n",
|
||||||
|
" b std_nw\n",
|
||||||
|
"c 0.007 0.002\n",
|
||||||
|
"SMB 0.217 0.129\n",
|
||||||
|
"HML -0.429 0.118\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"(b,u,Yhat,V,R2) = OlsNWFn(Y,X,2)\n",
|
||||||
|
"std_nw = sqrt.(diag(V))\n",
|
||||||
|
"\n",
|
||||||
|
"printblue(\"OLS Results (robust std):\\n\")\n",
|
||||||
|
"xNames = [\"c\",\"SMB\",\"HML\"]\n",
|
||||||
|
"printmat([b std_nw],colNames=[\"b\",\"std_nw\"],rowNames=xNames)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Task 3 \n",
|
||||||
|
"\n",
|
||||||
|
"Now redo the R estimation with the same sort of robust standard errors. Hint: the `NeweyWest` in the `sandwich`package."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"@webio": {
|
||||||
|
"lastCommId": null,
|
||||||
|
"lastKernelId": null
|
||||||
|
},
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Julia 1.6.3",
|
||||||
|
"language": "julia",
|
||||||
|
"name": "julia-1.6"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"file_extension": ".jl",
|
||||||
|
"mimetype": "application/julia",
|
||||||
|
"name": "julia",
|
||||||
|
"version": "1.6.3"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 4
|
||||||
|
}
|
132
Problemsets/PS09c_WarAndPeace.ipynb
Normal file
132
Problemsets/PS09c_WarAndPeace.ipynb
Normal file
|
@ -0,0 +1,132 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"using Downloads"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Download a Book from Internet\n",
|
||||||
|
"\n",
|
||||||
|
"and read it into a string in Julia. Then report the number of letters etc (see below)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {
|
||||||
|
"tags": []
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"File to download: http://www.gutenberg.org/files/2600/2600-0.txt\n",
|
||||||
|
"\n",
|
||||||
|
"check the subfolder Results\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"if !isdir(\"Results\")\n",
|
||||||
|
" error(\"create the subfolder Results before running this program\")\n",
|
||||||
|
"end\n",
|
||||||
|
"\n",
|
||||||
|
"http = \"http://www.gutenberg.org/files/2600/2600-0.txt\"\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"File to download: \",http)\n",
|
||||||
|
"Downloads.download(http,\"Results/WarAndPeace.txt\")\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"\\ncheck the subfolder Results\\n\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {
|
||||||
|
"tags": []
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"fh = open(\"Results/WarAndPeace.txt\")\n",
|
||||||
|
"str = read(fh,String) \n",
|
||||||
|
"close(fh)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Task 1\n",
|
||||||
|
"\n",
|
||||||
|
"1. Count the number of letters and the unique letters in `str`. Hint: `length() and `unique()`\n",
|
||||||
|
"\n",
|
||||||
|
"2. Count the number of word and lines. Hint: `split(str)` and `split(str,\"\\n\")`\n",
|
||||||
|
"\n",
|
||||||
|
"3. Count the number of unique words."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Task 2\n",
|
||||||
|
"\n",
|
||||||
|
"1. How often is Borodinó mentioned. Hint: `occursin.(,words)`\n",
|
||||||
|
"\n",
|
||||||
|
"2. Print all lines that contain the word Borodinó. Hint: `occursin(,line)`"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Task 3\n",
|
||||||
|
"\n",
|
||||||
|
"1. Change Borodinó everywhere to Berëzina and then count the occurances"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"@webio": {
|
||||||
|
"lastCommId": null,
|
||||||
|
"lastKernelId": null
|
||||||
|
},
|
||||||
|
"anaconda-cloud": {},
|
||||||
|
"kernel_info": {
|
||||||
|
"name": "julia-1.2"
|
||||||
|
},
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Julia 1.6.3",
|
||||||
|
"language": "julia",
|
||||||
|
"name": "julia-1.6"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"file_extension": ".jl",
|
||||||
|
"mimetype": "application/julia",
|
||||||
|
"name": "julia",
|
||||||
|
"version": "1.6.3"
|
||||||
|
},
|
||||||
|
"nteract": {
|
||||||
|
"version": "0.24.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 4
|
||||||
|
}
|
210
Problemsets/PS10_DataFrames.ipynb
Normal file
210
Problemsets/PS10_DataFrames.ipynb
Normal file
|
@ -0,0 +1,210 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Load Packages"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {
|
||||||
|
"tags": []
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"printyellow (generic function with 1 method)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"using Printf, Dates, Statistics, CSV, DataFrames\n",
|
||||||
|
"include(\"jlFiles/printmat.jl\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Loading Some Data with CSV.jl"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"The first 4 lines of Data/Options_prices_US_Canada.csv:\n",
|
||||||
|
"\n",
|
||||||
|
"symbol,exchange,date,adjusted close,option symbol,expiration,strike,call/put,style,ask,bid,volume,open interest,unadjusted\n",
|
||||||
|
"SPX,CBOE,03/30/17,2368.06,SPXW 170331C00300000,03/31/17,300,C,E,2073.9,2062.9,0,0,2368.927\n",
|
||||||
|
"SPX,CBOE,03/30/17,2368.06,SPXW 170331P00300000,03/31/17,300,P,E,0.1,0,0,0,2368.927\n",
|
||||||
|
"SPX,CBOE,03/30/17,2368.06,SPXW 170331C00400000,03/31/17,400,C,E,1974.1,1962.7,0,0,2368.927\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"DataFile = \"Data/Options_prices_US_Canada.csv\"\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"The first 4 lines of $(DataFile):\\n\")\n",
|
||||||
|
"txt = readlines(DataFile)\n",
|
||||||
|
"printmat(txt[1:4])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Use `normalizenames` to get names that can be used in Julia as variables names and specify the `dateformat` used in the csv file (to convert to proper Julia dates). The dates in the file are given as `03/30/17` which CSV/DataFrames interpret as 30 March year 17 (AD). We add `Dates.Year(2000)` to get year 2017."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {
|
||||||
|
"tags": []
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\u001b[1m13952×10 DataFrame\u001b[0m\n",
|
||||||
|
"\u001b[1m Row \u001b[0m│\u001b[1m symbol \u001b[0m\u001b[1m date \u001b[0m\u001b[1m close \u001b[0m\u001b[1m expiration \u001b[0m\u001b[1m strike \u001b[0m\u001b[1m call_put \u001b[0m\u001b[1m ask \u001b[0m\u001b[1m\u001b[0m ⋯\n",
|
||||||
|
"\u001b[1m \u001b[0m│\u001b[90m String3 \u001b[0m\u001b[90m Date \u001b[0m\u001b[90m Float64 \u001b[0m\u001b[90m Date \u001b[0m\u001b[90m Float64 \u001b[0m\u001b[90m String1 \u001b[0m\u001b[90m Float64 \u001b[0m\u001b[90m\u001b[0m ⋯\n",
|
||||||
|
"───────┼────────────────────────────────────────────────────────────────────────\n",
|
||||||
|
" 1 │ SPX 2017-03-30 2368.06 2017-03-31 300.0 C 2073.9 ⋯\n",
|
||||||
|
" 2 │ SPX 2017-03-30 2368.06 2017-03-31 300.0 P 0.1\n",
|
||||||
|
" 3 │ SPX 2017-03-30 2368.06 2017-03-31 400.0 C 1974.1\n",
|
||||||
|
" 4 │ SPX 2017-03-30 2368.06 2017-03-31 400.0 P 0.05\n",
|
||||||
|
" 5 │ SPX 2017-03-30 2368.06 2017-03-31 500.0 C 1874.1 ⋯\n",
|
||||||
|
" 6 │ SPX 2017-03-30 2368.06 2017-03-31 500.0 P 0.05\n",
|
||||||
|
" 7 │ SPX 2017-03-30 2368.06 2017-03-31 600.0 C 1774.1\n",
|
||||||
|
" 8 │ SPX 2017-03-30 2368.06 2017-03-31 600.0 P 0.05\n",
|
||||||
|
" 9 │ SPX 2017-03-30 2368.06 2017-03-31 700.0 C 1673.9 ⋯\n",
|
||||||
|
" 10 │ SPX 2017-03-30 2368.06 2017-03-31 700.0 P 0.05\n",
|
||||||
|
" 11 │ SPX 2017-03-30 2368.06 2017-03-31 750.0 C 1624.1\n",
|
||||||
|
" ⋮ │ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋱\n",
|
||||||
|
" 13943 │ XIU 2017-03-30 23.06 2019-03-15 21.0 C 3.21\n",
|
||||||
|
" 13944 │ XIU 2017-03-30 23.06 2019-03-15 21.0 P 1.71 ⋯\n",
|
||||||
|
" 13945 │ XIU 2017-03-30 23.06 2019-03-15 22.0 C 2.53\n",
|
||||||
|
" 13946 │ XIU 2017-03-30 23.06 2019-03-15 22.0 P 2.03\n",
|
||||||
|
" 13947 │ XIU 2017-03-30 23.06 2019-03-15 23.0 C 1.97\n",
|
||||||
|
" 13948 │ XIU 2017-03-30 23.06 2019-03-15 23.0 P 2.51 ⋯\n",
|
||||||
|
" 13949 │ XIU 2017-03-30 23.06 2019-03-15 24.0 C 1.5\n",
|
||||||
|
" 13950 │ XIU 2017-03-30 23.06 2019-03-15 24.0 P 3.12\n",
|
||||||
|
" 13951 │ XIU 2017-03-30 23.06 2019-03-15 25.0 C 1.13\n",
|
||||||
|
" 13952 │ XIU 2017-03-30 23.06 2019-03-15 25.0 P 3.64 ⋯\n",
|
||||||
|
"\u001b[36m 3 columns and 13931 rows omitted\u001b[0m"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"df1 = CSV.read(DataFile,DataFrame,normalizenames=true,dateformat=\"mm/dd/yy\")\n",
|
||||||
|
"\n",
|
||||||
|
"df1.date .+= Dates.Year(2000) #03/30/17 to 03/30/2017\n",
|
||||||
|
"df1.expiration .+= Dates.Year(2000)\n",
|
||||||
|
"\n",
|
||||||
|
"select!(df1,Not([:exchange,:option_symbol,:style,:unadjusted])) #deleting some columns\n",
|
||||||
|
"rename!(df1,:adjusted_close => :close) #renaming a column\n",
|
||||||
|
"\n",
|
||||||
|
"show(df1)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Task 1\n",
|
||||||
|
"\n",
|
||||||
|
"Create a new DataFrame that contains only the data for SPX and those option contracts that were traded (volume > 0). Hint: `df1[vv, :]` picks out the rows of the data frame for which `vv` is `true`. "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Task 2\n",
|
||||||
|
"\n",
|
||||||
|
"Create a *group* for each expiration date. These groups can be referred to as `dataG2[key]`.\n",
|
||||||
|
"\n",
|
||||||
|
"Hints: `groupby()`"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Task 3\n",
|
||||||
|
"\n",
|
||||||
|
"Print the number of contracts (`nrow`) and the sum of the open interest `:open_interest=>sum` for each of the expiration dates.\n",
|
||||||
|
"\n",
|
||||||
|
"Hint: `combine()`"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Task 4 \n",
|
||||||
|
"Creating two new DataFrames: for expiration date 2017-04-21 and another for 2017-06-16.\n",
|
||||||
|
"\n",
|
||||||
|
"Hint: `dataG2[(expiration = Date(\"2017-04-21\"),)]`"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Task 5\n",
|
||||||
|
"\n",
|
||||||
|
"For the expiration date 2017-04-21, calculate the mid price as the average of the `.ask` and `.bid`. \n",
|
||||||
|
"\n",
|
||||||
|
"Plot the mid price as a function of the strike price `.strike` for put options. Add a curve another curve for the call options."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"@webio": {
|
||||||
|
"lastCommId": null,
|
||||||
|
"lastKernelId": null
|
||||||
|
},
|
||||||
|
"anaconda-cloud": {},
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Julia 1.7.0",
|
||||||
|
"language": "julia",
|
||||||
|
"name": "julia-1.7"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"file_extension": ".jl",
|
||||||
|
"mimetype": "application/julia",
|
||||||
|
"name": "julia",
|
||||||
|
"version": "1.7.0"
|
||||||
|
},
|
||||||
|
"nteract": {
|
||||||
|
"version": "0.23.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 4
|
||||||
|
}
|
229
Problemsets/PS10b_OptionDeltas.ipynb
Normal file
229
Problemsets/PS10b_OptionDeltas.ipynb
Normal file
|
@ -0,0 +1,229 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Load Packages and Extra Functions"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"printyellow (generic function with 1 method)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"using Printf, Distributions, OffsetArrays, FiniteDiff\n",
|
||||||
|
"\n",
|
||||||
|
"include(\"jlFiles/OptionsCalculations.jl\")\n",
|
||||||
|
"include(\"jlFiles/printmat.jl\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"using Plots\n",
|
||||||
|
"#pyplot(size=(600,400))\n",
|
||||||
|
"gr(size=(480,320))\n",
|
||||||
|
"default(fmt = :svg)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# The Black-Scholes Model\n",
|
||||||
|
"\n",
|
||||||
|
"The next cell calculates call `C` and put prices `P` from the Black-Scholes formula.\n",
|
||||||
|
"\n",
|
||||||
|
"The key parameters are:\n",
|
||||||
|
"`(S,K,m,y,σ) = (current undelying price, strike price,time to expiration,interest rate, volatility)`"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"call price at K=42: 2.893 \n",
|
||||||
|
"\n",
|
||||||
|
"put price at K=42: 1.856 \n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"(S,K,m,y,σ) = (42,42,0.5,0.05,0.2)\n",
|
||||||
|
"\n",
|
||||||
|
"C = OptionBlackSPs(S,K,m,y,σ)\n",
|
||||||
|
"printlnPs(\"call price at K=$K: \",C,\"\\n\")\n",
|
||||||
|
"\n",
|
||||||
|
"P = OptionBlackSPs(S,K,m,y,σ,0,true)\n",
|
||||||
|
"printlnPs(\"put price at K=$K: \",P,\"\\n\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Task 1\n",
|
||||||
|
"\n",
|
||||||
|
"For a range of different prices of the underlying asset `S=30.0:60.0`, calculat the call and put prices and plot them (with `S` on the hoizontal axis)."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# The Binomial Option Pricing Model\n",
|
||||||
|
"\n",
|
||||||
|
"The next cell contains functions to implement a binomial option pricing model for European style (exercise at expiration only) and American style (exercise any day) options. We use the CRR (Cox-Ross-Rubinstein) parameterisation.\n",
|
||||||
|
"\n",
|
||||||
|
"The key parameters are the same as before, but also `n` which is the number of time steps used in the calculations (which defaults to 250)."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"BOPM_American (generic function with 3 methods)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"\"\"\"\n",
|
||||||
|
"CRRparams(σ,m,n,y)\n",
|
||||||
|
"\n",
|
||||||
|
" BOPM parameters according to CRR\n",
|
||||||
|
"\"\"\"\n",
|
||||||
|
"function CRRparams(σ,m,y,n)\n",
|
||||||
|
" h = m/n #time step size (in years)\n",
|
||||||
|
" u = exp(σ*sqrt(h)) #up move\n",
|
||||||
|
" d = exp(-σ*sqrt(h)) #down move\n",
|
||||||
|
" p = (exp(y*h) - d)/(u-d) #rn prob of up move\n",
|
||||||
|
" return h,u,d,p\n",
|
||||||
|
"end \n",
|
||||||
|
"\n",
|
||||||
|
"function BOPM_European(S,K,m,y,σ,isPut=false,n=250)\n",
|
||||||
|
" (h,u,d,p) = CRRparams(σ,m,y,n)\n",
|
||||||
|
" STree = BuildSTree(S,n,u,d)\n",
|
||||||
|
" price = EuOptionPrice(STree,K,y,h,p,isPut)[0][]\n",
|
||||||
|
" return price\n",
|
||||||
|
"end\n",
|
||||||
|
"\n",
|
||||||
|
"function BOPM_American(S,K,m,y,σ,isPut=false,n=250)\n",
|
||||||
|
" (h,u,d,p) = CRRparams(σ,m,y,n)\n",
|
||||||
|
" STree = BuildSTree(S,n,u,d)\n",
|
||||||
|
" price = AmOptionPrice(STree,K,y,h,p,isPut)[1][0][1]\n",
|
||||||
|
" return price\n",
|
||||||
|
"end"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"call (Eu) 2.891\n",
|
||||||
|
"put (Eu) 1.854\n",
|
||||||
|
"call (Am) 2.891\n",
|
||||||
|
"put (Am) 1.954\n",
|
||||||
|
"\n",
|
||||||
|
"\u001b[31m\u001b[1mNotice that c_e=c_a but that p_a>=p_e\u001b[22m\u001b[39m\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"c_e = BOPM_European(S,K,m,y,σ,false) #call price, European style\n",
|
||||||
|
"p_e = BOPM_European(S,K,m,y,σ,true) #put price, European\n",
|
||||||
|
"\n",
|
||||||
|
"c_a = BOPM_American(S,K,m,y,σ,false) #call, American\n",
|
||||||
|
"p_a = BOPM_American(S,K,m,y,σ,true) #put, American\n",
|
||||||
|
"\n",
|
||||||
|
"printmat([c_e,p_e,c_a,p_a],rowNames=[\"call (Eu)\",\"put (Eu)\",\"call (Am)\",\"put (Am)\"])\n",
|
||||||
|
"\n",
|
||||||
|
"printred(\"Notice that c_e=c_a but that p_a>=p_e\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Task 2\n",
|
||||||
|
"\n",
|
||||||
|
"Redo the calculation of p_e and p_a for `S=30:60` and plot the prices."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Delta\n",
|
||||||
|
"\n",
|
||||||
|
"To hedge an option contract (eg. if we are short one option contract), we buy $\\Delta$ units of the underlying asset, where $\\Delta$ is the partial derivative of the option price with respect to the price of the underlying asset.\n",
|
||||||
|
"\n",
|
||||||
|
"Calculate $\\Delta$ for both the European and American puts, using the BOPM function. Do the calculation for each value in `S_range` and plot the results.\n",
|
||||||
|
"\n",
|
||||||
|
"To calculate the derivatives we could use, for instance, \n",
|
||||||
|
"`FiniteDiff.finite_difference_derivative(the function,an S value)`"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"@webio": {
|
||||||
|
"lastCommId": null,
|
||||||
|
"lastKernelId": null
|
||||||
|
},
|
||||||
|
"anaconda-cloud": {},
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Julia 1.6.3",
|
||||||
|
"language": "julia",
|
||||||
|
"name": "julia-1.6"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"file_extension": ".jl",
|
||||||
|
"mimetype": "application/julia",
|
||||||
|
"name": "julia",
|
||||||
|
"version": "1.6.3"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 4
|
||||||
|
}
|
1688
Problemsets/Solutions/PS01_Basics_Solution.ipynb
Normal file
1688
Problemsets/Solutions/PS01_Basics_Solution.ipynb
Normal file
File diff suppressed because one or more lines are too long
1742
Problemsets/Solutions/PS02_Stats_Solution.ipynb
Normal file
1742
Problemsets/Solutions/PS02_Stats_Solution.ipynb
Normal file
File diff suppressed because one or more lines are too long
5438
Problemsets/Solutions/PS03_CLT_Solution.ipynb
Normal file
5438
Problemsets/Solutions/PS03_CLT_Solution.ipynb
Normal file
File diff suppressed because one or more lines are too long
522
Problemsets/Solutions/PS04_YieldToMaturity_Solution.ipynb
Normal file
522
Problemsets/Solutions/PS04_YieldToMaturity_Solution.ipynb
Normal file
File diff suppressed because one or more lines are too long
1839
Problemsets/Solutions/PS05_TradingStrategy_Solution.ipynb
Normal file
1839
Problemsets/Solutions/PS05_TradingStrategy_Solution.ipynb
Normal file
File diff suppressed because one or more lines are too long
1193
Problemsets/Solutions/PS07_Optimisation_Solution.ipynb
Normal file
1193
Problemsets/Solutions/PS07_Optimisation_Solution.ipynb
Normal file
File diff suppressed because one or more lines are too long
6294
Problemsets/Solutions/PS08_Volatility_Solution.ipynb
Normal file
6294
Problemsets/Solutions/PS08_Volatility_Solution.ipynb
Normal file
File diff suppressed because one or more lines are too long
369
Problemsets/Solutions/PS09a_PyCall_Solution.ipynb
Normal file
369
Problemsets/Solutions/PS09a_PyCall_Solution.ipynb
Normal file
|
@ -0,0 +1,369 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Python\n",
|
||||||
|
"\n",
|
||||||
|
"using the [PyCall.jl](https://github.com/JuliaPy/PyCall.jl) package."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"CovNWFn"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"using Printf, DelimitedFiles, LinearAlgebra, Statistics\n",
|
||||||
|
"\n",
|
||||||
|
"include(\"jlFiles/printmat.jl\")\n",
|
||||||
|
"include(\"jlFiles/OlsNW.jl\") #functions for OLS"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Sample size: (388,)\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"x = readdlm(\"Data/FFmFactorsPs.csv\",',',skipstart=1)\n",
|
||||||
|
"\n",
|
||||||
|
" #yearmonth, market, small minus big, high minus low\n",
|
||||||
|
"(ym,Rme,RSMB,RHML) = (x[:,1],x[:,2]/100,x[:,3]/100,x[:,4]/100) \n",
|
||||||
|
"x = nothing\n",
|
||||||
|
"\n",
|
||||||
|
"printlnPs(\"Sample size:\",size(Rme))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Do OLS (in Julia)\n",
|
||||||
|
"\n",
|
||||||
|
"use the function sin the file OlsNW.jl to do OLS. Report point estimates and standard errors."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\u001b[34m\u001b[1mOLS Results (assuming iid residuals):\u001b[22m\u001b[39m\n",
|
||||||
|
"\n",
|
||||||
|
" b std_iid\n",
|
||||||
|
"c 0.007 0.002\n",
|
||||||
|
"SMB 0.217 0.073\n",
|
||||||
|
"HML -0.429 0.074\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"Y = Rme\n",
|
||||||
|
"T = size(Y,1)\n",
|
||||||
|
"X = [ones(T) RSMB RHML]\n",
|
||||||
|
"\n",
|
||||||
|
"(b,u,Yhat,V,R2) = OlsGMFn(Y,X)\n",
|
||||||
|
"std_iid = sqrt.(diag(V))\n",
|
||||||
|
"\n",
|
||||||
|
"printblue(\"OLS Results (assuming iid residuals):\\n\")\n",
|
||||||
|
"xNames = [\"c\",\"SMB\",\"HML\"]\n",
|
||||||
|
"printmat([b std_iid],colNames=[\"b\",\"std_iid\"],rowNames=xNames)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Getting Started with PyCall"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"using PyCall\n",
|
||||||
|
"sm = pyimport(\"statsmodels.api\"); #activate this package"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"PyObject <class 'statsmodels.iolib.summary.Summary'>\n",
|
||||||
|
"\"\"\"\n",
|
||||||
|
" OLS Regression Results \n",
|
||||||
|
"==============================================================================\n",
|
||||||
|
"Dep. Variable: y R-squared: 0.134\n",
|
||||||
|
"Model: OLS Adj. R-squared: 0.130\n",
|
||||||
|
"Method: Least Squares F-statistic: 29.85\n",
|
||||||
|
"Date: Thu, 09 Dec 2021 Prob (F-statistic): 8.88e-13\n",
|
||||||
|
"Time: 10:00:28 Log-Likelihood: 672.28\n",
|
||||||
|
"No. Observations: 388 AIC: -1339.\n",
|
||||||
|
"Df Residuals: 385 BIC: -1327.\n",
|
||||||
|
"Df Model: 2 \n",
|
||||||
|
"Covariance Type: nonrobust \n",
|
||||||
|
"==============================================================================\n",
|
||||||
|
" coef std err t P>|t| [0.025 0.975]\n",
|
||||||
|
"------------------------------------------------------------------------------\n",
|
||||||
|
"const 0.0070 0.002 3.167 0.002 0.003 0.011\n",
|
||||||
|
"x1 0.2170 0.074 2.949 0.003 0.072 0.362\n",
|
||||||
|
"x2 -0.4291 0.074 -5.821 0.000 -0.574 -0.284\n",
|
||||||
|
"==============================================================================\n",
|
||||||
|
"Omnibus: 58.863 Durbin-Watson: 1.849\n",
|
||||||
|
"Prob(Omnibus): 0.000 Jarque-Bera (JB): 146.539\n",
|
||||||
|
"Skew: -0.749 Prob(JB): 1.51e-32\n",
|
||||||
|
"Kurtosis: 5.612 Cond. No. 38.8\n",
|
||||||
|
"==============================================================================\n",
|
||||||
|
"\n",
|
||||||
|
"Notes:\n",
|
||||||
|
"[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n",
|
||||||
|
"\"\"\"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stderr",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"┌ Warning: `vendor()` is deprecated, use `BLAS.get_config()` and inspect the output instead\n",
|
||||||
|
"│ caller = npyinitialize() at numpy.jl:67\n",
|
||||||
|
"└ @ PyCall C:\\Users\\psoderlind\\.julia\\packages\\PyCall\\3fwVL\\src\\numpy.jl:67\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"resultsP = sm.OLS(Y, X).fit() #can use Python functions directly\n",
|
||||||
|
"\n",
|
||||||
|
"println(resultsP.summary())"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[:HC0_se, :HC1_se, :HC2_se, :HC3_se, :_HCCM, :__class__, :__delattr__, :__dict__, :__dir__, :__doc__, :__eq__, :__format__, :__ge__, :__getattribute__, :__gt__, :__hash__, :__init__, :__init_subclass__, :__le__, :__lt__, :__module__, :__ne__, :__new__, :__reduce__, :__reduce_ex__, :__repr__, :__setattr__, :__sizeof__, :__str__, :__subclasshook__, :__weakref__, :_abat_diagonal, :_cache, :_data_attr, :_data_in_cache, :_get_robustcov_results, :_is_nested, :_use_t, :_wexog_singular_values, :aic, :bic, :bse, :centered_tss, :compare_f_test, :compare_lm_test, :compare_lr_test, :condition_number, :conf_int, :conf_int_el, :cov_HC0, :cov_HC1, :cov_HC2, :cov_HC3, :cov_kwds, :cov_params, :cov_type, :df_model, :df_resid, :diagn, :eigenvals, :el_test, :ess, :f_pvalue, :f_test, :fittedvalues, :fvalue, :get_influence, :get_prediction, :get_robustcov_results, :info_criteria, :initialize, :k_constant, :llf, :load, :model, :mse_model, :mse_resid, :mse_total, :nobs, :normalized_cov_params, :outlier_test, :params, :predict, :pvalues, :remove_data, :resid, :resid_pearson, :rsquared, :rsquared_adj, :save, :scale, :ssr, :summary, :summary2, :t_test, :t_test_pairwise, :tvalues, :uncentered_tss, :use_t, :wald_test, :wald_test_terms, :wresid]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"println(keys(resultsP)) #print all keys (field names)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Task 1\n",
|
||||||
|
"\n",
|
||||||
|
"Print the Julia and Python estimates (of the coefficients) in a table so we can compare directly."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Comparing the estimates in Julia and Python\n",
|
||||||
|
" 0.007 0.007\n",
|
||||||
|
" 0.217 0.217\n",
|
||||||
|
" -0.429 -0.429\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"b_P = resultsP.params #the numerical results are now a Julia vector\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"Comparing the estimates in Julia and Python\")\n",
|
||||||
|
"printmat([b b_P])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Task 2\n",
|
||||||
|
"\n",
|
||||||
|
"Print the smallest and largest values of the difference between the residuals according to Julia and those according to Python."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"(-2.7755575615628914e-17, 4.163336342344337e-17)\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"printmat(extrema(resultsP.resid - u))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# OLS (in Julia) with Robust Standard Errors\n",
|
||||||
|
"\n",
|
||||||
|
"Use standard errors that are robust to heteroskedastcity and autocorrelation (2 lags)."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 9,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\u001b[34m\u001b[1mOLS Results (robust std):\u001b[22m\u001b[39m\n",
|
||||||
|
"\n",
|
||||||
|
" b std_nw\n",
|
||||||
|
"c 0.007 0.002\n",
|
||||||
|
"SMB 0.217 0.129\n",
|
||||||
|
"HML -0.429 0.118\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"(b,u,Yhat,V,R2) = OlsNWFn(Y,X,2)\n",
|
||||||
|
"std_nw = sqrt.(diag(V))\n",
|
||||||
|
"\n",
|
||||||
|
"printblue(\"OLS Results (robust std):\\n\")\n",
|
||||||
|
"xNames = [\"c\",\"SMB\",\"HML\"]\n",
|
||||||
|
"printmat([b std_nw],colNames=[\"b\",\"std_nw\"],rowNames=xNames)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Task 3 \n",
|
||||||
|
"\n",
|
||||||
|
"Now redo the Python estimation with the same sort of robust standard errors. Hint: `resultsP.get_robustcov_results()`"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 10,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"PyObject <class 'statsmodels.iolib.summary.Summary'>\n",
|
||||||
|
"\"\"\"\n",
|
||||||
|
" OLS Regression Results \n",
|
||||||
|
"==============================================================================\n",
|
||||||
|
"Dep. Variable: y R-squared: 0.134\n",
|
||||||
|
"Model: OLS Adj. R-squared: 0.130\n",
|
||||||
|
"Method: Least Squares F-statistic: 11.87\n",
|
||||||
|
"Date: Thu, 09 Dec 2021 Prob (F-statistic): 9.94e-06\n",
|
||||||
|
"Time: 10:00:32 Log-Likelihood: 672.28\n",
|
||||||
|
"No. Observations: 388 AIC: -1339.\n",
|
||||||
|
"Df Residuals: 385 BIC: -1327.\n",
|
||||||
|
"Df Model: 2 \n",
|
||||||
|
"Covariance Type: HAC \n",
|
||||||
|
"==============================================================================\n",
|
||||||
|
" coef std err t P>|t| [0.025 0.975]\n",
|
||||||
|
"------------------------------------------------------------------------------\n",
|
||||||
|
"const 0.0070 0.002 2.850 0.005 0.002 0.012\n",
|
||||||
|
"x1 0.2170 0.129 1.688 0.092 -0.036 0.470\n",
|
||||||
|
"x2 -0.4291 0.118 -3.649 0.000 -0.660 -0.198\n",
|
||||||
|
"==============================================================================\n",
|
||||||
|
"Omnibus: 58.863 Durbin-Watson: 1.849\n",
|
||||||
|
"Prob(Omnibus): 0.000 Jarque-Bera (JB): 146.539\n",
|
||||||
|
"Skew: -0.749 Prob(JB): 1.51e-32\n",
|
||||||
|
"Kurtosis: 5.612 Cond. No. 38.8\n",
|
||||||
|
"==============================================================================\n",
|
||||||
|
"\n",
|
||||||
|
"Notes:\n",
|
||||||
|
"[1] Standard Errors are heteroscedasticity and autocorrelation robust (HAC) using 2 lags and without small sample correction\n",
|
||||||
|
"\"\"\"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"resultsP2 = resultsP.get_robustcov_results(cov_type=\"HAC\",maxlags=2)\n",
|
||||||
|
"\n",
|
||||||
|
"println(resultsP2.summary())"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"@webio": {
|
||||||
|
"lastCommId": null,
|
||||||
|
"lastKernelId": null
|
||||||
|
},
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Julia 1.7.0",
|
||||||
|
"language": "julia",
|
||||||
|
"name": "julia-1.7"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"file_extension": ".jl",
|
||||||
|
"mimetype": "application/julia",
|
||||||
|
"name": "julia",
|
||||||
|
"version": "1.7.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 4
|
||||||
|
}
|
363
Problemsets/Solutions/PS09b_RCall_Solution.ipynb
Normal file
363
Problemsets/Solutions/PS09b_RCall_Solution.ipynb
Normal file
|
@ -0,0 +1,363 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# R\n",
|
||||||
|
"\n",
|
||||||
|
"using the [RCall.jl](https://juliainterop.github.io/RCall.jl/stable/) package."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"CovNWFn"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"using Printf, DelimitedFiles, LinearAlgebra, Statistics\n",
|
||||||
|
"\n",
|
||||||
|
"include(\"jlFiles/printmat.jl\")\n",
|
||||||
|
"include(\"jlFiles/OlsNW.jl\") #functions for OLS"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Sample size: (388,)\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"x = readdlm(\"Data/FFmFactorsPs.csv\",',',skipstart=1)\n",
|
||||||
|
"\n",
|
||||||
|
" #yearmonth, market, small minus big, high minus low\n",
|
||||||
|
"(ym,Rme,RSMB,RHML) = (x[:,1],x[:,2]/100,x[:,3]/100,x[:,4]/100) \n",
|
||||||
|
"x = nothing\n",
|
||||||
|
"\n",
|
||||||
|
"printlnPs(\"Sample size:\",size(Rme))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Do OLS (in Julia)\n",
|
||||||
|
"\n",
|
||||||
|
"use the function sin the file OlsNW.jl to do OLS. Report point estimates and standard errors."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\u001b[34m\u001b[1mOLS Results (assuming iid residuals):\u001b[22m\u001b[39m\n",
|
||||||
|
"\n",
|
||||||
|
" b std_iid\n",
|
||||||
|
"c 0.007 0.002\n",
|
||||||
|
"SMB 0.217 0.073\n",
|
||||||
|
"HML -0.429 0.074\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"Y = Rme\n",
|
||||||
|
"T = size(Y,1)\n",
|
||||||
|
"X = [ones(T) RSMB RHML]\n",
|
||||||
|
"\n",
|
||||||
|
"(b,u,Yhat,V,R2) = OlsGMFn(Y,X)\n",
|
||||||
|
"std_iid = sqrt.(diag(V))\n",
|
||||||
|
"\n",
|
||||||
|
"printblue(\"OLS Results (assuming iid residuals):\\n\")\n",
|
||||||
|
"xNames = [\"c\",\"SMB\",\"HML\"]\n",
|
||||||
|
"printmat([b std_iid],colNames=[\"b\",\"std_iid\"],rowNames=xNames)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Getting Started with RCall"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"using RCall"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"RObject{VecSxp}\n",
|
||||||
|
"\n",
|
||||||
|
"Call:\n",
|
||||||
|
"lm(formula = Y ~ X + 0)\n",
|
||||||
|
"\n",
|
||||||
|
"Residuals:\n",
|
||||||
|
" Min 1Q Median 3Q Max \n",
|
||||||
|
"-0.20224 -0.02477 0.00335 0.02663 0.11840 \n",
|
||||||
|
"\n",
|
||||||
|
"Coefficients:\n",
|
||||||
|
" Estimate Std. Error t value Pr(>|t|) \n",
|
||||||
|
"X1 0.006983 0.002205 3.167 0.00166 ** \n",
|
||||||
|
"X2 0.216968 0.073565 2.949 0.00338 ** \n",
|
||||||
|
"X3 -0.429088 0.073710 -5.821 1.23e-08 ***\n",
|
||||||
|
"---\n",
|
||||||
|
"Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n",
|
||||||
|
"\n",
|
||||||
|
"Residual standard error: 0.04295 on 385 degrees of freedom\n",
|
||||||
|
"Multiple R-squared: 0.1488,\tAdjusted R-squared: 0.1422 \n",
|
||||||
|
"F-statistic: 22.44 on 3 and 385 DF, p-value: 2.07e-13\n",
|
||||||
|
"\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"RObject{VecSxp}\n",
|
||||||
|
"\n",
|
||||||
|
"Call:\n",
|
||||||
|
"lm(formula = Y ~ X + 0)\n",
|
||||||
|
"\n",
|
||||||
|
"Coefficients:\n",
|
||||||
|
" X1 X2 X3 \n",
|
||||||
|
" 0.006983 0.216968 -0.429088 \n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"@rput X Y\n",
|
||||||
|
"\n",
|
||||||
|
"ResultsR = reval(\"summary(mod <- lm(Y ~ X+0))\") #print summary of regression\n",
|
||||||
|
"println(ResultsR)\n",
|
||||||
|
"\n",
|
||||||
|
"resultsR = reval(\"mod <- lm(Y ~ X+0)\") #get all output"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"[:coefficients, :residuals, :effects, :rank, Symbol(\"fitted.values\"), :assign, :qr, Symbol(\"df.residual\"), :xlevels, :call, :terms, :model]\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"println(names(resultsR)) #print all keys (field names)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Task 1\n",
|
||||||
|
"\n",
|
||||||
|
"Print the Julia and Python estimates (of the coefficients) in a table so we can compare directly."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Comparing the estimates in Julia and R\n",
|
||||||
|
" 0.007 0.007\n",
|
||||||
|
" 0.217 0.217\n",
|
||||||
|
" -0.429 -0.429\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"b_R = rcopy(resultsR[:coefficients]) #the numerical results are now a Julia vector\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"Comparing the estimates in Julia and R\")\n",
|
||||||
|
"printmat([b b_R])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Task 2\n",
|
||||||
|
"\n",
|
||||||
|
"Print the smallest and largest values of the difference between the residuals according to Julia and those according to Python."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"(-1.942890293094024e-16, 6.453171330633722e-16)\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"resid_R = rcopy(resultsR[:residuals]) \n",
|
||||||
|
"\n",
|
||||||
|
"printmat(extrema(resid_R - u))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# OLS (in Julia) with Robust Standard Errors\n",
|
||||||
|
"\n",
|
||||||
|
"Use standard errors that are robust to heteroskedastcity and autocorrelation (2 lags)."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 9,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\u001b[34m\u001b[1mOLS Results (robust std):\u001b[22m\u001b[39m\n",
|
||||||
|
"\n",
|
||||||
|
" b std_nw\n",
|
||||||
|
"c 0.007 0.002\n",
|
||||||
|
"SMB 0.217 0.129\n",
|
||||||
|
"HML -0.429 0.118\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"(b,u,Yhat,V,R2) = OlsNWFn(Y,X,2)\n",
|
||||||
|
"std_nw = sqrt.(diag(V))\n",
|
||||||
|
"\n",
|
||||||
|
"printblue(\"OLS Results (robust std):\\n\")\n",
|
||||||
|
"xNames = [\"c\",\"SMB\",\"HML\"]\n",
|
||||||
|
"printmat([b std_nw],colNames=[\"b\",\"std_nw\"],rowNames=xNames)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Task 3 \n",
|
||||||
|
"\n",
|
||||||
|
"Now redo the R estimation with the same sort of robust standard errors. Hint: the `NeweyWest` in the `sandwich`package."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 10,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"@rlibrary sandwich"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 11,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
" bstd_nw (R)\n",
|
||||||
|
"c 0.007 0.002\n",
|
||||||
|
"SMB 0.217 0.129\n",
|
||||||
|
"HML -0.429 0.118\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"reval(\"mod <- lm(Y ~ X+0)\")\n",
|
||||||
|
"\n",
|
||||||
|
"VCV_nwR = reval(NeweyWest(resultsR,lag=2,prewhite=0))\n",
|
||||||
|
"std_nwR = sqrt.(diag(rcopy(VCV_nwR)))\n",
|
||||||
|
"\n",
|
||||||
|
"printmat([b std_nwR],colNames=[\"b\",\"std_nw (R)\"],rowNames=xNames)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"@webio": {
|
||||||
|
"lastCommId": null,
|
||||||
|
"lastKernelId": null
|
||||||
|
},
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Julia 1.7.0",
|
||||||
|
"language": "julia",
|
||||||
|
"name": "julia-1.7"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"file_extension": ".jl",
|
||||||
|
"mimetype": "application/julia",
|
||||||
|
"name": "julia",
|
||||||
|
"version": "1.7.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 4
|
||||||
|
}
|
389
Problemsets/Solutions/PS09c_WarAndPeace_Solution.ipynb
Normal file
389
Problemsets/Solutions/PS09c_WarAndPeace_Solution.ipynb
Normal file
|
@ -0,0 +1,389 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"using Downloads"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Download a Book from Internet\n",
|
||||||
|
"\n",
|
||||||
|
"and read it into a string in Julia. Then report the number of letters etc (see below)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {
|
||||||
|
"tags": []
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"File to download: http://www.gutenberg.org/files/2600/2600-0.txt\n",
|
||||||
|
"\n",
|
||||||
|
"check the subfolder Results\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"if !isdir(\"Results\")\n",
|
||||||
|
" error(\"create the subfolder Results before running this program\")\n",
|
||||||
|
"end\n",
|
||||||
|
"\n",
|
||||||
|
"http = \"http://www.gutenberg.org/files/2600/2600-0.txt\"\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"File to download: \",http)\n",
|
||||||
|
"Downloads.download(http,\"Results/WarAndPeace.txt\")\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"\\ncheck the subfolder Results\\n\")"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {
|
||||||
|
"tags": []
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"fh = open(\"Results/WarAndPeace.txt\")\n",
|
||||||
|
"str = read(fh,String) \n",
|
||||||
|
"close(fh)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Task 1\n",
|
||||||
|
"\n",
|
||||||
|
"1. Count the number of letters and the unique letters in `str`. Hint: `length() and `unique()`\n",
|
||||||
|
"\n",
|
||||||
|
"2. Count the number of word and lines. Hint: `split(str)` and `split(str,\"\\n\")`\n",
|
||||||
|
"\n",
|
||||||
|
"3. Count the number of unique words."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"number of letters and unique letters in W&P: 3293555 113\n",
|
||||||
|
"\n",
|
||||||
|
"unique letters: ['\\ufeff', 'T', 'h', 'e', ' ', 'P', 'r', 'o', 'j', 'c', 't', 'G', 'u', 'n', 'b', 'g', 'B', 'k', 'f', 'W', 'a', 'd', ',', 'y', 'L', 'l', 's', '\\r', '\\n', 'i', 'w', 'U', 'S', 'm', 'p', 'v', '.', 'Y', '-', 'I', ':', 'A', 'M', 'R', 'D', '2', '0', '1', '[', '#', '6', ']', 'J', '9', 'E', 'C', 'F', '8', 'V', '*', 'O', 'H', 'N', 'K', '/', '5', 'X', '7', '3', '“', '’', '—', '‘', '!', '?', '”', 'á', 'é', 'ë', 'í', ';', 'x', '(', ')', 'z', 'q', 'À', 'ó', 'ú', 'è', 'î', 'ô', 'à', 'ç', 'Q', 'â', 'ê', 'ï', 'Z', '4', 'ý', 'ö', 'ä', 'ü', 'Á', 'œ', 'É', '=', 'æ', '\"', '%', '\\'', '$']\n",
|
||||||
|
"\n",
|
||||||
|
"ASCII letters: ['T', 'h', 'e', ' ', 'P', 'r', 'o', 'j', 'c', 't', 'G', 'u', 'n', 'b', 'g', 'B', 'k', 'f', 'W', 'a', 'd', ',', 'y', 'L', 'l', 's', '\\r', '\\n', 'i', 'w', 'U', 'S', 'm', 'p', 'v', '.', 'Y', '-', 'I', ':', 'A', 'M', 'R', 'D', '2', '0', '1', '[', '#', '6', ']', 'J', '9', 'E', 'C', 'F', '8', 'V', '*', 'O', 'H', 'N', 'K', '/', '5', 'X', '7', '3', '!', '?', ';', 'x', '(', ')', 'z', 'q', 'Q', 'Z', '4', '=', '\"', '%', '\\'', '$']\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"n = length(str)\n",
|
||||||
|
"letters = unique(str)\n",
|
||||||
|
"println(\"number of letters and unique letters in W&P: \", n,\" \",length(letters))\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"\\nunique letters: \",letters)\n",
|
||||||
|
"\n",
|
||||||
|
"vv = isascii.(letters)\n",
|
||||||
|
"println(\"\\nASCII letters: \",letters[vv])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {
|
||||||
|
"tags": []
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"number of words in W&P, including pre-amble, etc: 566334\n",
|
||||||
|
"number of lines in W&P, including pre-amble, etc: 66033\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"words = split(str)\n",
|
||||||
|
"println(\"number of words in W&P, including pre-amble, etc: \",length(words))\n",
|
||||||
|
"\n",
|
||||||
|
"lines = split(str,\"\\n\")\n",
|
||||||
|
"println(\"number of lines in W&P, including pre-amble, etc: \",length(lines))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {
|
||||||
|
"tags": []
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"How many unique words are there in the file?\n",
|
||||||
|
"Number of unique words: 41971\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"println(\"How many unique words are there in the file?\")\n",
|
||||||
|
"UniqueWords = unique(words)\n",
|
||||||
|
"\n",
|
||||||
|
"println(\"Number of unique words: \",length(UniqueWords))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Task 2\n",
|
||||||
|
"\n",
|
||||||
|
"1. How often is Borodinó mentioned. Hint: `occursin.(,words)`\n",
|
||||||
|
"\n",
|
||||||
|
"2. Print all lines that contain the word Borodinó. Hint: `occursin(,line)`"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {
|
||||||
|
"tags": []
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"How often is Borodinó mentioned?\n",
|
||||||
|
"108\n",
|
||||||
|
"108\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"println(\"How often is Borodinó mentioned?\") \n",
|
||||||
|
"\n",
|
||||||
|
"println(sum(occursin.(\"Borodinó\",words))) #\"Borodinó\" or \"Borodinó. or similarly\n",
|
||||||
|
"\n",
|
||||||
|
"println(sum(z->occursin(\"Borodinó\",z),words)) #quicker approach"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 8,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"print the line numbers and the lines that contain the word Borodinó:\n",
|
||||||
|
"\n",
|
||||||
|
"38971 they reached Borodinó, seventy miles from Moscow. From Vyázma Napoleon\n",
|
||||||
|
"41375 the twenty-sixth the battle of Borodinó itself took place.\n",
|
||||||
|
"41377 Why and how were the battles of Shevárdino and Borodinó given and\n",
|
||||||
|
"41378 accepted? Why was the battle of Borodinó fought? There was not the least\n",
|
||||||
|
"41399 Before the battle of Borodinó our strength in proportion to the French\n",
|
||||||
|
"41416 In giving and accepting battle at Borodinó, Kutúzov acted involuntarily\n",
|
||||||
|
"41427 On the other question, how the battle of Borodinó and the preceding\n",
|
||||||
|
"41434 position at Borodinó.\n",
|
||||||
|
"41438 to it, from Borodinó to Utítsa, at the very place where the battle was\n",
|
||||||
|
"41445 the field of Borodinó.\n",
|
||||||
|
"41451 during the retreat passed many positions better than Borodinó. They did\n",
|
||||||
|
"41457 and that the position at Borodinó (the one where the battle was fought),\n",
|
||||||
|
"41463 Borodinó to the left of, and at a right angle to, the highroad (that\n",
|
||||||
|
"41481 later, when reports on the battle of Borodinó were written at leisure,\n",
|
||||||
|
"41486 the battle of Borodinó was fought by us on an entrenched position\n",
|
||||||
|
"41493 village of Nóvoe, and the center at Borodinó at the confluence of the\n",
|
||||||
|
"41496 To anyone who looks at the field of Borodinó without thinking of how\n",
|
||||||
|
"41503 to Borodinó (he could not have seen that position because it did not\n",
|
||||||
|
"41514 Borodinó—a plain no more advantageous as a position than any other plain\n",
|
||||||
|
"41531 and chief action of the battle of Borodinó was already lost on the\n",
|
||||||
|
"41551 distinct from the main course of the battle.) So the battle of Borodinó\n",
|
||||||
|
"41554 army and people) it has been described. The battle of Borodinó was not\n",
|
||||||
|
"41557 Shevárdino Redoubt, the Russians fought the battle of Borodinó on an\n",
|
||||||
|
"41749 hundred paces in front of the knoll and below it. This was Borodinó.\n",
|
||||||
|
"41780 “Borodinó,” the other corrected him.\n",
|
||||||
|
"41814 entrenchments. There, you see? There’s our center, at Borodinó, just\n",
|
||||||
|
"41855 A church procession was coming up the hill from Borodinó. First along\n",
|
||||||
|
"42128 Borodinó and thence turned to the left, passing an enormous number of\n",
|
||||||
|
"42133 him than any other spot on the plain of Borodinó.\n",
|
||||||
|
"42643 On August 25, the eve of the battle of Borodinó, M. de Beausset, prefect\n",
|
||||||
|
"42949 The fourth order was: The vice-King will occupy the village (Borodinó)\n",
|
||||||
|
"42957 given him, he was to advance from the left through Borodinó to the\n",
|
||||||
|
"42962 not be executed. After passing through Borodinó the vice-King was driven\n",
|
||||||
|
"42982 Many historians say that the French did not win the battle of Borodinó\n",
|
||||||
|
"42993 battle of Borodinó, and if this or that other arrangement depended on\n",
|
||||||
|
"43015 men at Borodinó was not due to Napoleon’s will, though he ordered the\n",
|
||||||
|
"43022 At the battle of Borodinó Napoleon shot at no one and killed no one.\n",
|
||||||
|
"43026 The French soldiers went to kill and be killed at the battle of Borodinó\n",
|
||||||
|
"43065 than previous ones because the battle of Borodinó was the first Napoleon\n",
|
||||||
|
"43078 Napoleon at the battle of Borodinó fulfilled his office as\n",
|
||||||
|
"43286 Pierre most of all was the view of the battlefield itself, of Borodinó\n",
|
||||||
|
"43289 Above the Kolochá, in Borodinó and on both sides of it, especially to\n",
|
||||||
|
"43297 riverbanks and in Borodinó. A white church could be seen through the\n",
|
||||||
|
"43298 mist, and here and there the roofs of huts in Borodinó as well as dense\n",
|
||||||
|
"43301 the whole space. Just as in the mist-enveloped hollow near Borodinó, so\n",
|
||||||
|
"43386 bridge across the Kolochá between Górki and Borodinó, which the French\n",
|
||||||
|
"43387 (having occupied Borodinó) were attacking in the first phase of the\n",
|
||||||
|
"43819 The chief action of the battle of Borodinó was fought within the seven\n",
|
||||||
|
"43820 thousand feet between Borodinó and Bagratión’s flèches. Beyond that\n",
|
||||||
|
"43825 battlefield. On the field between Borodinó and the flèches, beside the\n",
|
||||||
|
"43834 troops advanced on Borodinó from their left.\n",
|
||||||
|
"43838 to Borodinó, so that Napoleon could not see what was happening there,\n",
|
||||||
|
"43886 Borodinó had been occupied and the bridge over the Kolochá was in the\n",
|
||||||
|
"43890 as soon in fact as the adjutant had left Borodinó—the bridge had been\n",
|
||||||
|
"44222 times repulsed. In the center the French had not got beyond Borodinó,\n",
|
||||||
|
"44301 of the field of Borodinó.\n",
|
||||||
|
"44821 hundreds of years the peasants of Borodinó, Górki, Shevárdino, and\n",
|
||||||
|
"44903 Russians at Borodinó. The French invaders, like an infuriated animal\n",
|
||||||
|
"44909 wound it had received at Borodinó. The direct consequence of the battle\n",
|
||||||
|
"44910 of Borodinó was Napoleon’s senseless flight from Moscow, his retreat\n",
|
||||||
|
"44913 which at Borodinó for the first time the hand of an opponent of stronger\n",
|
||||||
|
"45069 from Smolénsk to Borodinó. The French army pushed on to Moscow, its\n",
|
||||||
|
"45078 consolidated. At Borodinó a collision took place. Neither army was\n",
|
||||||
|
"45097 Russian army were convinced that the battle of Borodinó was a victory.\n",
|
||||||
|
"45185 the twenty-sixth at Borodinó, and each day and hour and minute of the\n",
|
||||||
|
"45186 retreat from Borodinó to Filí.\n",
|
||||||
|
"45449 After the battle of Borodinó the abandonment and burning of Moscow was\n",
|
||||||
|
"45491 that could happen. They went away even before the battle of Borodinó and\n",
|
||||||
|
"45873 Borodinó.\n",
|
||||||
|
"45881 Toward the end of the battle of Borodinó, Pierre, having run down\n",
|
||||||
|
"46434 and commotion. Every day thousands of men wounded at Borodinó were\n",
|
||||||
|
"46442 Some said there had been another battle after Borodinó at which the\n",
|
||||||
|
"47598 to the second of September, that is from the battle of Borodinó to the\n",
|
||||||
|
"48315 ever since the battle of Borodinó, for all the generals who came to\n",
|
||||||
|
"48353 if after the battle of Borodinó, when the surrender of Moscow became\n",
|
||||||
|
"49092 particularly of the battle of Borodinó and of that vague sense of his\n",
|
||||||
|
"50087 ambulance station on the field of Borodinó. His feverish state and the\n",
|
||||||
|
"50105 Borodinó. They were accompanied by a doctor, Prince Andrew’s valet, his\n",
|
||||||
|
"50835 battle of Borodinó, there was a soiree, the chief feature of which was\n",
|
||||||
|
"51280 A few days before the battle of Borodinó, Nicholas received the\n",
|
||||||
|
"51740 The dreadful news of the battle of Borodinó, of our losses in killed and\n",
|
||||||
|
"51747 When he received the news of the battle of Borodinó and the abandonment\n",
|
||||||
|
"53596 The historians consider that, next to the battle of Borodinó and the\n",
|
||||||
|
"53703 whole campaign and by the battle of Borodinó, the Russian army—when\n",
|
||||||
|
"53711 Borodinó had been a victory, he alone—who as commander in chief might\n",
|
||||||
|
"53715 The beast wounded at Borodinó was lying where the fleeing hunter had\n",
|
||||||
|
"54244 or deliberately deceive themselves. No battle—Tarútino, Borodinó, or\n",
|
||||||
|
"54858 of Borodinó. He had sought it in philanthropy, in Freemasonry, in the\n",
|
||||||
|
"55314 day long. At the battle of Borodinó, when Bagratión was killed and nine\n",
|
||||||
|
"55319 And the quiet little Dokhtúrov rode thither, and Borodinó became the\n",
|
||||||
|
"55543 The undecided question as to whether the wound inflicted at Borodinó was\n",
|
||||||
|
"55658 That army could not recover anywhere. Since the battle of Borodinó\n",
|
||||||
|
"55805 The Battle of Borodinó, with the occupation of Moscow that followed it\n",
|
||||||
|
"55840 history: to say that the field of battle at Borodinó remained in the\n",
|
||||||
|
"55844 After the French victory at Borodinó there was no general engagement nor\n",
|
||||||
|
"55855 The period of the campaign of 1812 from the battle of Borodinó to the\n",
|
||||||
|
"55895 retreats after battles, the blow dealt at Borodinó and the renewed\n",
|
||||||
|
"57692 done at Mozháysk after the battle of Borodinó.\n",
|
||||||
|
"58038 French had given battle at Borodinó, did not achieve its purpose when it\n",
|
||||||
|
"58063 enemy in full strength at Borodinó—defeated at Krásnoe and the Berëzina\n",
|
||||||
|
"58772 activity in 1812, never once swerving by word or deed from Borodinó to\n",
|
||||||
|
"58819 Beginning with the battle of Borodinó, from which time his disagreement\n",
|
||||||
|
"58820 with those about him began, he alone said that the battle of Borodinó\n",
|
||||||
|
"58845 this enemy of decisive action, gave battle at Borodinó, investing the\n",
|
||||||
|
"58848 contradiction to everyone else, declared till his death that Borodinó\n",
|
||||||
|
"59762 Borodinó for more than a month had recently died in the Rostóvs’ house\n",
|
||||||
|
"60187 one at Borodinó.\n",
|
||||||
|
"61413 the cold in his head at Borodinó to the sparks which set Moscow on\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"println(\"print the line numbers and the lines that contain the word Borodinó:\\n\")\n",
|
||||||
|
"for (i,line) in enumerate(lines)\n",
|
||||||
|
" if occursin(\"Borodinó\",line)\n",
|
||||||
|
" println(i,\" \",line)\n",
|
||||||
|
" end\n",
|
||||||
|
"end"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Task 3\n",
|
||||||
|
"\n",
|
||||||
|
"1. Change Borodinó everywhere to Berëzina and then count the occurances"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 9,
|
||||||
|
"metadata": {
|
||||||
|
"tags": []
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Change Borodinó everywhere to Berëzina and then count the occurances\n",
|
||||||
|
"125\n",
|
||||||
|
"125\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"println(\"Change Borodinó everywhere to Berëzina and then count the occurances\")\n",
|
||||||
|
"str2 = replace(str,\"Borodinó\"=>\"Berëzina\");\n",
|
||||||
|
"words2 = split(str2)\n",
|
||||||
|
"println(sum(occursin.(\"Berëzina\",words2)))\n",
|
||||||
|
"\n",
|
||||||
|
"println(sum(z->occursin(\"Berëzina\",z),words2)) #quicker approach"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"@webio": {
|
||||||
|
"lastCommId": null,
|
||||||
|
"lastKernelId": null
|
||||||
|
},
|
||||||
|
"anaconda-cloud": {},
|
||||||
|
"kernel_info": {
|
||||||
|
"name": "julia-1.2"
|
||||||
|
},
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Julia 1.7.0",
|
||||||
|
"language": "julia",
|
||||||
|
"name": "julia-1.7"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"file_extension": ".jl",
|
||||||
|
"mimetype": "application/julia",
|
||||||
|
"name": "julia",
|
||||||
|
"version": "1.7.0"
|
||||||
|
},
|
||||||
|
"nteract": {
|
||||||
|
"version": "0.24.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 4
|
||||||
|
}
|
559
Problemsets/Solutions/PS10_DataFrames_Solution.ipynb
Normal file
559
Problemsets/Solutions/PS10_DataFrames_Solution.ipynb
Normal file
File diff suppressed because one or more lines are too long
645
Problemsets/Solutions/PS10b_OptionDeltas_Solution.ipynb
Normal file
645
Problemsets/Solutions/PS10b_OptionDeltas_Solution.ipynb
Normal file
File diff suppressed because one or more lines are too long
112
Problemsets/jlFiles/OlsNW.jl
Normal file
112
Problemsets/jlFiles/OlsNW.jl
Normal 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
|
||||||
|
#------------------------------------------------------------------------------
|
104
Problemsets/jlFiles/OptionsCalculations.jl
Normal file
104
Problemsets/jlFiles/OptionsCalculations.jl
Normal 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
|
Loading…
Reference in New Issue
Block a user