Skip to content

Instantly share code, notes, and snippets.

@bkamins
Created August 27, 2022 17:49
Show Gist options
  • Save bkamins/7038c6814a36416a3c21c87b641511b3 to your computer and use it in GitHub Desktop.
Save bkamins/7038c6814a36416a3c21c87b641511b3 to your computer and use it in GitHub Desktop.
Julia implementation of a model of volatility clustering on financial markets
using DataFrames
using Plots
using Random
using StatsBase
function cont_run(time=10000, n=10000, λ=0.05, q=0.1)
r = zeros(time)
θ = zeros(n)
pchange = zeros(n)
for t = 1:time
ε = randn()
if ε > 0
r[t] = sum(<(ε), θ) / (λ * n)
else
r[t] = -sum(<(-ε), θ) / (λ * n)
end
θ .= ifelse.(rand!(pchange) .< q, abs(r[t]), θ)
end
return kurtosis(r)
end
@time cont_run()
@time cont_run()
function run_sim(time=5000, n=5000)
df = DataFrame()
for λ in range(0.01, 0.05, length=21), q in range(0.01, 0.05, length=21)
push!(df, (λ=λ, q=q, k=cont_run(time, n, λ, q)))
end
return df
end
Random.seed!(1234); # ensure reproducibility of the results
@time df = run_sim()
df_reshape = unstack(df, :λ, :q, :k)
heatmap(df_reshape.λ, names(df_reshape)[2:end],
Matrix(df_reshape[:, Not(:λ)]),
xlabel="λ", ylabel="q", title="kurtosis")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment