Skip to content

Commit cf8fd0a

Browse files
authored
Merge pull request #1258 from stan-dev/feature/std-normal-qf
Expose std_normal_log_qf
2 parents a0f23e6 + c82b3ce commit cf8fd0a

7 files changed

Lines changed: 369 additions & 31 deletions

File tree

src/middle/Stan_math_signatures.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,10 @@ let math_sigs =
350350
; ([basic_vectorized], "sinh", [DDeepVectorized], SoA)
351351
; ([basic_vectorized], "sqrt", [DDeepVectorized], SoA)
352352
; ([basic_vectorized], "square", [DDeepVectorized], SoA)
353+
(* TODO: Eventually will want to move _qf to be part of the distribution list above *)
354+
; ([basic_vectorized], "std_normal_qf", [DDeepVectorized], SoA)
355+
(* std_normal_qf is an alias for inv_Phi *)
356+
; ([basic_vectorized], "std_normal_log_qf", [DDeepVectorized], SoA)
353357
; ([basic_vectorized], "step", [DReal], SoA)
354358
; ([basic_vectorized], "tan", [DDeepVectorized], SoA)
355359
; ([basic_vectorized], "tanh", [DDeepVectorized], SoA)

src/stan_math_backend/Expression_gen.ml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ let fn_renames =
127127
; (FnNegInf, "stan::math::negative_infinity")
128128
; (FnResizeToMatch, "stan::math::resize_to_match")
129129
; (FnNaN, "std::numeric_limits<double>::quiet_NaN") ]
130+
@ [ ("lmultiply", "stan::math::multiply_log")
131+
; ("lchoose", "stan::math::binomial_coefficient_log")
132+
; ("std_normal_qf", "stan::math::inv_Phi") ]
130133
|> String.Map.of_alist_exn
131134

132135
let map_rect_calls = Int.Table.create ()
@@ -263,11 +266,6 @@ and gen_operator_app op ppf es_in =
263266
and gen_misc_special_math_app (f : string) (mem_pattern : Mem_pattern.t)
264267
(ret_type : UnsizedType.returntype option) =
265268
match f with
266-
| "lmultiply" ->
267-
Some (fun ppf es -> pp_binary_f ppf "stan::math::multiply_log" es)
268-
| "lchoose" ->
269-
Some
270-
(fun ppf es -> pp_binary_f ppf "stan::math::binomial_coefficient_log" es)
271269
| "target" -> Some (fun ppf _ -> pf ppf "stan::math::get_lp(lp__, lp_accum__)")
272270
| "get_lp" -> Some (fun ppf _ -> pf ppf "stan::math::get_lp(lp__, lp_accum__)")
273271
| f when Map.mem fn_renames f ->

test/integration/good/function-signatures/math/functions/inv_phi.stan

Lines changed: 0 additions & 25 deletions
This file was deleted.

