Daniel@0: function [samples, energies, diagn] = hmc(f, x, options, gradf, varargin) Daniel@0: %HMC Hybrid Monte Carlo sampling. Daniel@0: % Daniel@0: % Description Daniel@0: % SAMPLES = HMC(F, X, OPTIONS, GRADF) uses a hybrid Monte Carlo Daniel@0: % algorithm to sample from the distribution P ~ EXP(-F), where F is the Daniel@0: % first argument to HMC. The Markov chain starts at the point X, and Daniel@0: % the function GRADF is the gradient of the `energy' function F. Daniel@0: % Daniel@0: % HMC(F, X, OPTIONS, GRADF, P1, P2, ...) allows additional arguments to Daniel@0: % be passed to F() and GRADF(). Daniel@0: % Daniel@0: % [SAMPLES, ENERGIES, DIAGN] = HMC(F, X, OPTIONS, GRADF) also returns a Daniel@0: % log of the energy values (i.e. negative log probabilities) for the Daniel@0: % samples in ENERGIES and DIAGN, a structure containing diagnostic Daniel@0: % information (position, momentum and acceptance threshold) for each Daniel@0: % step of the chain in DIAGN.POS, DIAGN.MOM and DIAGN.ACC respectively. Daniel@0: % All candidate states (including rejected ones) are stored in Daniel@0: % DIAGN.POS. Daniel@0: % Daniel@0: % [SAMPLES, ENERGIES, DIAGN] = HMC(F, X, OPTIONS, GRADF) also returns Daniel@0: % the ENERGIES (i.e. negative log probabilities) corresponding to the Daniel@0: % samples. The DIAGN structure contains three fields: Daniel@0: % Daniel@0: % POS the position vectors of the dynamic process. Daniel@0: % Daniel@0: % MOM the momentum vectors of the dynamic process. Daniel@0: % Daniel@0: % ACC the acceptance thresholds. Daniel@0: % Daniel@0: % S = HMC('STATE') returns a state structure that contains the state of Daniel@0: % the two random number generators RAND and RANDN and the momentum of Daniel@0: % the dynamic process. These are contained in fields randstate, Daniel@0: % randnstate and mom respectively. The momentum state is only used for Daniel@0: % a persistent momentum update. Daniel@0: % Daniel@0: % HMC('STATE', S) resets the state to S. If S is an integer, then it Daniel@0: % is passed to RAND and RANDN and the momentum variable is randomised. Daniel@0: % If S is a structure returned by HMC('STATE') then it resets the Daniel@0: % generator to exactly the same state. Daniel@0: % Daniel@0: % The optional parameters in the OPTIONS vector have the following Daniel@0: % interpretations. Daniel@0: % Daniel@0: % OPTIONS(1) is set to 1 to display the energy values and rejection Daniel@0: % threshold at each step of the Markov chain. If the value is 2, then Daniel@0: % the position vectors at each step are also displayed. Daniel@0: % Daniel@0: % OPTIONS(5) is set to 1 if momentum persistence is used; default 0, Daniel@0: % for complete replacement of momentum variables. Daniel@0: % Daniel@0: % OPTIONS(7) defines the trajectory length (i.e. the number of leap- Daniel@0: % frog steps at each iteration). Minimum value 1. Daniel@0: % Daniel@0: % OPTIONS(9) is set to 1 to check the user defined gradient function. Daniel@0: % Daniel@0: % OPTIONS(14) is the number of samples retained from the Markov chain; Daniel@0: % default 100. Daniel@0: % Daniel@0: % OPTIONS(15) is the number of samples omitted from the start of the Daniel@0: % chain; default 0. Daniel@0: % Daniel@0: % OPTIONS(17) defines the momentum used when a persistent update of Daniel@0: % (leap-frog) momentum is used. This is bounded to the interval [0, Daniel@0: % 1). Daniel@0: % Daniel@0: % OPTIONS(18) is the step size used in leap-frogs; default 1/trajectory Daniel@0: % length. Daniel@0: % Daniel@0: % See also Daniel@0: % METROP Daniel@0: % Daniel@0: Daniel@0: % Copyright (c) Ian T Nabney (1996-2001) Daniel@0: Daniel@0: % Global variable to store state of momentum variables: set by set_state Daniel@0: % Used to initialise variable if set Daniel@0: global HMC_MOM Daniel@0: if nargin <= 2 Daniel@0: if ~strcmp(f, 'state') Daniel@0: error('Unknown argument to hmc'); Daniel@0: end Daniel@0: switch nargin Daniel@0: case 1 Daniel@0: samples = get_state(f); Daniel@0: return; Daniel@0: case 2 Daniel@0: set_state(f, x); Daniel@0: return; Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: display = options(1); Daniel@0: if (round(options(5) == 1)) Daniel@0: persistence = 1; Daniel@0: % Set alpha to lie in [0, 1) Daniel@0: alpha = max(0, options(17)); Daniel@0: alpha = min(1, alpha); Daniel@0: salpha = sqrt(1-alpha*alpha); Daniel@0: else Daniel@0: persistence = 0; Daniel@0: end Daniel@0: L = max(1, options(7)); % At least one step in leap-frogging Daniel@0: if options(14) > 0 Daniel@0: nsamples = options(14); Daniel@0: else Daniel@0: nsamples = 100; % Default Daniel@0: end Daniel@0: if options(15) >= 0 Daniel@0: nomit = options(15); Daniel@0: else Daniel@0: nomit = 0; Daniel@0: end Daniel@0: if options(18) > 0 Daniel@0: step_size = options(18); % Step size. Daniel@0: else Daniel@0: step_size = 1/L; % Default Daniel@0: end Daniel@0: x = x(:)'; % Force x to be a row vector Daniel@0: nparams = length(x); Daniel@0: Daniel@0: % Set up strings for evaluating potential function and its gradient. Daniel@0: f = fcnchk(f, length(varargin)); Daniel@0: gradf = fcnchk(gradf, length(varargin)); Daniel@0: Daniel@0: % Check the gradient evaluation. Daniel@0: if (options(9)) Daniel@0: % Check gradients Daniel@0: feval('gradchek', x, f, gradf, varargin{:}); Daniel@0: end Daniel@0: Daniel@0: samples = zeros(nsamples, nparams); % Matrix of returned samples. Daniel@0: if nargout >= 2 Daniel@0: en_save = 1; Daniel@0: energies = zeros(nsamples, 1); Daniel@0: else Daniel@0: en_save = 0; Daniel@0: end Daniel@0: if nargout >= 3 Daniel@0: diagnostics = 1; Daniel@0: diagn_pos = zeros(nsamples, nparams); Daniel@0: diagn_mom = zeros(nsamples, nparams); Daniel@0: diagn_acc = zeros(nsamples, 1); Daniel@0: else Daniel@0: diagnostics = 0; Daniel@0: end Daniel@0: Daniel@0: n = - nomit + 1; Daniel@0: Eold = feval(f, x, varargin{:}); % Evaluate starting energy. Daniel@0: nreject = 0; Daniel@0: if (~persistence | isempty(HMC_MOM)) Daniel@0: p = randn(1, nparams); % Initialise momenta at random Daniel@0: else Daniel@0: p = HMC_MOM; % Initialise momenta from stored state Daniel@0: end Daniel@0: lambda = 1; Daniel@0: Daniel@0: % Main loop. Daniel@0: while n <= nsamples Daniel@0: Daniel@0: xold = x; % Store starting position. Daniel@0: pold = p; % Store starting momenta Daniel@0: Hold = Eold + 0.5*(p*p'); % Recalculate Hamiltonian as momenta have changed Daniel@0: Daniel@0: if ~persistence Daniel@0: % Choose a direction at random Daniel@0: if (rand < 0.5) Daniel@0: lambda = -1; Daniel@0: else Daniel@0: lambda = 1; Daniel@0: end Daniel@0: end Daniel@0: % Perturb step length. Daniel@0: epsilon = lambda*step_size*(1.0 + 0.1*randn(1)); Daniel@0: Daniel@0: % First half-step of leapfrog. Daniel@0: p = p - 0.5*epsilon*feval(gradf, x, varargin{:}); Daniel@0: x = x + epsilon*p; Daniel@0: Daniel@0: % Full leapfrog steps. Daniel@0: for m = 1 : L - 1 Daniel@0: p = p - epsilon*feval(gradf, x, varargin{:}); Daniel@0: x = x + epsilon*p; Daniel@0: end Daniel@0: Daniel@0: % Final half-step of leapfrog. Daniel@0: p = p - 0.5*epsilon*feval(gradf, x, varargin{:}); Daniel@0: Daniel@0: % Now apply Metropolis algorithm. Daniel@0: Enew = feval(f, x, varargin{:}); % Evaluate new energy. Daniel@0: p = -p; % Negate momentum Daniel@0: Hnew = Enew + 0.5*p*p'; % Evaluate new Hamiltonian. Daniel@0: a = exp(Hold - Hnew); % Acceptance threshold. Daniel@0: if (diagnostics & n > 0) Daniel@0: diagn_pos(n,:) = x; Daniel@0: diagn_mom(n,:) = p; Daniel@0: diagn_acc(n,:) = a; Daniel@0: end Daniel@0: if (display > 1) Daniel@0: fprintf(1, 'New position is\n'); Daniel@0: disp(x); Daniel@0: end Daniel@0: Daniel@0: if a > rand(1) % Accept the new state. Daniel@0: Eold = Enew; % Update energy Daniel@0: if (display > 0) Daniel@0: fprintf(1, 'Finished step %4d Threshold: %g\n', n, a); Daniel@0: end Daniel@0: else % Reject the new state. Daniel@0: if n > 0 Daniel@0: nreject = nreject + 1; Daniel@0: end Daniel@0: x = xold; % Reset position Daniel@0: p = pold; % Reset momenta Daniel@0: if (display > 0) Daniel@0: fprintf(1, ' Sample rejected %4d. Threshold: %g\n', n, a); Daniel@0: end Daniel@0: end Daniel@0: if n > 0 Daniel@0: samples(n,:) = x; % Store sample. Daniel@0: if en_save Daniel@0: energies(n) = Eold; % Store energy. Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: % Set momenta for next iteration Daniel@0: if persistence Daniel@0: p = -p; Daniel@0: % Adjust momenta by a small random amount. Daniel@0: p = alpha.*p + salpha.*randn(1, nparams); Daniel@0: else Daniel@0: p = randn(1, nparams); % Replace all momenta. Daniel@0: end Daniel@0: Daniel@0: n = n + 1; Daniel@0: end Daniel@0: Daniel@0: if (display > 0) Daniel@0: fprintf(1, '\nFraction of samples rejected: %g\n', ... Daniel@0: nreject/(nsamples)); Daniel@0: end Daniel@0: if diagnostics Daniel@0: diagn.pos = diagn_pos; Daniel@0: diagn.mom = diagn_mom; Daniel@0: diagn.acc = diagn_acc; Daniel@0: end Daniel@0: % Store final momentum value in global so that it can be retrieved later Daniel@0: HMC_MOM = p; Daniel@0: return Daniel@0: Daniel@0: % Return complete state of sampler (including momentum) Daniel@0: function state = get_state(f) Daniel@0: Daniel@0: global HMC_MOM Daniel@0: state.randstate = rand('state'); Daniel@0: state.randnstate = randn('state'); Daniel@0: state.mom = HMC_MOM; Daniel@0: return Daniel@0: Daniel@0: % Set complete state of sampler (including momentum) or just set randn Daniel@0: % and rand with integer argument. Daniel@0: function set_state(f, x) Daniel@0: Daniel@0: global HMC_MOM Daniel@0: if isnumeric(x) Daniel@0: rand('state', x); Daniel@0: randn('state', x); Daniel@0: HMC_MOM = []; Daniel@0: else Daniel@0: if ~isstruct(x) Daniel@0: error('Second argument to hmc must be number or state structure'); Daniel@0: end Daniel@0: if (~isfield(x, 'randstate') | ~isfield(x, 'randnstate') ... Daniel@0: | ~isfield(x, 'mom')) Daniel@0: error('Second argument to hmc must contain correct fields') Daniel@0: end Daniel@0: rand('state', x.randstate); Daniel@0: randn('state', x.randnstate); Daniel@0: HMC_MOM = x.mom; Daniel@0: end Daniel@0: return