-
Notifications
You must be signed in to change notification settings - Fork 607
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Some updates in progress of revising LOO/WAIC methods. - Added a cross-validation function for quap fits. see cv_quap - helper functions for ulam growing closer to feature complete
- Loading branch information
Richard McElreath
committed
Jan 1, 2019
1 parent
e627f5d
commit ff4c6f1
Showing
13 changed files
with
307 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
Package: rethinking | ||
Type: Package | ||
Title: Statistical Rethinking book package | ||
Version: 1.80 | ||
Date: 2018-10-23 | ||
Version: 1.81 | ||
Date: 2019-01-01 | ||
Author: Richard McElreath | ||
Maintainer: Richard McElreath <[email protected]> | ||
Imports: coda, MASS, mvtnorm, loo | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# SIMPLE IMPLEMENTATION OF HAMILTONIAN MONTE CARLO. | ||
# | ||
# Modified slightly based on code by | ||
# Radford M. Neal, 2010. | ||
# | ||
# This original appears in Figure 2 of "MCMC using Hamiltonian dynamics", | ||
# the Handbook of Markov Chain Monte Carlo. | ||
# | ||
# The arguments to the HMC function are as follows: | ||
# | ||
# U A function to evaluate minus the log of the density of the | ||
# distribution to be sampled, plus any constant - ie, the | ||
# "potential energy". | ||
# | ||
# grad_U A function to evaluate the gradient of U. | ||
# | ||
# epsilon The stepsize to use for the leapfrog steps. | ||
# | ||
# L The number of leapfrog steps to do to propose a new state. | ||
# | ||
# current_q The current state (position variables only). | ||
# | ||
# Momentum variables are sampled from independent standard normal | ||
# distributions within this function. The value return is the vector | ||
# of new position variables (equal to current_q if the endpoint of the | ||
# trajectory was rejected). | ||
|
||
# this modified version returns trajectories for q,p | ||
# ... for arguments to pass to U and grad_U | ||
# returns H for monitoring divergences | ||
HMC2 <- function (U, grad_U, epsilon, L, current_q , ... ) { | ||
q = current_q | ||
p = rnorm(length(q),0,1) # independent standard normal variates | ||
current_p = p | ||
# Make a half step for momentum at the beginning | ||
p = p - epsilon * grad_U( q , ... ) / 2 | ||
# Alternate full steps for position and momentum | ||
qtraj <- matrix(NA,nrow=L+1,ncol=length(q)) | ||
ptraj <- qtraj | ||
qtraj[1,] <- current_q | ||
ptraj[1,] <- p | ||
for (i in 1:L) | ||
{ | ||
# Make a full step for the position | ||
q = q + epsilon * p | ||
# Make a full step for the momentum, except at end of trajectory | ||
if (i!=L) { | ||
p = p - epsilon * grad_U(q,...) | ||
ptraj[i+1,] <- p | ||
} | ||
qtraj[i+1,] <- q | ||
} | ||
# Make a half step for momentum at the end. | ||
p = p - epsilon * grad_U(q,...) / 2 | ||
ptraj[L+1,] <- p | ||
# Negate momentum at end of trajectory to make the proposal symmetric | ||
p = -p | ||
# Evaluate potential and kinetic energies at start and end of trajectory | ||
current_U = U(current_q,...) | ||
current_K = sum(current_p^2) / 2 | ||
proposed_U = U(q,...) | ||
proposed_K = sum(p^2) / 2 | ||
H0 <- current_U + current_K | ||
H1 <- proposed_U + proposed_K | ||
# Accept or reject the state at end of trajectory, returning either | ||
# the position at the end of the trajectory or the initial position | ||
new_q <- q | ||
accept <- 0 | ||
if (runif(1) < exp(current_U-proposed_U+current_K-proposed_K)) { | ||
new_q <- q # accept | ||
accept <- 1 | ||
} else | ||
new_q <- current_q # reject | ||
|
||
return( | ||
list( | ||
q = new_q, | ||
traj = qtraj, | ||
ptraj = ptraj, | ||
accept = accept , | ||
dH = H1 - H0 ) ) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.