Skip to content

Commit

Permalink
update linear interpolation signature
Browse files Browse the repository at this point in the history
  • Loading branch information
Yi Zhang committed Nov 14, 2024
1 parent 6fb6f1a commit f8b1698
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
8 changes: 4 additions & 4 deletions docs/torsten_users_guide.tex
Original file line number Diff line number Diff line change
Expand Up @@ -1082,10 +1082,10 @@ \section{Piecewise linear interpolation}
\label{sec:org5f1a4b1}
\index{linear interpolation}
\begin{minted}[breaklines=true,fontsize=\footnotesize,breakanywhere=true]{stan}
real linear_interpolation(real xout, array[] real x, array[] real y)
real pmx_ln_interpolate(real xout, array[] real x, array[] real y)
\end{minted}
\begin{minted}[breaklines=true,fontsize=\footnotesize,breakanywhere=true]{stan}
array[] real linear_interpolation(array[] real xout, array[] real x, array[] real y)
array[] real pmx_ln_interpolate(array[] real xout, array[] real x, array[] real y)
\end{minted}
Torsten also provides function \texttt{linear\_interpolation} for piecewise linear interpolation over a
set of x, y pairs. It returns the values of a piecewise linear
Expand Down Expand Up @@ -1872,7 +1872,7 @@ \section{Linear intepolation}
for(i in 2:(nx-1))
x[i] = x[i-1] + xSimplex[i-1] * (xmax - xmin);

yHat = linear_interpolation(xObs, x, y);
yHat = pmx_ln_interpolate(xObs, x, y);
}

model{
Expand All @@ -1885,7 +1885,7 @@ \section{Linear intepolation}
real yHatPred[nPred];
real yPred[nPred];

yHatPred = linear_interpolation(xPred, x, y);
yHatPred = pmx_ln_interpolate(xPred, x, y);
for(i in 1:nPred)
yPred[i] = normal_rng(yHatPred[i], sigma);
}
Expand Down
46 changes: 46 additions & 0 deletions example-models/testInterp2/ln_interpolation.stan
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
data{
int nObs;
array[nObs] real xObs;
array[nObs] real yObs;
int nx;
int nPred;
array[nPred] real xPred;
}

transformed data{
real xmin = min(xObs);
real xmax = max(xObs);
}

parameters{
array[nx] real y;
real<lower = 0> sigma;
simplex[nx - 1] xSimplex;
}

transformed parameters{
array[nObs] real yHat;
array[nx] real x;

x[1] = xmin;
x[nx] = xmax;
for(i in 2:(nx-1))
x[i] = x[i-1] + xSimplex[i-1] * (xmax - xmin);

yHat = pmx_ln_interpolate(xObs, x, y);
}

model{
xSimplex ~ dirichlet(rep_vector(1, nx - 1));
y ~ normal(0, 25);
yObs ~ normal(yHat, sigma);
}

generated quantities{
array[nPred] real yHatPred;
array[nPred] real yPred;

yHatPred = pmx_ln_interpolate(xPred, x, y);
for(i in 1:nPred)
yPred[i] = normal_rng(yHatPred[i], sigma);
}

0 comments on commit f8b1698

Please sign in to comment.