s-broda / ARCHModels.jl
Showing 3 of 5 files from the diff.

@@ -287,7 +287,7 @@
Loading
287 287
#dimensional array of the right type.
288 288
@inline function loglik!(ht::AbstractVector{T2}, lht::AbstractVector{T2},
289 289
                         zt::AbstractVector{T2}, at::AbstractVector{T2}, vs::Type{VS}, ::Type{SD}, meanspec::MS,
290 -
                         data::Vector{T1}, coefs::AbstractVector{T3}, subsetmask=trues(nparams(vs))
290 +
                         data::Vector{T1}, coefs::AbstractVector{T3}, subsetmask=trues(nparams(vs)), returnearly=false
291 291
                         ) where {VS<:UnivariateVolatilitySpec, SD<:StandardizedDistribution,
292 292
                                  MS<:MeanSpec, T1<:AbstractFloat, T2, T3
293 293
                                  }
@@ -295,13 +295,14 @@
Loading
295 295
	lowergarch, uppergarch = constraints(VS, T1)
296 296
	lowerdist, upperdist = constraints(SD, T1)
297 297
    lowermean, uppermean = constraints(MS, T1)
298 -
    all(lowerdist.<distcoefs.<upperdist) && all(lowermean.<meancoefs.<uppermean) && all(lowergarch[subsetmask].<garchcoefs[subsetmask].<uppergarch[subsetmask]) || return T2(-Inf)
298 +
	all_inbounds = all(lowerdist.<distcoefs.<upperdist) && all(lowermean.<meancoefs.<uppermean) && all(lowergarch[subsetmask].<garchcoefs[subsetmask].<uppergarch[subsetmask])
299 +
    returnearly && !all_inbounds && return T2(-Inf)
299 300
	garchcoefs .*= subsetmask
300 301
    T = length(data)
301 302
	r1 = presample(VS)
302 303
	r2 = presample(meanspec)
303 304
    r = max(r1, r2)
304 -
    T > r || error("Sample too small.")
305 +
    T - r > 0 || error("Sample too small.")
305 306
	ki = kernelinvariants(SD, distcoefs)
306 307
    @inbounds begin
307 308
        h0 = var(data) # could be moved outside
@@ -329,10 +330,11 @@
Loading
329 330
        end#for
330 331
    end#inbounds
331 332
    LL += T*logconst(SD, distcoefs)
333 +
	return all_inbounds ? LL : T2(-Inf)
332 334
end#function
333 335
334 336
function loglik(spec::Type{VS}, dist::Type{SD}, meanspec::MS,
335 -
                   data::Vector{<:AbstractFloat}, coefs::AbstractVector{T2}, subsetmask=trues(nparams(spec))
337 +
                   data::Vector{<:AbstractFloat}, coefs::AbstractVector{T2}, subsetmask=trues(nparams(spec)), returnearly=false
336 338
                   ) where {VS<:UnivariateVolatilitySpec, SD<:StandardizedDistribution,
337 339
                            MS<:MeanSpec, T2
338 340
                            }
@@ -342,7 +344,7 @@
Loading
342 344
    lht = CircularBuffer{T2}(r)
343 345
    zt = CircularBuffer{T2}(r)
344 346
	at = CircularBuffer{T2}(r)
345 -
    loglik!(ht, lht, zt, at, spec, dist, meanspec, data, coefs, subsetmask)
347 +
    loglik!(ht, lht, zt, at, spec, dist, meanspec, data, coefs, subsetmask, returnearly)
346 348
347 349
end
348 350
@@ -375,7 +377,7 @@
Loading
375 377
              ) where {VS<:UnivariateVolatilitySpec, SD<:StandardizedDistribution,
376 378
                       MS<:MeanSpec, T<:AbstractFloat
377 379
                       }
378 -
    obj = x -> -loglik(VS, SD, meanspec, data, x)
380 +
    obj = x -> -loglik(VS, SD, meanspec, data, x, trues(length(garchcoefs)), true)
379 381
    coefs = vcat(garchcoefs, distcoefs, meancoefs)
380 382
    res = optimize(obj, coefs, algorithm; autodiff=autodiff, kwargs...)
381 383
    coefs .= Optim.minimizer(res)
@@ -458,7 +460,7 @@
Loading
458 460
	distcoefs = startingvals(SD, data)
459 461
    meancoefs = startingvals(ms, data)
460 462
461 -
	obj = x -> -loglik(VS_large, SD, ms, data, x, mask)
463 +
	obj = x -> -loglik(VS_large, SD, ms, data, x, mask, true)
462 464
    coefs = vcat(garchcoefs, distcoefs, meancoefs)
463 465
    res = optimize(obj, coefs, algorithm; autodiff=autodiff, kwargs...)
464 466
    coefs .= Optim.minimizer(res)
@@ -511,7 +513,7 @@
Loading
511 513
- `algorithm=BFGS(), autodiff=:forward, kwargs...`: passed on to the optimizer.
512 514
513 515
# Example
514 -
```jldoctest
516 +
```
515 517
julia> selectmodel(EGARCH, BG96)
516 518
517 519
EGARCH{1, 1, 2} model with Gaussian errors, T=1974.

@@ -171,7 +171,7 @@
Loading
171 171
    return lower, upper
172 172
end
173 173
174 -
function startingvals(::ARMA{p, q, T}, data::Vector{T})  where {p, q, T<:AbstractFloat}
174 +
function startingvals(mod::ARMA{p, q, T}, data::Vector{T})  where {p, q, T<:AbstractFloat}
175 175
    N = length(data)
176 176
    X = Matrix{T}(undef, N-p, p+1)
177 177
    X[:, 1] .= T(1)
@@ -179,6 +179,9 @@
Loading
179 179
        X[:, i+1] .= data[p-i+1:N-i]
180 180
    end
181 181
    phi = X \ data[p+1:end]
182 +
    lower, upper = constraints(ARMA{p, q}, T)
183 +
    phi[2:end] .= max.(phi[2:end], lower[2:p+1]*.99)
184 +
    phi[2:end] .= min.(phi[2:end], upper[2:p+1]*.99)
182 185
    return T[phi..., zeros(T, q)...]
183 186
end
184 187

@@ -44,7 +44,7 @@
Loading
44 44
    Ji = try
45 45
        inv(J)
46 46
    catch e
47 -
        if e isa LinearAlgebra.SingularException
47 +
        if e in [LinearAlgebra.SingularException, LinearAlgebra.LAPACKException(1)]
48 48
            @warn "Fisher information is singular; vcov matrix is inaccurate."
49 49
            pinv(J)
50 50
        else
Files Coverage
src 99.41%
Project Totals (12 files) 99.41%
Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading