Skip to content

Commit

Permalink
Merge 5a95f33 into 25e9ef3
Browse files Browse the repository at this point in the history
  • Loading branch information
mschauer committed Sep 4, 2018
2 parents 25e9ef3 + 5a95f33 commit ce68745
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 21 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Expand Up @@ -4,7 +4,8 @@ os:
- linux
- osx
julia:
- 0.6
- 0.7
- 1.0
- nightly
notifications:
email: false
Expand Down
3 changes: 2 additions & 1 deletion REQUIRE
@@ -1,2 +1,3 @@
julia 0.6
julia 0.7
Distributions
SpecialFunctions
4 changes: 3 additions & 1 deletion src/MicrostructureNoise.jl
@@ -1,6 +1,8 @@
module MicrostructureNoise

using Distributions
using SpecialFunctions, Distributions

using Statistics

include("microstructure.jl")

Expand Down
24 changes: 11 additions & 13 deletions src/microstructure.jl
Expand Up @@ -120,7 +120,7 @@ function MCMC(Π::Union{Prior,Dict}, tt, y, α0::Float64, σα, iterations; subi


Z = zeros(N)
ii = Vector(N) # vector of start indices of increments
ii = Vector(undef, N) # vector of start indices of increments
td = zeros(N+1)
for k in 1:N
if k == N
Expand All @@ -139,9 +139,7 @@ function MCMC(Π::Union{Prior,Dict}, tt, y, α0::Float64, σα, iterations; subi
acc = 0
αs = Float64[]
si = 1
fixalpha = false
fixeta = false


μ = zeros(n+1)
C = zeros(n+1)
θs = Any[]
Expand All @@ -151,7 +149,7 @@ function MCMC(Π::Union{Prior,Dict}, tt, y, α0::Float64, σα, iterations; subi

for iter in 1:iterations
# update Zk (necessary because x changes)
if !(eta == 0.0 && fixeta)
if !(η == 0.0 && fixeta)
for k in 1:N
Z[k] = sum((x[i+1] - x[i]).^2 ./ (tt[i+1]-tt[i]) for i in ii[k])
end
Expand Down Expand Up @@ -194,7 +192,7 @@ function MCMC(Π::Union{Prior,Dict}, tt, y, α0::Float64, σα, iterations; subi
end
if !fixeta
# update eta
assert(length(x) == n + 1)
@assert(length(x) == n + 1)
z = sum((x[i] - y[i])^2 for i in 2:(n+1))
η = rand(InverseGamma(αη + n/2, βη + z/2))

Expand All @@ -207,7 +205,7 @@ function MCMC(Π::Union{Prior,Dict}, tt, y, α0::Float64, σα, iterations; subi
# Sample x from Kalman smoothing distribution

# Forward pass
if eta == 0.0 && fixeta
if η == 0.0 && fixeta
# do nothing
else
C[1] = C0
Expand Down Expand Up @@ -254,7 +252,7 @@ function MCMC(Π::Union{Prior,Dict}, tt, y, α0::Float64, σα, iterations; subi
end
end

td, samples, ηs, αs, round(acc/iterations, 3)
td, samples, ηs, αs, round(acc/iterations, digits=3)
end

"""
Expand Down Expand Up @@ -293,11 +291,11 @@ posterior median and mean of `s^2`, as well as posterior mean of `s`.
function posterior_volatility(td, samples; burnin = size(samples, 2)÷3, qu = 0.90)
p = 1.0 - qu
A = view(samples, :, burnin:size(samples, 2))
post_qup = mapslices(v-> quantile(v, 1 - p/2), A, 2)
post_mean = mean(A, 2)
post_mean_root = mean(sqrt.(A), 2)
post_median = median(A, 2)
post_qlow = mapslices(v-> quantile(v, p/2), A, 2)
post_qup = mapslices(v-> quantile(v, 1 - p/2), A, dims=2)
post_mean = mean(A, dims=2)
post_mean_root = mean(sqrt.(A), dims=2)
post_median = median(A, dims=2)
post_qlow = mapslices(v-> quantile(v, p/2), A, dims=2)
Posterior(
td,
post_qlow,
Expand Down
2 changes: 1 addition & 1 deletion test/piecewise.jl
@@ -1,3 +1,3 @@
using Base.Test, MicrostructureNoise
using Test, MicrostructureNoise

@test MicrostructureNoise.piecewise([0,1,2],[10,11]) == ([0, 1, 1, 2], [10, 10, 11, 11])
2 changes: 1 addition & 1 deletion test/runtests.jl
@@ -1,5 +1,5 @@
using MicrostructureNoise
using Base.Test
using Test

include("smoketest.jl")
include("piecewise.jl")
Expand Down
9 changes: 6 additions & 3 deletions test/smoketest.jl
@@ -1,5 +1,8 @@
using MicrostructureNoise, Distributions, Base.Test
srand(10)
using MicrostructureNoise, Distributions

using Random, LinearAlgebra

Random.seed!(10)
η = 0.01

n = 1000
Expand Down Expand Up @@ -35,4 +38,4 @@ posterior = MicrostructureNoise.posterior_volatility(td, θs)
# Test posterior mean versus truth

@test abs(mean(ηs)-η) < 0.2*η
@test norm(diff(td).*(mean(θs,2) - σ.(td[1:end-1]).^2)) < 1
@test norm(diff(td).*(mean(θs, dims=2) - σ.(td[1:end-1]).^2)) < 1

0 comments on commit ce68745

Please sign in to comment.