def benchmark_eight_schools_hmc(<EOL>num_results=int(<NUM_LIT>),<EOL>num_burnin_steps=int(<NUM_LIT>),<EOL>num_leapfrog_steps=<NUM_LIT:3>,<EOL>step_size=<NUM_LIT>):
num_schools = <NUM_LIT:8><EOL>treatment_effects = tf.constant(<EOL>[<NUM_LIT>, <NUM_LIT:8>, -<NUM_LIT:3>, <NUM_LIT:7>, -<NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT>, <NUM_LIT:12>],<EOL>dtype=np.float32,<EOL>name='<STR_LIT>')<EOL>treatment_stddevs = tf.constant(<EOL>[<NUM_LIT:15>, <NUM_LIT:10>, <NUM_LIT:16>, <NUM_LIT:11>, <NUM_LIT:9>, <NUM_LIT:11>, <NUM_LIT:10>, <NUM_LIT>],<EOL>dtype=np.float32,<EOL>name='<STR_LIT>')<EOL>def unnormalized_posterior_log_prob(<EOL>avg_effect, avg_stddev, school_effects_standard):<EOL><INDENT>"""<STR_LIT>"""<EOL>return eight_schools_joint_log_prob(<EOL>treatment_effects, treatment_stddevs,<EOL>avg_effect, avg_stddev, school_effects_standard)<EOL><DEDENT>if tf.executing_eagerly():<EOL><INDENT>sample_chain = tf.function(tfp.mcmc.sample_chain)<EOL><DEDENT>else:<EOL><INDENT>sample_chain = tfp.mcmc.sample_chain<EOL><DEDENT>def computation():<EOL><INDENT>"""<STR_LIT>"""<EOL>_, kernel_results = sample_chain(<EOL>num_results=num_results,<EOL>num_burnin_steps=num_burnin_steps,<EOL>current_state=(<EOL>tf.zeros([], name='<STR_LIT>'),<EOL>tf.zeros([], name='<STR_LIT>'),<EOL>tf.ones([num_schools], name='<STR_LIT>'),<EOL>),<EOL>kernel=tfp.mcmc.HamiltonianMonteCarlo(<EOL>target_log_prob_fn=unnormalized_posterior_log_prob,<EOL>step_size=step_size,<EOL>num_leapfrog_steps=num_leapfrog_steps))<EOL>return kernel_results.is_accepted<EOL><DEDENT>is_accepted_tensor = computation()<EOL>if not tf.executing_eagerly():<EOL><INDENT>session = tf.compat.v1.Session()<EOL>session.run(is_accepted_tensor)<EOL><DEDENT>start_time = time.time()<EOL>if tf.executing_eagerly():<EOL><INDENT>is_accepted = computation()<EOL><DEDENT>else:<EOL><INDENT>is_accepted = session.run(is_accepted_tensor)<EOL><DEDENT>wall_time = time.time() - start_time<EOL>num_accepted = np.sum(is_accepted)<EOL>acceptance_rate = np.float32(num_accepted) / np.float32(num_results)<EOL>return dict(<EOL>iters=(num_results + num_burnin_steps) * num_leapfrog_steps,<EOL>extras={'<STR_LIT>': acceptance_rate},<EOL>wall_time=wall_time)<EOL>
Runs HMC on the eight-schools unnormalized posterior.