Skip to content

Commit d54630e

Browse files
authored
Use BlueStyle (#19)
1 parent e03cf75 commit d54630e

File tree

9 files changed

+205
-176
lines changed

9 files changed

+205
-176
lines changed

.JuliaFormatter.toml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
style="blue"

.github/workflows/Format.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Format
2+
3+
on:
4+
pull_request:
5+
6+
jobs:
7+
format:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v2
11+
- uses: julia-actions/setup-julia@latest
12+
with:
13+
version: 1
14+
- name: Install JuliaFormatter and format code
15+
run: |
16+
using Pkg: Pkg
17+
Pkg.add(; name="JuliaFormatter", uuid="98e50ef6-434e-11e9-1051-2b60c6c9e899")
18+
using JuliaFormatter
19+
format("."; verbose=true)
20+
shell: julia --color=yes {0}
21+
- uses: reviewdog/action-suggester@v1
22+
with:
23+
tool_name: JuliaFormatter
24+
fail_on_error: true

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Julia implementation of elliptical slice sampling.
66
[![Build Status](https://github.com/TuringLang/EllipticalSliceSampling.jl/workflows/CI/badge.svg?branch=main)](https://github.com/TuringLang/EllipticalSliceSampling.jl/actions?query=workflow%3ACI%20branch%3Amain)
77
[![Codecov](https://codecov.io/gh/TuringLang/EllipticalSliceSampling.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/TuringLang/EllipticalSliceSampling.jl)
88
[![Coveralls](https://coveralls.io/repos/github/TuringLang/EllipticalSliceSampling.jl/badge.svg?branch=main)](https://coveralls.io/github/TuringLang/EllipticalSliceSampling.jl?branch=main)
9+
[![Code Style: Blue](https://img.shields.io/badge/code%20style-blue-4495d1.svg)](https://github.com/invenia/BlueStyle)
910

1011
## Overview
1112

src/EllipticalSliceSampling.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
module EllipticalSliceSampling
22

3-
import AbstractMCMC
4-
import ArrayInterface
5-
import Distributions
3+
using AbstractMCMC: AbstractMCMC
4+
using ArrayInterface: ArrayInterface
5+
using Distributions: Distributions
66

7-
import Random
8-
import Statistics
7+
using Random: Random
8+
using Statistics: Statistics
99

1010
export ESSModel, ESS
1111

src/abstractmcmc.jl

+2-5
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ end
1919

2020
# first step of the elliptical slice sampler
2121
function AbstractMCMC.step(
22-
rng::Random.AbstractRNG,
23-
model::AbstractMCMC.AbstractModel,
24-
::ESS;
25-
kwargs...
22+
rng::Random.AbstractRNG, model::AbstractMCMC.AbstractModel, ::ESS; kwargs...
2623
)
2724
# initial sample from the Gaussian prior
2825
f = initial_sample(rng, model)
@@ -39,7 +36,7 @@ function AbstractMCMC.step(
3936
model::AbstractMCMC.AbstractModel,
4037
::ESS,
4138
state::ESSState;
42-
kwargs...
39+
kwargs...,
4340
)
4441
# obtain the prior
4542
prior = EllipticalSliceSampling.prior(model)

src/model.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ struct ESSModel{P,L} <: AbstractMCMC.AbstractModel
77
loglikelihood::L
88

99
function ESSModel{P,L}(prior::P, loglikelihood::L) where {P,L}
10-
isgaussian(P) ||
11-
error("prior distribution has to be a Gaussian distribution")
12-
new{P,L}(prior, loglikelihood)
10+
isgaussian(P) || error("prior distribution has to be a Gaussian distribution")
11+
return new{P,L}(prior, loglikelihood)
1312
end
1413
end
1514

16-
ESSModel(prior, loglikelihood) =
17-
ESSModel{typeof(prior),typeof(loglikelihood)}(prior, loglikelihood)
15+
function ESSModel(prior, loglikelihood)
16+
return ESSModel{typeof(prior),typeof(loglikelihood)}(prior, loglikelihood)
17+
end
1818

1919
# obtain prior
2020
prior(model::ESSModel) = model.prior

test/regression.jl

+52-49
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,70 @@
11
@testset "regression.jl" begin
22

3-
# number of input features
4-
N = 200
5-
# homoscedastic observation noise
6-
σ = 0.3
3+
# number of input features
4+
N = 200
5+
# homoscedastic observation noise
6+
σ = 0.3
77

8-
# experiment of Murray et al.
9-
@testset "GP regression" begin
10-
# added to the diagonal of the covariance matrix due to numerical issues of
11-
# the Cholesky decomposition
12-
jitter = 1e-9
8+
# experiment of Murray et al.
9+
@testset "GP regression" begin
10+
# added to the diagonal of the covariance matrix due to numerical issues of
11+
# the Cholesky decomposition
12+
jitter = 1e-9
1313

14-
# for different dimensions of input features
15-
for d in 1:10
16-
# sample input features
17-
inputs = rand(d, N)
14+
# for different dimensions of input features
15+
for d in 1:10
16+
# sample input features
17+
inputs = rand(d, N)
1818

19-
# define covariance matrix of latent variable
20-
# add noise to the diagonal due to numerical issues
21-
prior_Σ = Symmetric(exp.((-0.5) .* pairwise(SqEuclidean(), inputs; dims = 2))) +
22-
jitter * I
23-
prior = MvNormal(prior_Σ)
19+
# define covariance matrix of latent variable
20+
# add noise to the diagonal due to numerical issues
21+
prior_Σ =
22+
Symmetric(exp.((-0.5) .* pairwise(SqEuclidean(), inputs; dims=2))) +
23+
jitter * I
24+
prior = MvNormal(prior_Σ)
2425

25-
# sample noisy observations
26-
observations = rand(prior) .+ σ .* randn(N)
26+
# sample noisy observations
27+
observations = rand(prior) .+ σ .* randn(N)
2728

28-
# define log likelihood function
29-
(f) = let observations = observations, σ = σ
30-
logpdf(MvNormal(f, σ), observations)
31-
end
29+
# define log likelihood function
30+
(f) =
31+
let observations = observations, σ = σ
32+
logpdf(MvNormal(f, σ), observations)
33+
end
3234

33-
# run elliptical slice sampler for 100 000 time steps
34-
samples = sample(ESSModel(prior, ℓ), ESS(), 100_000; progress = false)
35+
# run elliptical slice sampler for 100 000 time steps
36+
samples = sample(ESSModel(prior, ℓ), ESS(), 100_000; progress=false)
3537

36-
# compute analytical posterior of GP
37-
posterior_Σ = prior_Σ * (I - (prior_Σ + σ^2 * I) \ prior_Σ)
38-
posterior_μ = posterior_Σ * observations / σ^2
38+
# compute analytical posterior of GP
39+
posterior_Σ = prior_Σ * (I - (prior_Σ + σ^2 * I) \ prior_Σ)
40+
posterior_μ = posterior_Σ * observations / σ^2
3941

40-
# compare with empirical estimates
41-
@test mean(samples) posterior_μ rtol = 0.05
42+
# compare with empirical estimates
43+
@test mean(samples) posterior_μ rtol = 0.05
44+
end
4245
end
43-
end
4446

45-
# extreme case with independent observations
46-
@testset "Independent components" begin
47-
# define distribution of latent variables
48-
prior = MvNormal(N, 1)
47+
# extreme case with independent observations
48+
@testset "Independent components" begin
49+
# define distribution of latent variables
50+
prior = MvNormal(N, 1)
4951

50-
# sample noisy observations
51-
observations = rand(prior) .+ σ .* randn(N)
52+
# sample noisy observations
53+
observations = rand(prior) .+ σ .* randn(N)
5254

53-
# define log likelihood function
54-
(f) = let observations = observations, σ = σ
55-
logpdf(MvNormal(f, σ), observations)
56-
end
55+
# define log likelihood function
56+
(f) =
57+
let observations = observations, σ = σ
58+
logpdf(MvNormal(f, σ), observations)
59+
end
5760

58-
# run elliptical slice sampling for 100 000 time steps
59-
samples = sample(ESSModel(prior, ℓ), ESS(), 100_000; progress = false)
61+
# run elliptical slice sampling for 100 000 time steps
62+
samples = sample(ESSModel(prior, ℓ), ESS(), 100_000; progress=false)
6063

61-
# compute analytical posterior
62-
posterior_μ = observations / (1 + σ^2)
64+
# compute analytical posterior
65+
posterior_μ = observations / (1 + σ^2)
6366

64-
# compare with empirical estimates
65-
@test mean(samples) posterior_μ rtol = 0.02
66-
end
67+
# compare with empirical estimates
68+
@test mean(samples) posterior_μ rtol = 0.02
69+
end
6770
end

test/runtests.jl

+6-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ end
2121

2222
@testset "EllipticalSliceSampling" begin
2323
println("Simple tests")
24-
@testset "Simple tests" begin include("simple.jl") end
24+
@testset "Simple tests" begin
25+
include("simple.jl")
26+
end
2527

2628
println("GP regression tests")
27-
@testset "GP regression tests" begin include("regression.jl") end
29+
@testset "GP regression tests" begin
30+
include("regression.jl")
31+
end
2832
end

0 commit comments

Comments
 (0)