test/integration/good/function-signatures/math/matrix/inv_Phi.stan

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
data {
22
int d_int;
3+
real d_real;
34
array[d_int] int d_int_array;
45
array[d_int] real d_real_array;
56
matrix[d_int, d_int] d_matrix;
@@ -23,6 +24,7 @@ data {
2324
array[3, 4, 5] matrix[2, 3] x5w;
2425
}
2526
transformed data {
27+
real transformed_data_real;
2628
matrix[d_int, d_int] transformed_data_matrix;
2729
vector[d_int] transformed_data_vector;
2830
row_vector[d_int] transformed_data_row_vector;
@@ -41,6 +43,8 @@ transformed data {
4143
array[3, 4, 5] row_vector[2] trans_x4w;
4244
array[3, 4, 5] matrix[2, 3] trans_x5w;
4345

46+
transformed_data_real = inv_Phi(d_int);
47+
transformed_data_real = inv_Phi(d_real);
4448
transformed_data_matrix = inv_Phi(d_matrix);
4549
transformed_data_vector = inv_Phi(d_vector);
4650
transformed_data_row_vector = inv_Phi(d_row_vector);
@@ -83,6 +87,8 @@ parameters {
8387
array[3, 4, 5] matrix[2, 3] p_x5w;
8488
}
8589
transformed parameters {
90+
real transformed_param_real;
91+
8692
matrix[d_int, d_int] transformed_param_matrix;
8793
vector[d_int] transformed_param_vector;
8894
row_vector[d_int] transformed_param_row_vector;
@@ -100,6 +106,10 @@ transformed parameters {
100106
array[3, 4, 5] row_vector[2] trans_p_x4w;
101107
array[3, 4, 5] matrix[2, 3] trans_p_x5w;
102108

109+
transformed_param_real = inv_Phi(d_int);
110+
transformed_param_real = inv_Phi(d_real);
111+
transformed_param_real = inv_Phi(p_real);
112+
103113
transformed_param_matrix = inv_Phi(d_matrix);
104114
transformed_param_vector = inv_Phi(d_vector);
105115
transformed_param_row_vector = inv_Phi(d_row_vector);
@@ -124,4 +134,3 @@ transformed parameters {
124134
model {
125135
y_p ~ normal(0, 1);
126136
}
127-
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
data {
2+
int d_int;
3+
real d_real;
4+
array[d_int] int d_int_array;
5+
array[d_int] real d_real_array;
6+
matrix[d_int, d_int] d_matrix;
7+
vector[d_int] d_vector;
8+
row_vector[d_int] d_row_vector;
9+
10+
array[3] vector[2] x3y;
11+
array[3] row_vector[2] x4y;
12+
array[3] matrix[2, 3] x5y;
13+
14+
array[3, 4] int x1z;
15+
array[3, 4] real x2z;
16+
array[3, 4] vector[2] x3z;
17+
array[3, 4] row_vector[2] x4z;
18+
array[3, 4] matrix[2, 3] x5z;
19+
20+
array[3, 4, 5] int x1w;
21+
array[3, 4, 5] real x2w;
22+
array[3, 4, 5] vector[2] x3w;
23+
array[3, 4, 5] row_vector[2] x4w;
24+
array[3, 4, 5] matrix[2, 3] x5w;
25+
}
26+
transformed data {
27+
real transformed_data_real;
28+
matrix[d_int, d_int] transformed_data_matrix;
29+
vector[d_int] transformed_data_vector;
30+
row_vector[d_int] transformed_data_row_vector;
31+
32+
array[3] vector[2] trans_x3y;
33+
array[3] row_vector[2] trans_x4y;
34+
array[3] matrix[2, 3] trans_x5y;
35+
36+
array[3, 4] real trans_x2z;
37+
array[3, 4] vector[2] trans_x3z;
38+
array[3, 4] row_vector[2] trans_x4z;
39+
array[3, 4] matrix[2, 3] trans_x5z;
40+
41+
array[3, 4, 5] real trans_x2w;
42+
array[3, 4, 5] vector[2] trans_x3w;
43+
array[3, 4, 5] row_vector[2] trans_x4w;
44+
array[3, 4, 5] matrix[2, 3] trans_x5w;
45+
46+
transformed_data_real = std_normal_log_qf(d_int);
47+
transformed_data_real = std_normal_log_qf(d_real);
48+
transformed_data_matrix = std_normal_log_qf(d_matrix);
49+
transformed_data_vector = std_normal_log_qf(d_vector);
50+
transformed_data_row_vector = std_normal_log_qf(d_row_vector);
51+
trans_x3y = std_normal_log_qf(x3y);
52+
trans_x4y = std_normal_log_qf(x4y);
53+
trans_x5y = std_normal_log_qf(x5y);
54+
55+
trans_x2z = std_normal_log_qf(x1z);
56+
trans_x2z = std_normal_log_qf(x2z);
57+
trans_x3z = std_normal_log_qf(x3z);
58+
trans_x4z = std_normal_log_qf(x4z);
59+
trans_x5z = std_normal_log_qf(x5z);
60+
61+
trans_x2w = std_normal_log_qf(x1w);
62+
trans_x2w = std_normal_log_qf(x2w);
63+
trans_x3w = std_normal_log_qf(x3w);
64+
trans_x4w = std_normal_log_qf(x4w);
65+
trans_x5w = std_normal_log_qf(x5w);
66+
}
67+
parameters {
68+
real p_real;
69+
real y_p;
70+
array[d_int] real p_real_array;
71+
matrix[d_int, d_int] p_matrix;
72+
vector[d_int] p_vector;
73+
row_vector[d_int] p_row_vector;
74+
75+
array[3] vector[2] p_x3y;
76+
array[3] row_vector[2] p_x4y;
77+
array[3] matrix[2, 3] p_x5y;
78+
79+
array[3, 4] real p_x2z;
80+
array[3, 4] vector[2] p_x3z;
81+
array[3, 4] row_vector[2] p_x4z;
82+
array[3, 4] matrix[2, 3] p_x5z;
83+
84+
array[3, 4, 5] real p_x2w;
85+
array[3, 4, 5] vector[2] p_x3w;
86+
array[3, 4, 5] row_vector[2] p_x4w;
87+
array[3, 4, 5] matrix[2, 3] p_x5w;
88+
}
89+
transformed parameters {
90+
real transformed_param_real;
91+
92+
matrix[d_int, d_int] transformed_param_matrix;
93+
vector[d_int] transformed_param_vector;
94+
row_vector[d_int] transformed_param_row_vector;
95+
array[3] vector[2] trans_p_x3y;
96+
array[3] row_vector[2] trans_p_x4y;
97+
array[3] matrix[2, 3] trans_p_x5y;
98+
99+
array[3, 4] real trans_p_x2z;
100+
array[3, 4] vector[2] trans_p_x3z;
101+
array[3, 4] row_vector[2] trans_p_x4z;
102+
array[3, 4] matrix[2, 3] trans_p_x5z;
103+
104+
array[3, 4, 5] real trans_p_x2w;
105+
array[3, 4, 5] vector[2] trans_p_x3w;
106+
array[3, 4, 5] row_vector[2] trans_p_x4w;
107+
array[3, 4, 5] matrix[2, 3] trans_p_x5w;
108+
109+
transformed_param_real = std_normal_log_qf(d_int);
110+
transformed_param_real = std_normal_log_qf(d_real);
111+
transformed_param_real = std_normal_log_qf(p_real);
112+
113+
transformed_param_matrix = std_normal_log_qf(d_matrix);
114+
transformed_param_vector = std_normal_log_qf(d_vector);
115+
transformed_param_row_vector = std_normal_log_qf(d_row_vector);
116+
transformed_param_matrix = std_normal_log_qf(p_matrix);
117+
transformed_param_vector = std_normal_log_qf(p_vector);
118+
transformed_param_row_vector = std_normal_log_qf(p_row_vector);
119+
120+
trans_p_x3y = std_normal_log_qf(p_x3y);
121+
trans_p_x4y = std_normal_log_qf(p_x4y);
122+
trans_p_x5y = std_normal_log_qf(p_x5y);
123+
124+
trans_p_x2z = std_normal_log_qf(p_x2z);
125+
trans_p_x3z = std_normal_log_qf(p_x3z);
126+
trans_p_x4z = std_normal_log_qf(p_x4z);
127+
trans_p_x5z = std_normal_log_qf(p_x5z);
128+
129+
trans_p_x2w = std_normal_log_qf(p_x2w);
130+
trans_p_x3w = std_normal_log_qf(p_x3w);
131+
trans_p_x4w = std_normal_log_qf(p_x4w);
132+
trans_p_x5w = std_normal_log_qf(p_x5w);
133+
}
134+
model {
135+
y_p ~ normal(0, 1);
136+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
data {
2+
int d_int;
3+
real d_real;
4+
array[d_int] int d_int_array;
5+
array[d_int] real d_real_array;
6+
matrix[d_int, d_int] d_matrix;
7+
vector[d_int] d_vector;
8+
row_vector[d_int] d_row_vector;
9+
10+
array[3] vector[2] x3y;
11+
array[3] row_vector[2] x4y;
12+
array[3] matrix[2, 3] x5y;
13+
14+
array[3, 4] int x1z;
15+
array[3, 4] real x2z;
16+
array[3, 4] vector[2] x3z;
17+
array[3, 4] row_vector[2] x4z;
18+
array[3, 4] matrix[2, 3] x5z;
19+
20+
array[3, 4, 5] int x1w;
21+
array[3, 4, 5] real x2w;
22+
array[3, 4, 5] vector[2] x3w;
23+
array[3, 4, 5] row_vector[2] x4w;
24+
array[3, 4, 5] matrix[2, 3] x5w;
25+
}
26+
transformed data {
27+
real transformed_data_real;
28+
matrix[d_int, d_int] transformed_data_matrix;
29+
vector[d_int] transformed_data_vector;
30+
row_vector[d_int] transformed_data_row_vector;
31+
32+
array[3] vector[2] trans_x3y;
33+
array[3] row_vector[2] trans_x4y;
34+
array[3] matrix[2, 3] trans_x5y;
35+
36+
array[3, 4] real trans_x2z;
37+
array[3, 4] vector[2] trans_x3z;
38+
array[3, 4] row_vector[2] trans_x4z;
39+
array[3, 4] matrix[2, 3] trans_x5z;
40+
41+
array[3, 4, 5] real trans_x2w;
42+
array[3, 4, 5] vector[2] trans_x3w;
43+
array[3, 4, 5] row_vector[2] trans_x4w;
44+
array[3, 4, 5] matrix[2, 3] trans_x5w;
45+
46+
transformed_data_real = std_normal_qf(d_int);
47+
transformed_data_real = std_normal_qf(d_real);
48+
transformed_data_matrix = std_normal_qf(d_matrix);
49+
transformed_data_vector = std_normal_qf(d_vector);
50+
transformed_data_row_vector = std_normal_qf(d_row_vector);
51+
trans_x3y = std_normal_qf(x3y);
52+
trans_x4y = std_normal_qf(x4y);
53+
trans_x5y = std_normal_qf(x5y);
54+
55+
trans_x2z = std_normal_qf(x1z);
56+
trans_x2z = std_normal_qf(x2z);
57+
trans_x3z = std_normal_qf(x3z);
58+
trans_x4z = std_normal_qf(x4z);
59+
trans_x5z = std_normal_qf(x5z);
60+
61+
trans_x2w = std_normal_qf(x1w);
62+
trans_x2w = std_normal_qf(x2w);
63+
trans_x3w = std_normal_qf(x3w);
64+
trans_x4w = std_normal_qf(x4w);
65+
trans_x5w = std_normal_qf(x5w);
66+
}
67+
parameters {
68+
real p_real;
69+
real y_p;
70+
array[d_int] real p_real_array;
71+
matrix[d_int, d_int] p_matrix;
72+
vector[d_int] p_vector;
73+
row_vector[d_int] p_row_vector;
74+
75+
array[3] vector[2] p_x3y;
76+
array[3] row_vector[2] p_x4y;
77+
array[3] matrix[2, 3] p_x5y;
78+
79+
array[3, 4] real p_x2z;
80+
array[3, 4] vector[2] p_x3z;
81+
array[3, 4] row_vector[2] p_x4z;
82+
array[3, 4] matrix[2, 3] p_x5z;
83+
84+
array[3, 4, 5] real p_x2w;
85+
array[3, 4, 5] vector[2] p_x3w;
86+
array[3, 4, 5] row_vector[2] p_x4w;
87+
array[3, 4, 5] matrix[2, 3] p_x5w;
88+
}
89+
transformed parameters {
90+
real transformed_param_real;
91+
92+
matrix[d_int, d_int] transformed_param_matrix;
93+
vector[d_int] transformed_param_vector;
94+
row_vector[d_int] transformed_param_row_vector;
95+
array[3] vector[2] trans_p_x3y;
96+
array[3] row_vector[2] trans_p_x4y;
97+
array[3] matrix[2, 3] trans_p_x5y;
98+
99+
array[3, 4] real trans_p_x2z;
100+
array[3, 4] vector[2] trans_p_x3z;
101+
array[3, 4] row_vector[2] trans_p_x4z;
102+
array[3, 4] matrix[2, 3] trans_p_x5z;
103+
104+
array[3, 4, 5] real trans_p_x2w;
105+
array[3, 4, 5] vector[2] trans_p_x3w;
106+
array[3, 4, 5] row_vector[2] trans_p_x4w;
107+
array[3, 4, 5] matrix[2, 3] trans_p_x5w;
108+
109+
transformed_param_real = std_normal_qf(d_int);
110+
transformed_param_real = std_normal_qf(d_real);
111+
transformed_param_real = std_normal_qf(p_real);
112+
113+
transformed_param_matrix = std_normal_qf(d_matrix);
114+
transformed_param_vector = std_normal_qf(d_vector);
115+
transformed_param_row_vector = std_normal_qf(d_row_vector);
116+
transformed_param_matrix = std_normal_qf(p_matrix);
117+
transformed_param_vector = std_normal_qf(p_vector);
118+
transformed_param_row_vector = std_normal_qf(p_row_vector);
119+
120+
trans_p_x3y = std_normal_qf(p_x3y);
121+
trans_p_x4y = std_normal_qf(p_x4y);
122+
trans_p_x5y = std_normal_qf(p_x5y);
123+
124+
trans_p_x2z = std_normal_qf(p_x2z);
125+
trans_p_x3z = std_normal_qf(p_x3z);
126+
trans_p_x4z = std_normal_qf(p_x4z);
127+
trans_p_x5z = std_normal_qf(p_x5z);
128+
129+
trans_p_x2w = std_normal_qf(p_x2w);
130+
trans_p_x3w = std_normal_qf(p_x3w);
131+
trans_p_x4w = std_normal_qf(p_x4w);
132+
trans_p_x5w = std_normal_qf(p_x5w);
133+
}
134+
model {
135+
y_p ~ normal(0, 1);
136+
}

0 commit comments

Comments
 (0)