Please can somebody help me understand the reason for the error that I am getting:
@tf.function
def utilities(x, betas, errors):
"""
`x * betas + errors` with broadcasting.
"""
x = tf.cast(x, dtype=tf.float32)
return tf.transpose(x) * betas + errors
k = 10
sigma_beta = 5.
sigma_error = 1.
def alt_pooled_model(X_train):
return tfd.JointDistributionSequential([
tfd.HalfCauchy(loc=0., scale=sigma_beta, name="sigma_beta"),
tfd.HalfCauchy(loc=0., scale=sigma_error, name="sigma_error"),
tfd.Normal(loc=tf.zeros(k), scale=sigma_beta, name="beta"),
tfd.Gumbel(loc=0., scale=sigma_error, name="error"),
lambda beta, error: tfd.Deterministic(
tf.math.argmax(
tfd.Multinomial(
total_count=1,
logits=utilities(X_train, beta[..., tf.newaxis], error[..., tf.newaxis]),
#name="MNL"
).sample(), axis=-1
),
name="best_choices"
),
])
def target_log_prob(sigma_beta, sigma_error, beta, error):
return alt_pooled_model(X_train).log_prob(sigma_beta=sigma_beta, sigma_error=sigma_error, beta=beta, error=error,
best_choices=best_choices)
# Use NUTS for inference
hmc = tfp.mcmc.NoUTurnSampler(
target_log_prob_fn=target_log_prob,
step_size=.01)
# Unconstrain the scale parameters, which must be positive
hmc = tfp.mcmc.TransformedTransitionKernel(
inner_kernel=hmc,
bijector=[
tfp.bijectors.Identity(), # sigma_beta
tfp.bijectors.Identity(), # sigma_error
tfp.bijectors.Identity(), # beta
tfp.bijectors.Identity(), # error
])
# Adapt the step size for 100 steps before burnin and main sampling
hmc = tfp.mcmc.DualAveragingStepSizeAdaptation(
inner_kernel=hmc,
num_adaptation_steps=100,
target_accept_prob=.75)
# Initialize 10 chains using samples from the prior
joint_sample = alt_pooled_model(X_train).sample(10)
initial_state = [
joint_sample[0],
joint_sample[1],
joint_sample[2],
joint_sample[3],
]
# Compile with tf.function and XLA for improved runtime performance
@tf.function(autograph=False, experimental_compile=True)
def run():
return tfp.mcmc.sample_chain(
num_results=500,
current_state=initial_state,
kernel=hmc,
num_burnin_steps=200,
trace_fn=lambda _, kr: kr)
samples, traces = run()
print('R-hat diagnostics: ', tfp.mcmc.potential_scale_reduction(samples))
ValueError: Inconsistent names: component with name "beta" was referred to by a different name "error".
Please help