0

I am trying to implement parts of Facebook's prophet with some help from this example.

https://github.com/luke14free/pm-prophet/blob/master/pmprophet/model.py

This goes well :), but I am having some problems with the dot product I don't understand. Note that I am implementing the linear trends.

ds = pd.to_datetime(df['dagindex'], format='%d-%m-%y')


m = pm.Model()
changepoint_prior_scale = 0.05
n_changepoints = 25
changepoints = pd.date_range(
    start=pd.to_datetime(ds.min()),
    end=pd.to_datetime(ds.max()),
    periods=n_changepoints + 2
)[1: -1]

with m:

    # priors
    sigma = pm.HalfCauchy('sigma', 10, testval=1)
    #trend
    growth = pm.Normal('growth', 0, 10)

    prior_changepoints = pm.Laplace('changepoints', 0, changepoint_prior_scale, shape=len(changepoints))

    y = np.zeros(len(df))

    # indexes x_i for the changepoints. 
    s = [np.abs((ds - i).values).argmin() for i in changepoints]

    g = growth
    x = np.arange(len(ds))
    # delta
    d = prior_changepoints

    regression = x * g

    base_piecewise_regression = []

    for i in s:
        local_x = x.copy()[:-i]
        local_x = np.concatenate([np.zeros(i), local_x])
        base_piecewise_regression.append(local_x)

    piecewise_regression = np.array(base_piecewise_regression)

#  this dot product doesn't work?
    piecewise_regression = pm.math.dot(theano.shared(piecewise_regression).T, d)

#  If I comment out this line and use that one as dot product. It works fine
#     piecewise_regression = (piecewise_regression.T * d[None, :]).sum(axis=-1)
    regression += piecewise_regression

    y += regression

    obs = pm.Normal('y',
                   mu=(y - df.gebruikers.mean()) / df.gebruikers.std(),
                   sd=sigma,
                   observed=(df.gebruikers - df.gebruikers.mean()) / df.gebruikers.std())

    start = pm.find_MAP(maxeval=10000)
    trace = pm.sample(500, step=pm.NUTS(), start=start)

If I run the snippet above with

piecewise_regression = (piecewise_regression.T * d[None, :]).sum(axis=-1)

the model works as expected. However I cannot get it to work with a dot product. The NUTS sampler doesn't sample at all.

piecewise_regression = pm.math.dot(theano.shared(piecewise_regression).T, d)

EDIT

Ive got a minimal working example

The problem still occurs with theano.shared. I’ve got a minimal working example:

np.random.seed(5)

n_changepoints = 10
t = np.arange(1000)
s = np.sort(np.random.choice(t, size=n_changepoints, replace=False))
a = (t[:, None] > s) * 1

real_delta = np.random.normal(size=n_changepoints)
y = np.dot(a, real_delta) * t

with pm.Model():
    sigma = pm.HalfCauchy('sigma', 10, testval=1)
    delta = pm.Laplace('delta', 0, 0.05, shape=n_changepoints)
    g = tt.dot(a, delta) * t
    obs = pm.Normal('obs',
                   mu=(g - y.mean()) / y.std(),
                   sd=sigma,
                   observed=(y - y.mean()) / y.std())
    trace = pm.sample(500)

It seems to have something to do with the size of matrix a. NUTS doesnt’t sample if I start with

t = np.arange(1000)

however the example above does sample when I reduce the size of t to:

t = np.arange(100)

ritchie46
  • 10,405
  • 1
  • 24
  • 43
  • How exactly is it failing to sample? Initialization failure? Chain encountering non-finite value? – merv Oct 08 '18 at 03:40
  • It doesn't provide any error message. It just doesn't sample (the progress bar hangs on 0). – ritchie46 Oct 09 '18 at 11:19

0 Answers0