#NEWLINE# def sampler_on_nd_gaussian( sampler_cls , burnin , n_samples , dim = 10 ) : #NEWLINE# #INDENT# batchsize = 3 #NEWLINE# rng = numpy . random . RandomState ( 123 ) #NEWLINE# mu = numpy . array ( ( rng . rand ( dim ) * 10 ) , dtype = theano . config . floatX ) #NEWLINE# cov = numpy . array ( rng . rand ( dim , dim ) , dtype = theano . config . floatX ) #NEWLINE# cov = ( ( cov + _samples . T ) / 2.0 ) #NEWLINE# cov [ ( numpy . arange ( dim ) , numpy . arange ( dim ) ) ] = 1.0 #NEWLINE# cov_inv = numpy . linalg . inv ( cov ) #NEWLINE# def gaussian_energy( x ) : #NEWLINE# #INDENT# return ( 0.5 * ( theano . tensor . dot ( ( x - mu ) , cov_inv ) * ( x - mu ) ) . sum ( axis = 1 ) ) #NEWLINE# #UNINDENT# position = rng . randn ( batchsize , dim ) . astype ( theano . config . floatX ) #NEWLINE# position = theano . shared ( position ) #NEWLINE# sampler = sampler_cls ( position , gaussian_energy , initial_stepsize = 0.001 , stepsize_max = 0.5 ) #NEWLINE# garbage = [ sampler . draw ( ) for r in range ( burnin ) ] #NEWLINE# _samples = numpy . asarray ( [ sampler . draw ( ) for r in range ( n_samples ) ] ) #NEWLINE# samples = _samples . T . reshape ( dim , ( - 1 ) ) . T #NEWLINE# print ( '****** TARGET VALUES ******' ) #NEWLINE# print ( 'target mean:' , mu ) #NEWLINE# print ( 'target cov:\n' , cov ) #NEWLINE# print ( '****** EMPIRICAL MEAN/COV USING HMC ******' ) #NEWLINE# print ( 'empirical mean: ' , samples . mean ( axis = 0 ) ) #NEWLINE# print ( 'empirical_cov:\n' , numpy . cov ( samples . T ) ) #NEWLINE# print ( '****** HMC INTERNALS ******' ) #NEWLINE# print ( 'final stepsize' , sampler . stepsize . get_value ( ) ) #NEWLINE# print ( 'final acceptance_rate' , sampler . avg_acceptance_rate . get_value ( ) ) #NEWLINE# return sampler