Skip to content

Commit a8c4995

Browse files
authored
Merge pull request #1516 from stan-dev/laplace-check-indexing-exprs
Laplace: also check into index expressions for incompatibilities.
2 parents 4426837 + cf58a7a commit a8c4995

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

src/frontend/Typechecker.ml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,8 @@ let verify_second_order_derivative_compatibility (ast : typed_program) =
635635
let rec check_expr seen = function
636636
| {expr= FunApp (StanLib _, {name; _}, _); _}
637637
when Stan_math_signatures.lacks_higher_order_autodiff name ->
638+
(* note: we could possibly check all the arguments are DataOnly
639+
and still allow it, but those seem like mostly useless cases. *)
638640
Semantic_error.laplace_compatibility id_loc name |> error
639641
| {expr= FunApp (UserDefined _, name, es); _} ->
640642
(* we want the location to be the use-site no matter what *)
@@ -643,14 +645,15 @@ let verify_second_order_derivative_compatibility (ast : typed_program) =
643645
| {expr= Variable name; emeta= {type_= UFun _; _}} ->
644646
check_fun seen {name with id_loc}
645647
| e -> Ast.fold_expression check_expr fold_nop seen e.expr in
648+
let check_lval acc l = fold_lval_with check_expr fold_nop acc l in
646649
let rec check_stmt seen s =
647650
match s.stmt with
648651
| NRFunApp (UserDefined _, name, es) ->
649652
let seen' = check_fun seen {name with id_loc} in
650653
List.fold ~f:check_expr ~init:seen' es
651654
| stmt ->
652-
Ast.fold_statement check_expr check_stmt fold_nop fold_nop seen stmt
653-
in
655+
Ast.fold_statement check_expr check_stmt check_lval fold_nop seen
656+
stmt in
654657
let visited' = Set.add visited fn_name in
655658
List.fold ~f:check_stmt ~init:visited' (get_function_bodies fn_name) in
656659
ignore
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
functions {
2+
3+
vector algebra_system(vector x, vector y, array[] real dat,
4+
array[] int dat_int) {
5+
vector[2] f_x;
6+
f_x[1] = x[1] - y[1];
7+
f_x[2] = x[2] - y[2];
8+
return f_x;
9+
}
10+
11+
12+
real helper(real eta){
13+
array[3] real dat = {eta, 0.2, 0.3};
14+
dat[cols(algebra_solver(algebra_system, [eta]', [1]', {0.1}, {2}))] = 0.0;
15+
16+
return dat[1];
17+
}
18+
// specify negative binomial likelihood with mean offset
19+
real ll_function(vector theta, // latent Gaussian
20+
real eta,
21+
vector log_ye, // mean offset
22+
array[] int y) {
23+
24+
// observed count
25+
return neg_binomial_2_lpmf(y | exp(log_ye + theta), eta) + helper(eta);
26+
}
27+
28+
// specify covariance function
29+
matrix K_function(array[] vector x, int n_obs, real alpha, real rho) {
30+
matrix[n_obs, n_obs] K = gp_exp_quad_cov(x, alpha, rho);
31+
for (i in 1 : n_obs)
32+
K[i, i] += 1e-8;
33+
return K;
34+
}
35+
}
36+
data {
37+
int n_obs;
38+
int n_coordinates;
39+
array[n_obs] int y;
40+
vector[n_obs] ye;
41+
array[n_obs] vector[n_coordinates] x;
42+
}
43+
44+
transformed data {
45+
vector[n_obs] log_ye = log(ye);
46+
vector[n_obs] theta_0 = rep_vector(0.0, n_obs); // initial guess
47+
}
48+
parameters {
49+
real<lower=0> alpha;
50+
real<lower=0> rho;
51+
real<lower=0> eta;
52+
}
53+
54+
generated quantities {
55+
vector[n_obs] theta = laplace_latent_rng(ll_function, (eta, log_ye, y),
56+
theta_0,
57+
K_function, (x, n_obs, alpha, rho));
58+
}

test/integration/bad/embedded_laplace/stanc.expected

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,21 @@ Semantic error in 'autodiff_incompatibility4.stan', line 51, column 43 to column
5757
The function 'reduce_sum', called by this likelihood function,
5858
does not currently support higher-order derivatives, and
5959
cannot be used in an embedded Laplace approximation.
60+
[exit 1]
61+
$ ../../../../../install/default/bin/stanc autodiff_incompatibility5.stan
62+
Semantic error in 'autodiff_incompatibility5.stan', line 55, column 43 to column 54:
63+
-------------------------------------------------
64+
53:
65+
54: generated quantities {
66+
55: vector[n_obs] theta = laplace_latent_rng(ll_function, (eta, log_ye, y),
67+
^
68+
56: theta_0,
69+
57: K_function, (x, n_obs, alpha, rho));
70+
-------------------------------------------------
71+
72+
The function 'algebra_solver', called by this likelihood function,
73+
does not currently support higher-order derivatives, and
74+
cannot be used in an embedded Laplace approximation.
6075
[exit 1]
6176
$ ../../../../../install/default/bin/stanc bad_callback1.stan
6277
Semantic error in 'bad_callback1.stan', line 37, column 29 to column 40:

0 commit comments

Comments
 (0)