|
| 1 | +using DelaySSAToolkit, JumpProcesses, OrdinaryDiffEq, Statistics |
| 2 | +using Test |
| 3 | +using Random |
| 4 | +rng = MersenneTwister(12345) |
| 5 | +delay = 20.0 |
| 6 | +function reset_history!(h; start_time = nothing) |
| 7 | + @inbounds for i in 1:length(h) |
| 8 | + h[i] = eltype(h)[] |
| 9 | + end |
| 10 | + nothing |
| 11 | +end |
| 12 | + |
| 13 | +function empirical_rate(sol, agg) |
| 14 | + if typeof(agg) <: DelayCoevolve |
| 15 | + return (sol(sol.t[end]) - sol(sol.t[1] + delay)) / (sol.t[end] - sol.t[1] - delay) |
| 16 | + else |
| 17 | + return (sol(sol.t[end]) - sol(sol.t[1])) / (sol.t[end] - sol.t[1]) |
| 18 | + end |
| 19 | +end |
| 20 | + |
| 21 | +function hawkes_rate(i::Int, g, h) |
| 22 | + function rate(u, p, t) |
| 23 | + λ, α, β = p |
| 24 | + x = zero(typeof(t)) |
| 25 | + for j in g[i] |
| 26 | + for _t in reverse(h[j]) |
| 27 | + λij = α * exp(-β * (t - _t)) |
| 28 | + if λij ≈ 0 |
| 29 | + break |
| 30 | + end |
| 31 | + x += λij |
| 32 | + end |
| 33 | + end |
| 34 | + return λ + x |
| 35 | + end |
| 36 | + return rate |
| 37 | +end |
| 38 | + |
| 39 | +function hawkes_jump(i::Int, g, h, agg; uselrate = true) |
| 40 | + rate = hawkes_rate(i, g, h) |
| 41 | + urate = rate |
| 42 | + if uselrate |
| 43 | + lrate(u, p, t) = p[1] |
| 44 | + rateinterval = (u, p, t) -> begin |
| 45 | + _lrate = lrate(u, p, t) |
| 46 | + _urate = urate(u, p, t) |
| 47 | + return _urate == _lrate ? typemax(t) : 1 / (2 * _urate) |
| 48 | + end |
| 49 | + else |
| 50 | + lrate = nothing |
| 51 | + rateinterval = (u, p, t) -> begin |
| 52 | + _urate = urate(u, p, t) |
| 53 | + return 1 / (2 * _urate) |
| 54 | + end |
| 55 | + end |
| 56 | + if typeof(agg) <: DelayCoevolve |
| 57 | + affect! = (integrator) -> begin |
| 58 | + push!(h[i], integrator.t) |
| 59 | + integrator.u[i] += 0 |
| 60 | + end |
| 61 | + else |
| 62 | + affect! = (integrator) -> begin |
| 63 | + push!(h[i], integrator.t) |
| 64 | + integrator.u[i] += 1 |
| 65 | + end |
| 66 | + end |
| 67 | + return VariableRateJump(rate, affect!; lrate, urate, rateinterval) |
| 68 | +end |
| 69 | + |
| 70 | +function hawkes_jump(u, g, h, agg; uselrate = true) |
| 71 | + return [hawkes_jump(i, g, h, agg; uselrate) for i in 1:length(u)] |
| 72 | +end |
| 73 | + |
| 74 | +function hawkes_problem(p, agg::DelayCoevolve; u = [0.0], tspan = (0.0, 50.0), |
| 75 | + save_positions = (false, true), |
| 76 | + g = [[1]], h = [[]], uselrate = true) |
| 77 | + dprob = DiscreteProblem(u, tspan, p) |
| 78 | + jumps = JumpSet(hawkes_jump(u, g, h, agg; uselrate)...) |
| 79 | + de_chan0 = [[]] |
| 80 | + delay_trigger = Dict(1=>[1=>delay]) # add a delay of 1.0 to the first jump |
| 81 | + delay_complete = Dict(1=>[1=>1]) # complete the delay will duplicate 1 product |
| 82 | + delay_interrupt = Dict() |
| 83 | + delayjumpset = DelayJumpSet(delay_trigger, delay_complete, delay_interrupt) |
| 84 | + jprob = DelayJumpProblem(dprob, agg, jumps, delayjumpset, de_chan0; dep_graph = g, save_positions, rng) |
| 85 | + return jprob |
| 86 | +end |
| 87 | + |
| 88 | +function f!(du, u, p, t) |
| 89 | + du .= 0 |
| 90 | + nothing |
| 91 | +end |
| 92 | + |
| 93 | +function hawkes_problem(p, agg; u = [0.0], tspan = (0.0, 50.0), |
| 94 | + save_positions = (false, true), |
| 95 | + g = [[1]], h = [[]], kwargs...) |
| 96 | + oprob = ODEProblem(f!, u, tspan, p) |
| 97 | + jumps = hawkes_jump(u, g, h, agg) |
| 98 | + jprob = JumpProblem(oprob, agg, jumps...; save_positions, rng) |
| 99 | + return jprob |
| 100 | +end |
| 101 | + |
| 102 | +function expected_stats_hawkes_problem(p, tspan, agg) |
| 103 | + if typeof(agg) <: DelayCoevolve |
| 104 | + T = tspan[end] - tspan[1] + delay |
| 105 | + # stepper = SSAStepper() |
| 106 | + else |
| 107 | + T = tspan[end] - tspan[1] |
| 108 | + end |
| 109 | + λ, α, β = p |
| 110 | + γ = β - α |
| 111 | + κ = β / γ |
| 112 | + Eλ = λ * κ |
| 113 | + # Equation 21 |
| 114 | + # J. Da Fonseca and R. Zaatour, |
| 115 | + # “Hawkes Process: Fast Calibration, Application to Trade Clustering and Diffusive Limit.” |
| 116 | + # Rochester, NY, Aug. 04, 2013. doi: 10.2139/ssrn.2294112. |
| 117 | + Varλ = (Eλ * (T * κ^2 + (1 - κ^2) * (1 - exp(-T * γ)) / γ)) / (T^2) |
| 118 | + return Eλ, Varλ |
| 119 | +end |
| 120 | + |
| 121 | +u0 = [0.0] |
| 122 | +p = (0.5, 0.5, 2.0) |
| 123 | +tspan = (0.0, 250.0) |
| 124 | +g = [[1]] |
| 125 | +h = [Float64[]] |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | +aggs = (Direct(), DelayCoevolve(), DelayCoevolve()) |
| 130 | +uselrate = zeros(Bool, length(aggs)) |
| 131 | +uselrate[3] = true |
| 132 | +Nsims = Int(5e2) |
| 133 | + |
| 134 | +for (i, agg) in enumerate(aggs) |
| 135 | + @info "Testing $(typeof(agg))" |
| 136 | + jump_prob = hawkes_problem(p, agg; u = u0, tspan, g, h, uselrate = uselrate[i]) |
| 137 | + if typeof(agg) <: DelayCoevolve |
| 138 | + stepper = SSAStepper() |
| 139 | + else |
| 140 | + stepper = Tsit5() |
| 141 | + end |
| 142 | + sols = Vector{ODESolution}(undef, Nsims) |
| 143 | + for n in 1:Nsims |
| 144 | + reset_history!(h) |
| 145 | + sols[n] = solve(jump_prob, stepper) |
| 146 | + end |
| 147 | + if typeof(agg) <: DelayCoevolve |
| 148 | + λs = permutedims(mapreduce((sol) -> empirical_rate(sol, agg), hcat, sols)) |
| 149 | + else |
| 150 | + cols = length(sols[1].u[1].u) |
| 151 | + λs = permutedims(mapreduce((sol) -> empirical_rate(sol, agg), hcat, sols))[:, 1:cols] |
| 152 | + end |
| 153 | + Eλ, Varλ = expected_stats_hawkes_problem(p, tspan, agg) |
| 154 | + @test isapprox(mean(λs), Eλ; atol = 0.01) |
| 155 | + @test isapprox(var(λs), Varλ; atol = 0.001) |
| 156 | +end |
0 commit comments