Here’s what I am trying to do:
I am working on a toy problem of fitting a piecewise-linear function to some data. I have defined changepoints as deterministic locations where the slope of the line can change. What I want is to infer the values of the slopes at each of these changepoints. I have managed to run this using another method as outlined here. I am trying out an alternative approach here wherein I define the piecewise linear function f based on the stochastic slope values (delta).
I understand that I am mixing numpy arrays and tensors in a way that is not allowed. (Although, I don’t quite understand the real issue here). I was hoping someone could point me in the right direction to implementing this approach.
import theano
import numpy as np
import pymc3 as pm
from pymc3 import *
aa = pm.Model()
with aa:
delta = pm.Laplace('delta', 0, 5, shape=(20,))
x = np.linspace(0, 10, 200)
changepoints = np.linspace(0, 10, 20)
def f(t):
arr = [*map(lambda x: int(x), np.array(changepoints) < t)]
slope = pm.math.dot(arr, delta)
gamma = -changepoints * delta
intercept = pm.math.dot(arr, gamma)
return slope*t + intercept
y_obs = np.array(map(f, x))
sigma = HalfCauchy("sigma", beta=10, testval=1.0)
likelihood = Normal("y_obs", mu=y_obs, sigma=sigma, observed=y)