Hi,

I was hoping to get some advice on how to code an unusual piecewise function in stan.

I have a scalar predictor variable x that is predicting a vector variable \vec{y}. It does so in a piecewise way with an unknown number of changepoints x_1,x_2... x_n. This is linked to a piecewise vector constant function \vec{v}(x)=\vec{v_1},\vec{v_2},...\vec{v_{n-1}}. Additionally there is a scalar part to the function that looks like f(x) = 1 - Betacdf(x,\alpha,\beta) .

For x_n<x<x_{n+1} we get the formula for y

\vec{y}(x)=\vec{v_n}[f(x)-f(x_{n+1})]+\vec{y}(x_{n+1})

and \vec{y}(1) = \vec{0}

If there were 0 changepoints, then we would simply get the relationship.

\vec{y}(x)=\vec{v}f(x)

For graphical demonstration, I have an image of y (in 2 dimensional space) evaluated at a set of x values in a case with 2 change points. Note that the vectors come out of the endpoints of the previous vector.

Does anybody have any advice for creating a piecewise function like this in Stan, particularly for an unknown number of changepoints? I think I would need a parameter for an unknown number of changepoints (perhaps with geometric prior), a set of \vec{v} parameters, an \alpha and \beta parameter, and values of x (data) as inputs.