Time series forecasting with random data points

That’s an interesting problem. I’ve done something a little like that before. The code below fits an AR1 model where the observed data points are distributions, not points. The basic idea is to avoid using the observed argument and use the distributions as priors on latent variables.

import pymc3 as pm
import numpy as np
import matplotlib.pyplot as plt

n = 20

random_sds = np.exp(np.random.randn(20))
random_means = np.random.randn(20)

with pm.Model() as model:
    k = pm.Uniform('k')
    tau = pm.Gamma('tau',alpha=0.1,beta=0.1)
    y = pm.AR1('y',k=k, tau_e=tau, shape=n)
    x = pm.Normal('x', mu=y-random_means, sd=random_sds, shape=n)
    trace = pm.sample(cores=1,chains=1)
    
plt.plot(trace['y'].mean(axis=0))
plt.plot(random_means)
2 Likes