Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
1 views
unlisted
ubuntu2204
Kernel: Julia 1.10
import Pkg Pkg.add(["LaTeXStrings"])
Warning: could not download https://pkg.julialang.org/registries exception = RequestError: Failed to connect to pkg.julialang.org port 443 after 30002 ms: Timeout was reached while requesting https://pkg.julialang.org/registries @ Pkg.Registry /ext/julia/julia-1.10.0/share/julia/stdlib/v1.10/Pkg/src/Registry/Registry.jl:69 Resolving package versions... No Changes to `~/.julia/environment/v1.10/Project.toml` No Changes to `~/.julia/environment/v1.10/Manifest.toml`
using Plots using CSV using DataFrames using LaTeXStrings us_500 = DataFrame(CSV.File("HistoricalPricesUS500.csv")) eur_600 = DataFrame(CSV.File("HistoricalPricesSTOXX600.csv")) jp_225 = DataFrame(CSV.File("HistoricalPricesJP225.csv")) function col_name_format!(df_array) for df in df_array try select!(df, :Date, " Close" => :Close) catch end end end function format_date_string!(df) index_99 = 1 try while df.Date[index_99][7] != '9' index_99 += 1 end println(index_99) @inbounds for row in 1:index_99-1 df.Date[row] = df.Date[row][1:6] * "20" * df.Date[row][7:8] end @inbounds for row in index_99:size(df, 1) df.Date[row] = df.Date[row][1:6] * "19" * df.Date[row][7:8] end catch BoundsError println("You have already formatted this dataframe") end end col_name_format!([eur_600, jp_225, us_500]) format_date_string!(eur_600) format_date_string!(jp_225) format_date_string!(us_500) CSV.write("HistoricalPricesJP225-formatted.csv", jp_225) CSV.write("HistoricalPricesSTOXX600-formatted.csv", eur_600) CSV.write("HistoricalPricesUS500-formatted.csv", us_500)
You have already formatted this dataframe You have already formatted this dataframe You have already formatted this dataframe
"HistoricalPricesUS500-formatted.csv"
us_500 = DataFrame(CSV.File("HistoricalPricesUS500-formatted.csv", dateformat = "mm/dd/yy")) eur_600 = DataFrame(CSV.File("HistoricalPricesSTOXX600-formatted.csv", dateformat = "mm/dd/yy")) jp_225 = DataFrame(CSV.File("HistoricalPricesJP225-formatted.csv", dateformat = "mm/dd/yy")) sort!.([jp_225, us_500, eur_600], :Date) mean = x -> sum(x)/length(x) standard_dev = x -> sqrt((sum(x.^2) + length(x)*mean(x)^2)/(length(x) - 1)) function add_log_changes!(df) df.Change = append!([0.0], [log(df.Close[i]/df.Close[i-1]) for i in 2:size(df, 1)]) end function add_vol_ma!(df, window::Int64) df."VolatilityMA_" = append!(fill(0.0, window + 1), [standard_dev(df.Change[(i - window):i])*sqrt(252) for i in (window + 2):length(df.Change)]) select!(df, Not(:VolatilityMA_), :VolatilityMA_ => "VolatilityMA_$window") end function plot_volatilities(df, title) return plot(df.Date , [df.VolatilityMA_20 df.VolatilityMA_60 df.constant_vol], label = [L"σ \ ∼ \ MA, T = 20" L"σ \ ∼ \ MA, T = 60" L"σ = Constant \ vol"], title = title) end add_log_changes!.([jp_225, us_500, eur_600]) add_vol_ma!.([jp_225, us_500, eur_600], 20) add_vol_ma!.([jp_225, us_500, eur_600], 60) us_500.constant_vol = standard_dev(us_500.Change)*sqrt(252) jp_225.constant_vol = standard_dev(jp_225.Change)*sqrt(252) eur_600.constant_vol = standard_dev(eur_600.Change)*sqrt(252) plot() display(plot_volatilities(us_500, "S&P500 Volatility over time")) display(plot_volatilities(eur_600, "STOXX600 Volatility over time")) display(plot_volatilities(jp_225, "NIKKEI225 Volatility over time"))
Image in a Jupyter notebookImage in a Jupyter notebookImage in a Jupyter notebook

Below yields the incorrect result, possibly because assigning a list comprehension to a df slice is multithreaded?

function ewma_λ_094!(df) std_t = 0.0 for i in 1:99 std_t += (0.94^(i - 1))*(df.Change[(100 - i)])^2 end std_t = sqrt(252 * std_t * (1-0.94)) df.ewma_1 = Array{Float64, 1}(undef, size(df, 1)) df.ewma_1[1:100] = append!(fill(0.0, 99), [std_t]) df.ewma_1[101:end] = [sqrt( (0.94*(df.Change[i]^2) + 0.06*((df.ewma_1[i - 1])^2)) * sqrt(252) ) for i in 101:size(df, 1)] return df end ewma_λ_094!(us_500) println("ewma_1, row 4 should be: ",sqrt( (0.94*(us_500.Change[102]^2) + 0.06*(us_500.ewma_1[101]^2)) *sqrt(252))) us_500[99:154, :ewma_1]
ewma_1, row 4 should be: 0.15140859291637426
56-element Vector{Float64}: 0.0 0.1452236480399996 0.15262717563008693 0.027142406397633143 0.022818624494616673 0.06220258087804803 0.033831809822339486 0.016704481403294353 0.008728944160058675 0.07271728837002381 0.005827179789317804 0.014358988371999625 0.0173644618825166 ⋮ 0.004437935353399633 705628.4313832406 705629.4073291771 705632.3351669885 705633.311112925 705634.2870588622 705635.2630047991 705636.238950736 705639.1667885471 0.025828263269288776 0.055219108804401736 0.009699900965760056

This one gives the correct result

function ewma_094(df) nrows = nrow(df) df.ewma_1 = zeros(nrows) df.ewma_1[100] = sqrt( 252 * sum( 0.94^(i-1) * df.Change[(100-i)]^2 for i in 1:99 ) * (1-0.94)) for ridx in 101:nrows df.ewma_1[ridx] = sqrt( (0.94*(df.Change[ridx]^2) + 0.06*((df.ewma_1[ridx - 1])^2)) * sqrt(252) ) end return df end us_500 = ewma_094(us_500) us_500.ewma_1[99:110]
12-element Vector{Float64}: 0.0 0.1452236480399996 0.15262717563008693 0.15140859291637426 0.14951808593099433 0.15862618073364068 0.15846421040896336 0.1555520368563569 0.15206112404377298 0.16526156207840798 0.16139158199426953 0.15816260661792444