Skip to content

Commit

Permalink
Updated Documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Al-Ahmadgaid Asaad committed Apr 13, 2017
1 parent c561c43 commit 048e94a
Showing 1 changed file with 0 additions and 70 deletions.
70 changes: 0 additions & 70 deletions src/HMC.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,76 +2,6 @@
HAMILTONIAN MONTE CARLO TYPE INSTANTIATION
Approximate Bayesian Inference via Hamiltonian Monte Carlo.
**Examples:**
```julia
using DataFrames
using Distributions
using Gadfly
using StochMCMC
Gadfly.push_theme(:dark)
srand(123);
# Define data parameters
w0 = -.3; w1 = -.5; stdev = 5.; a = 1 / stdev
# Generate Hypothetical Data
n = 200;
x = rand(Uniform(-1, 1), n);
A = [ones(length(x)) x];
B = [w0; w1];
f = A * B;
y = f + rand(Normal(0, a), n);
my_df = DataFrame(Independent = round(x, 4), Dependent = round(y, 4));
"""
The log prior function is given by the following codes:
"""
function logprior(theta::Array{Float64}; mu::Array{Float64} = zero_vec, s::Array{Float64} = eye_mat)
w0_prior = log(pdf(Normal(mu[1, 1], s[1, 1]), theta[1]))
w1_prior = log(pdf(Normal(mu[2, 1], s[2, 2]), theta[2]))
w_prior = [w0_prior w1_prior]
return w_prior |> sum
end
"""
The log likelihood function is given by the following codes:
"""
function loglike(theta::Array{Float64}; alpha::Float64 = a, x::Array{Float64} = x, y::Array{Float64} = y)
yhat = theta[1] + theta[2] * x
likhood = Float64[]
for i in 1:length(yhat)
push!(likhood, log(pdf(Normal(yhat[i], alpha), y[i])))
end
return likhood |> sum
end
"""
The log posterior function is given by the following codes:
"""
function logpost(theta::Array{Float64})
loglike(theta, alpha = a, x = x, y = y) + logprior(theta, mu = zero_vec, s = eye_mat)
end
U(theta::Array{Float64}) = - logpost(theta);
K(p::Array{Float64}; Σ = eye(length(p))) = (p' * inv(Σ) * p) / 2;
function dU(theta::Array{Float64}; alpha::Float64 = a, b::Float64 = eye_mat[1, 1])
[-alpha * sum(y - (theta[1] + theta[2] * x));
-alpha * sum((y - (theta[1] + theta[2] * x)) .* x)] + b * theta
end
dK(p::AbstractArray{Float64}; Σ::Array{Float64} = eye(length(p))) = inv(Σ) * p;
srand(123);
HMC_object = HMC(U, K, dU, dK, zeros(2), 2);
chain2 = mcmc(HMC_object, leapfrog_params = Dict([:ɛ => .09, :τ => 20]), r = 10000);
```
"""
immutable HMC
U ::Function
Expand Down

0 comments on commit 048e94a

Please sign in to comment.