I tried it just now, but it complained about dimensions.
I adapted the code to this:
import arviz as az
import numpy as np
import matplotlib.pyplot as plt
import math
size = 1000
x = np.linspace(0, 2*math.pi, size)
y = np.sin(x)
# Plotting this way works as expected
plt.plot(x, y)
plt.show()
# But this gives an error
dt = az.from_dict({
"posterior": {
"some_var_name": y,
}
})
# So I'm unable to do this
pc = az.plot_dist(dt)
pc.show()
I’m not saying it should work, for example, dt has no information about x. But the code shows the gist of what I want to achieve. I want to plot x and y arrays similar to this, alongside a trace created by pm.sample().
I add the real code in case the above is not enough for debugging (I tried making it as short as possible):
# Based on https://www.pymc.io/projects/docs/en/stable/learn/core_notebooks/GLM_linear.html
import arviz as az
import numpy as np
inv = np.linalg.inv
import pymc as pm
from pymc import Model, Normal
import scipy
import matplotlib.pyplot as plt
RANDOM_SEED = 8927
rng = np.random.default_rng(RANDOM_SEED)
# Generating data
size = 1000
intercept_true = 1
slope_true = 2
x = np.linspace(0, 1, size)
# Line from which we generate data
regression_line = intercept_true + slope_true * x
# Simulate data by adding noise
sigma_epsilon = 0.5
epsilon = rng.normal(0, sigma_epsilon, size)
y = regression_line + epsilon
# Prior parameters
mu_0 = np.array([[0],[0]])
Sigma_0 = np.diag(np.array([10, 10]))
# Model definition
with Model() as model:
# Priors
sigma = 0.5
intercept = Normal("intercept", mu_0[0], Sigma_0[0][0])
slope = Normal("slope", mu_0[1], Sigma_0[1][1])
likelihood = Normal("y", mu=intercept + slope * x, sigma=sigma, observed=y)
# Sampling a trace using NUTS
with model:
trace = pm.sample()
# Analytical posterior
# https://statproofbook.github.io/P/blrkc-post.html, eqs 1--4
X = np.matrix([np.ones(size), x]).T # size×2
beta = np.matrix([intercept_true, slope_true]).T # 2×1
Sigma = np.identity(size) * sigma_epsilon
Sigma_n = inv(
X.T @ inv(Sigma) @ X + inv(Sigma_0)
)
mu_n = Sigma_n @ (X.T @ inv(Sigma) @ np.matrix(y).T + inv(Sigma_0) @ mu_0)
# We need a bigger range than [0, 1] as intercept and slope are 1 and 2 respectively
x1 = np.linspace(0,3,size)
analytical = scipy.stats.multivariate_normal(np.ravel(mu_n), Sigma_n)
intercept_analytical = analytical.marginal(0).pdf(x1)
slope_analytical = analytical.marginal(1).pdf(x1)
# We can plot the analytical posteriors normally using matplotlib's `plot`
plt.subplot(1,2,1)
plt.plot(x1, intercept_analytical)
plt.subplot(1,2,2)
plt.plot(x1, slope_analytical)
plt.show()
# Gives error
reference = az.from_dict({
"posterior": {
"intercept": intercept_analytical,
"slope": slope_analytical
}
})
# Things I want to do
# Plotting
pc = az.plot_dist(
{
"Reference": reference,
"NUTS": trace,
},
)
pc.add_legend("model")
pc.show()
# Measuring Wasserstein distance
d = az.wasserstein(trace, reference)
intercept_analytical and slope_analytical are like y from the previous example, arrays of values corresponding to an array of x-values. This is in contrast to trace.posterior.intercept, holding an array of samples.