Getting "mass matrix contains zero on the diagonal" when trying to fit a piecewise linear model

Do the piecewise functions have to connect? This formulation should work much better, it’s what is used within the facebook prophet model. Here I’m using it to generate an example of one of these functions:

# x axis
x = np.linspace(0, 10, 100)

# locations on axis of the changepoints
cps = np.array([2, 4, 7])

# helper function
def cp_design_mat(x, cps):
    return (0.5 * (1.0 + tt.sgn(tt.tile(x[:,None], (1,len(cps))) - cps)))

A = cp_design_mat(x, cps)

# the following are unknown parameters that you can estimate by giving 
# them priors and then sampling with NUTS
k = 0.0  # initial slope
m = 0.3 # offset
delta = np.array([0.5, -0.4, -0.6]) # slope parameters

# generate the function at x values
y = (k + tt.dot(A, delta))*x + (m + tt.dot(A, -cps * delta))

plt.plot(x, y)

1 Like