Daniel@0: function [samples, energies, diagn] = metrop(f, x, options, gradf, varargin) Daniel@0: %METROP Markov Chain Monte Carlo sampling with Metropolis algorithm. Daniel@0: % Daniel@0: % Description Daniel@0: % SAMPLES = METROP(F, X, OPTIONS) uses the Metropolis algorithm to Daniel@0: % sample from the distribution P ~ EXP(-F), where F is the first Daniel@0: % argument to METROP. The Markov chain starts at the point X and each Daniel@0: % candidate state is picked from a Gaussian proposal distribution and Daniel@0: % accepted or rejected according to the Metropolis criterion. Daniel@0: % Daniel@0: % SAMPLES = METROP(F, X, OPTIONS, [], P1, P2, ...) allows additional Daniel@0: % arguments to be passed to F(). The fourth argument is ignored, but Daniel@0: % is included for compatibility with HMC and the optimisers. Daniel@0: % Daniel@0: % [SAMPLES, ENERGIES, DIAGN] = METROP(F, X, OPTIONS) also returns a log Daniel@0: % 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 and acceptance threshold) for each step of the Daniel@0: % chain in DIAGN.POS and DIAGN.ACC respectively. All candidate states Daniel@0: % (including rejected ones) are stored in DIAGN.POS. Daniel@0: % Daniel@0: % S = METROP('STATE') returns a state structure that contains the state Daniel@0: % of the two random number generators RAND and RANDN. These are Daniel@0: % contained in fields randstate, randnstate. Daniel@0: % Daniel@0: % METROP('STATE', S) resets the state to S. If S is an integer, then Daniel@0: % it is passed to RAND and RANDN. If S is a structure returned by Daniel@0: % METROP('STATE') then it resets the generator to exactly the same Daniel@0: % 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(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(18) is the variance of the proposal distribution; default 1. Daniel@0: % Daniel@0: % See also Daniel@0: % HMC Daniel@0: % Daniel@0: Daniel@0: % Copyright (c) Ian T Nabney (1996-2001) Daniel@0: Daniel@0: if nargin <= 2 Daniel@0: if ~strcmp(f, 'state') Daniel@0: error('Unknown argument to metrop'); Daniel@0: end Daniel@0: switch nargin Daniel@0: case 1 Daniel@0: % Return state of sampler Daniel@0: samples = get_state(f); % Function defined in this module Daniel@0: return; Daniel@0: case 2 Daniel@0: % Set the state of the sampler Daniel@0: set_state(f, x); % Function defined in this module Daniel@0: return; Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: if 0 Daniel@0: seed = 42; Daniel@0: randn('state', seed); Daniel@0: rand('state', seed) Daniel@0: end Daniel@0: Daniel@0: display = options(1); Daniel@0: if options(14) > 0 Daniel@0: nsamples = options(14); Daniel@0: else Daniel@0: nsamples = 100; 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.0 Daniel@0: std_dev = sqrt(options(18)); Daniel@0: else Daniel@0: std_dev = 1.0; % default Daniel@0: end Daniel@0: nparams = length(x); Daniel@0: Daniel@0: % Set up string for evaluating potential function. Daniel@0: f = fcnchk(f, length(varargin)); 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_acc = zeros(nsamples, 1); Daniel@0: else Daniel@0: diagnostics = 0; Daniel@0: end Daniel@0: Daniel@0: % Main loop. Daniel@0: n = - nomit + 1; Daniel@0: Eold = feval(f, x, varargin{:}); % Evaluate starting energy. Daniel@0: nreject = 0; % Initialise count of rejected states. Daniel@0: while n <= nsamples Daniel@0: Daniel@0: xold = x; Daniel@0: % Sample a new point from the proposal distribution Daniel@0: x = xold + randn(1, nparams)*std_dev; Daniel@0: %fprintf('netlab propose: xold = %5.3f,%5.3f, xnew = %5.3f,%5.3f\n',... Daniel@0: % xold(1), xold(2), x(1), x(2)); Daniel@0: Daniel@0: % Now apply Metropolis algorithm. Daniel@0: Enew = feval(f, x, varargin{:}); % Evaluate new energy. Daniel@0: a = exp(Eold - Enew); % Acceptance threshold. Daniel@0: if (diagnostics & n > 0) Daniel@0: diagn_pos(n,:) = x; 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: r = rand(1); Daniel@0: %fprintf('netlab: n=%d, a=%f/%f=%5.3f (%5.3f), r=%5.3f\n',... Daniel@0: % n, exp(-Enew), exp(-Eold), a, exp(-Enew)/exp(-Eold), r); Daniel@0: if a > r % Accept the new state. Daniel@0: Eold = Enew; 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: 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: 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: Daniel@0: if diagnostics Daniel@0: diagn.pos = diagn_pos; Daniel@0: diagn.acc = diagn_acc; Daniel@0: end Daniel@0: Daniel@0: % Return complete state of the sampler. Daniel@0: function state = get_state(f) Daniel@0: Daniel@0: state.randstate = rand('state'); Daniel@0: state.randnstate = randn('state'); Daniel@0: return Daniel@0: Daniel@0: % Set state of sampler, either from full state, or with an integer Daniel@0: function set_state(f, x) Daniel@0: Daniel@0: if isnumeric(x) Daniel@0: rand('state', x); Daniel@0: randn('state', x); Daniel@0: else Daniel@0: if ~isstruct(x) Daniel@0: error('Second argument to metrop must be number or state structure'); Daniel@0: end Daniel@0: if (~isfield(x, 'randstate') | ~isfield(x, 'randnstate')) Daniel@0: error('Second argument to metrop must contain correct fields') Daniel@0: end Daniel@0: rand('state', x.randstate); Daniel@0: randn('state', x.randnstate); Daniel@0: end Daniel@0: return