# Spline monotonicity

107 views

### Julian King

May 26, 2017, 5:58:00 AM5/26/17
to Stan users mailing list
Hello Stan repository of knowledge.

Is there any easy way to guarantee that a fitted spline is monotonic?

Perhaps manually calculate the derivative at all of the sets of points (ie using calculus, by hand) and then include a constraint within Stan?

I realise Stan doesn't like hard constraints, so maybe you would need to use a logistic function instead of a Heaviside constraint...

Suggestions appreciated!

Thanks!
Julian

### Bob Carpenter

May 28, 2017, 3:11:02 PM5/28/17
What is the actual constraint you're trying to enforce?

- Bob

P.S. We've moved to: http://discourse.mc-stan.org and will
be closing this list in a couple weeks.
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

### Mike Lawrence

May 28, 2017, 6:29:03 PM5/28/17
If you want a monotonic function, I'd model a smooth on the derivative as positive-constrained then use cumulative_sum to compute what a given sample for the derivative implies for the function. Something like:

data{
int n ;
vector[n] y ; #scaled to mean=0,sd=1
}
parameters{
real mu ; #intercept
real sigma ; #scale of measurement noise
... # whatever parameters you need for your smooth here
}
transformed parameters{
vector[n] f ; #smoothed values
vector<lower=0>[n] df ; #1st derivative of f
df = ... ; #construct your smooth here
f = cumulative_sum(df)
}
model{
mu ~ normal(0,1) ; prior on intercept
sigma ~ weibul(2,1) ; prior on noise scale
... # priors for your smoothing parameters
y ~ normal( mu + f , sigma ) ;
}

Note that while you mention splines, if you have less than a few hundred unique locations sampled in the domain you're  smoothing across, I'd use a Gaussian Process instead.

Mike

--
Mike Lawrence
Department of Psychology & Neuroscience
Dalhousie University

~ Certainty is (possibly) folly ~

On Sun, May 28, 2017 at 4:09 PM, Bob Carpenter wrote:
What is the actual constraint you're trying to enforce?

- Bob

P.S.  We've moved to:  http://discourse.mc-stan.org and will
be closing this list in a couple weeks.

> On May 26, 2017, at 5:58 AM, Julian King <jking...@gmail.com> wrote:
>
> Hello Stan repository of knowledge.
>
> Is there any easy way to guarantee that a fitted spline is monotonic?
>
> Perhaps manually calculate the derivative at all of the sets of points (ie using calculus, by hand) and then include a constraint within Stan?
>
> I realise Stan doesn't like hard constraints, so maybe you would need to use a logistic function instead of a Heaviside constraint...
>
> Suggestions appreciated!
>
> Thanks!
> Julian
>
>
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+unsubscribe@googlegroups.com.

> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+unsubscribe@googlegroups.com.

### Mike Lawrence

May 28, 2017, 7:40:09 PM5/28/17
Oops, you'll have to put "vector<lower=0>[n] df" in the parameters section, not transformed parameters

> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.

> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.

To post to this group, send email to stan-...@googlegroups.com.

--

### Julian King

May 28, 2017, 8:20:50 PM5/28/17
to Stan users mailing list
Thanks for the heads up re relocation.

I have a complex multivariate function that is built up from the product of univariate elements. Some of the univariate elements are currently either linear (y propto x) or power functions (y propro x^(-a), a>0). I want to try to explore more realistic approximations, particularly for the reciprocal-like functions.

From the problem specification, however, I know that these functions must be monotonic for the solution to be plausible. So I need a non-parametric approximation that is also guaranteed to be monotonic.

Julian

### Julian King

May 28, 2017, 8:23:03 PM5/28/17
to Stan users mailing list
Thanks for the suggestion. This might work, but is not as convenient as it could be because I need to pre-calculate this for all the values of x I am interested in. The x values are not uniformly spaced either, so I would need something like f(x_2) = f(x_1) + (x_2-x_1)*f''(x_2).

I really would like this to be as a function, so I can call f(x) for arbitrary x. (I guess you could do this with the routine below, a lookup and linear interpolation).
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.

> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.