Fitting a local level model using the Tensorflow Probability

140 Views Asked by At

I have simulated time-series data with pre-defined parameters below. I want to build a local linear model that approximates the true mean at 5.

# Set true parameters
mu = 5
sigma = 0.5
size = 200

# Generate data
samples = np.random.normal(mu, sigma, size=size)

The code below is my TF code that constructs a local level model and trains it using the surrogate posterior and Adam optimizer at 200 steps.

# Instantiate a local level model
sts_model = tfp.sts.LocalLevel(observed_time_series=samples)

# Build the variational surrogate posteriors `qs`.
surrogate_posterior = tfp.sts.build_factored_surrogate_posterior(
    model=sts_model)

loss_curve = tfp.vi.fit_surrogate_posterior(
    target_log_prob_fn=sts_model.joint_log_prob(samples),
    surrogate_posterior=surrogate_posterior,
    optimizer=tf.optimizers.Adam(learning_rate=0.1),
    num_steps = 200
)

# Draw samples from the variational posterior.
sample_obs = surrogate_posterior.sample(1000)

print("Inferred parameters:")
for param in sts_model.parameters:
  print("{}: {} +- {}".format(param.name,
                              np.mean(sample_obs[param.name], axis=0),
                              np.std(sample_obs[param.name], axis=0)))

The model does achieve convergence (as the loss curve approaches 0). However, when sampling from the posterior distribution, the mean is at 0.75, quite far from the true value at 5. What could be the issue with my code?

Thank you in advance!

0

There are 0 best solutions below