Skip to content

Commit a20c2ce

Browse files
committed
Added docs for two new GLMs and broadcasting in GLMs
1 parent 2357114 commit a20c2ce

File tree

4 files changed

+320
-2
lines changed

4 files changed

+320
-2
lines changed

src/functions-reference/binary_distributions.Rmd

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,49 @@ dropping constant additive terms.
142142

143143
### Stan Functions
144144

145+
<!-- real; bernoulli_logit_glm_lpmf; (int y | matrix x, real alpha, vector beta); -->
146+
\index{{\tt \bfseries bernoulli\_logit\_glm\_lpmf }!{\tt (int y \textbar\ matrix x, real alpha, vector beta): real}|hyperpage}
147+
148+
`real` **`bernoulli_logit_glm_lpmf`**`(int y | matrix x, real alpha, vector beta)`<br>\newline
149+
The log Bernoulli probability mass of y given chance of success
150+
`inv_logit(alpha+x*beta)`, where a constant intercept `alpha` and dependant variable value `y` are used
151+
for all observations. The number of columns of `x` needs to match the length of the
152+
weight vector `beta`.
153+
If `x` and `y` are data (not parameters) this function can be executed on a GPU.
154+
155+
<!-- real; bernoulli_logit_glm_lpmf; (int y | matrix x, vector alpha, vector beta); -->
156+
\index{{\tt \bfseries bernoulli\_logit\_glm\_lpmf }!{\tt (int y \textbar\ matrix x, vector alpha, vector beta): real}|hyperpage}
157+
158+
`real` **`bernoulli_logit_glm_lpmf`**`(int y | matrix x, vector alpha, vector beta)`<br>\newline
159+
The log Bernoulli probability mass of y given chance of success
160+
`inv_logit(alpha+x*beta)`, where an intercept `alpha` is used that is
161+
allowed to vary with the different observations. The dependant variable
162+
value `y` is used for all observations.
163+
The number of rows of `x` must match the length of `alpha` and
164+
the number of columns of `x` needs to match the length of the weight vector `beta`.
165+
If `x` and `y` are data (not parameters) this function can be executed on a GPU.
166+
167+
<!-- real; bernoulli_logit_glm_lpmf; (int[] y | row_vector x, real alpha, vector beta); -->
168+
\index{{\tt \bfseries bernoulli\_logit\_glm\_lpmf }!{\tt (int[] y \textbar\ row\_vector x, real alpha, vector beta): real}|hyperpage}
169+
170+
`real` **`bernoulli_logit_glm_lpmf`**`(int[] y | row_vector x, real alpha, vector beta)`<br>\newline
171+
The log Bernoulli probability mass of y given chance of success
172+
`inv_logit(alpha+x*beta)`, where a constant intercept `alpha` and
173+
same indepependent variables values `x` are used for all observations.
174+
The number of columns of `x` needs to match the length of the weight vector `beta`.
175+
176+
<!-- real; bernoulli_logit_glm_lpmf; (int[] y | row_vector x, vector alpha, vector beta); -->
177+
\index{{\tt \bfseries bernoulli\_logit\_glm\_lpmf }!{\tt (int[] y \textbar\ row\_vector x, vector alpha, vector beta): real}|hyperpage}
178+
179+
`real` **`bernoulli_logit_glm_lpmf`**`(int[] y | row_vector x, vector alpha, vector beta)`<br>\newline
180+
The log Bernoulli probability mass of y given chance of success
181+
`inv_logit(alpha+x*beta)`, where an intercept `alpha` is used that is
182+
allowed to vary with the different observations.
183+
Same indepependent variables values `x` are used for all observations.
184+
The length of `y` must match the length of `alpha` and
185+
the number of columns of `x` needs to match the length of the weight vector `beta`.
186+
187+
145188
<!-- real; bernoulli_logit_glm_lpmf; (int[] y | matrix x, real alpha, vector beta); -->
146189
\index{{\tt \bfseries bernoulli\_logit\_glm\_lpmf }!{\tt (int[] y \textbar\ matrix x, real alpha, vector beta): real}|hyperpage}
147190

@@ -152,6 +195,7 @@ for all observations. The number of rows of the independent variable
152195
matrix `x` needs to match the length of the dependent variable vector
153196
`y` and the number of columns of `x` needs to match the length of the
154197
weight vector `beta`.
198+
If `x` and `y` are data (not parameters) this function can be executed on a GPU.
155199

156200
<!-- real; bernoulli_logit_glm_lpmf; (int[] y | matrix x, vector alpha, vector beta); -->
157201
\index{{\tt \bfseries bernoulli\_logit\_glm\_lpmf }!{\tt (int[] y \textbar\ matrix x, vector alpha, vector beta): real}|hyperpage}
@@ -163,3 +207,4 @@ allowed to vary with the different observations. The number of rows of
163207
the independent variable matrix `x` needs to match the length of the
164208
dependent variable vector `y` and `alpha` and the number of columns of
165209
`x` needs to match the length of the weight vector `beta`.
210+
If `x` and `y` are data (not parameters) this function can be executed on a GPU.

src/functions-reference/bounded_discrete_distributions.Rmd

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ cat(' * <a href="binomial-distribution-logit-parameterization.html">Binomial Dis
1010
cat(' * <a href="beta-binomial-distribution.html">Beta-Binomial Distribution</a>\n')
1111
cat(' * <a href="hypergeometric-distribution.html">Hypergeometric Distribution</a>\n')
1212
cat(' * <a href="categorical-distribution.html">Categorical Distribution</a>\n')
13+
cat(' * <a href="categorical-logit-glm.html">Categorical Logit Generalised Linear Model (Softmax Regression)</a>\n')
1314
cat(' * <a href="ordered-logistic-distribution.html">Ordered Logistic Distribution</a>\n')
15+
cat(' * <a href="ordered-logistic-glm.html">Ordered Logistic Generalised Linear Model (Ordinal Regression)</a>\n')
1416
cat(' * <a href="ordered-probit-distribution.html">Ordered Probit Distribution</a>\n')
1517
}
1618
```
@@ -296,6 +298,81 @@ Generate a categorical variate with outcome in range $1:N$ from
296298
log-odds vector beta; may only be used in transformed data and generated
297299
quantities blocks
298300

301+
## Categorical Logit Generalised Linear Model (Softmax Regression) {#categorical-logit-glm}
302+
303+
Stan also supplies a single primitive for a Generalised Linear Model
304+
with Categorical likelihood and logit link function, i.e. a primitive
305+
for a softmax regression. This should provide a more efficient
306+
implementation of softmax regression than a manually written
307+
regression in terms of a Categorical likelihood and matrix
308+
multiplication.
309+
310+
### Probability Mass Functions
311+
312+
If $N,M,K \in \mathbb{N}$, $N,M,K > 0$, and if $x\in \mathbb{R}^{M\cdot K}, \alpha \in \mathbb{R}^N, \beta\in \mathbb{R}^{K\cdot N}$, then for $y \in \{1,\ldots,N\}^M$,
313+
\[ \text{CategoricalLogitGLM}(y~|~x,\alpha,\beta) = \\[5pt]
314+
\prod_{1\leq i \leq M}\text{CategoricalLogit}(y_i~|~\alpha+\beta_i\cdot x_i) = \\[15pt]
315+
\prod_{1\leq i \leq M}\text{Categorical}(y_i~|~softmax(\alpha+\beta_i\cdot x_i)). \]
316+
See section [softmax](#softmax) for the definition of the softmax function.
317+
318+
### Sampling Statement
319+
320+
`y ~ ` **`categorical_logit_glm`**`(x, alpha, beta)`
321+
322+
Increment target log probability density with `categorical_logit_glm(y | x, alpha, beta)`
323+
dropping constant additive terms.
324+
<!-- real; categorical_logit_glm ~; -->
325+
\index{{\tt \bfseries categorical\_logit\_glm }!sampling statement|hyperpage}
326+
327+
328+
### Stan Functions
329+
330+
<!-- real; categorical_logit_glm_lpmf; (int y | row_vector x, vector alpha, matrix beta); -->
331+
\index{{\tt \bfseries categorical\_logit\_glm\_lpmf }!{\tt (int y \textbar\ row\_vector x, vector alpha, matrix beta): real}|hyperpage}
332+
333+
`real` **`categorical_logit_glm_lpmf`**`(int y | row_vector x, vector alpha, matrix beta)`<br>\newline
334+
The log categorical probability mass function with outcome `y` in
335+
$1:N$ given $N$-vector of log-odds of outcomes `alpha + x * beta`.
336+
The size of independant variable row vector `x` needs to match the number of rows of the
337+
weight matrix `beta`. The size of intercept vector `alpha` must match number
338+
of columns of the weight matrix `beta`.
339+
340+
<!-- real; categorical_logit_glm_lpmf; (int y | matrix x, vector alpha, matrix beta); -->
341+
\index{{\tt \bfseries categorical\_logit\_glm\_lpmf }!{\tt (int y \textbar\ matrix x, vector alpha, matrix beta): real}|hyperpage}
342+
343+
`real` **`categorical_logit_glm_lpmf`**`(int y | matrix x, vector alpha, matrix beta)`<br>\newline
344+
The log categorical probability mass function with outcomes `y` in
345+
$1:N$ given $N$-vector of log-odds of outcomes `alpha + x * beta`.
346+
Same vector of intercepts `alpha` and same dependant variable value `y` are used for all instances.
347+
The number of columns of independant variable `x` needs to match the number of rows of the
348+
weight matrix `beta`. The size of intercept vector `alpha` must match number
349+
of columns of the weight matrix `beta`. If `x` and `y` are data (not parameters) this function can be executed on a GPU.
350+
351+
<!-- real; categorical_logit_glm_lpmf; (int[] y | vector theta); -->
352+
\index{{\tt \bfseries categorical\_logit\_glm\_lpmf }!{\tt (int[] y \textbar\ row\_vector x, vector alpha, matrix beta): real}|hyperpage}
353+
354+
`real` **`categorical_logit_glm_lpmf`**`(int[] y | row_vector x, vector alpha, matrix beta)`<br>\newline
355+
The log categorical probability mass function with outcomes `y` in
356+
$1:N$ given $N$-vector of log-odds of outcomes `alpha + x * beta`.
357+
Same vector of intercepts `alpha` and same row vector of independant variables `x` are used for all instances.
358+
The size of independant variable matrix `x` needs to match the number of rows of the
359+
weight vector `beta`. The size of intercept vector `alpha` must match number
360+
of columns of the weight vector `beta`.
361+
362+
<!-- real; categorical_logit_glm_lpmf; (int[] y | vector theta); -->
363+
\index{{\tt \bfseries categorical\_logit\_glm\_lpmf }!{\tt (int[] y \textbar\ matrix x, vector alpha, matrix beta): real}|hyperpage}
364+
365+
`real` **`categorical_logit_glm_lpmf`**`(int[] y | matrix x, vector alpha, matrix beta)`<br>\newline
366+
The log categorical probability mass function with outcomes `y` in
367+
$1:N$ given $N$-vector of log-odds of outcomes `alpha + x * beta`.
368+
Same vector of intercepts `alpha` is used for all instances.
369+
The number of rows of the independent variable
370+
matrix `x` needs to match the length of the dependent variable vector
371+
`y`. The number of columns of independant variable `x` needs to match the number of rows of the
372+
weight matrix `beta`. The size of intercept vector `alpha` must match number
373+
of columns of the weight matrix `beta`. If `x` and `y` are data (not parameters) this function can be executed on a GPU.
374+
375+
299376
## Ordered Logistic Distribution
300377

301378
### Probability Mass Function
@@ -339,6 +416,73 @@ eta, and cutpoints c.
339416
Generate an ordered logistic variate with linear predictor eta and
340417
cutpoints c; may only be used in transformed data and generated quantities blocks
341418

419+
## Ordered Logistic Generalised Linear Model (Ordinal Regression)
420+
421+
### Probability Mass Function
422+
423+
If $N,M,K \in \mathbb{N}$ with $N, M > 0$, $K > 2$, $c \in \mathbb{R}^{K-1}$ such that
424+
$c_k < c_{k+1}$ for $k \in \{1,\ldots,K-2\}$, and $x\in \mathbb{R}^{N\cdot M}, \beta\in \mathbb{R}^M$, then for $y \in \{1,\ldots,K\}^N$,
425+
\[\text{OrderedLogisticGLM}(y~|~x,\beta,c) = \\[4pt]
426+
\prod_{1\leq i \leq N}\text{OrderedLogistic}(y_i~|~x_i\cdot \beta,c) = \\[17pt]
427+
\prod_{1\leq i \leq N}\left\{ \begin{array}{ll}
428+
1 - \text{logit}^{-1}(x_i\cdot \beta - c_1) & \text{if } y = 1, \\[4pt]
429+
\text{logit}^{-1}(x_i\cdot \beta - c_{y-1}) - \text{logit}^{-1}(x_i\cdot \beta - c_{y}) & \text{if } 1 < y < K, \text{and} \\[4pt]
430+
\text{logit}^{-1}(x_i\cdot \beta - c_{K-1}) - 0 & \text{if } y = K.
431+
\end{array} \right. \] The $k=K$
432+
case is written with the redundant subtraction of zero to illustrate
433+
the parallelism of the cases; the $y=1$ and $y=K$ edge cases can be
434+
subsumed into the general definition by setting $c_0 = -\infty$ and
435+
$c_K = +\infty$ with $\text{logit}^{-1}(-\infty) = 0$ and
436+
$\text{logit}^{-1}(\infty) = 1$.
437+
438+
### Sampling Statement
439+
440+
`y ~ ` **`ordered_logistic_glm`**`(x, beta, c)`
441+
442+
Increment target log probability density with `ordered_logistic_lpmf(y | x, beta, c)`
443+
dropping constant additive terms.
444+
<!-- real; ordered_logistic ~; -->
445+
\index{{\tt \bfseries ordered\_logistic\_glm }!sampling statement|hyperpage}
446+
447+
### Stan Functions
448+
449+
<!-- real; ordered_logistic_glm_lpmf; (int y | row_vector x, vector beta, vector c); -->
450+
\index{{\tt \bfseries ordered\_logistic\_glm\_lpmf }!{\tt (int y \textbar\ row\_vector x, vector beta, vector c): real}|hyperpage}
451+
452+
`real` **`ordered_logistic_glm_lpmf`**`(int y | row_vector x, vector beta, vector c)`<br>\newline
453+
The log ordered logistic probability mass of y, given linear predictors `x * beta`, and cutpoints c.
454+
The size of independant variable row vector `x` needs to match the size of the weight vector `beta`.
455+
Cutpoints `c` must be ordered.
456+
457+
<!-- real; ordered_logistic_glm_lpmf; (int y | matrix x, vector beta, vector c); -->
458+
\index{{\tt \bfseries ordered\_logistic\_glm\_lpmf }!{\tt (int y \textbar\ matrix x, vector beta, vector c): real}|hyperpage}
459+
460+
`real` **`ordered_logistic_glm_lpmf`**`(int y | matrix x, vector beta, vector c)`<br>\newline
461+
The log ordered logistic probability mass of y, given linear predictors `x * beta`, and cutpoints c.
462+
Same value of independant variable `y` is used for all instances.
463+
The number of columns of independant variable row vector `x` needs to match the size of the weight vector `beta`.
464+
Cutpoints `c` must be ordered. If `x` and `y` are data (not parameters) this function can be executed on a GPU.
465+
466+
<!-- real; ordered_logistic_glm_lpmf; (int[] y | row_vector x, vector beta, vector c); -->
467+
\index{{\tt \bfseries ordered\_logistic\_glm\_lpmf }!{\tt (int[] y \textbar\ row\_vector x, vector beta, vector c): real}|hyperpage}
468+
469+
`real` **`ordered_logistic_glm_lpmf`**`(int[] y | row_vector x, vector beta, vector c)`<br>\newline
470+
The log ordered logistic probability mass of y, given linear predictors `x * beta`, and cutpoints c.
471+
Same row vector of independant variables `x` is used for all instances.
472+
The size of independant variable row vector `x` needs to match the size of the weight vector `beta`.
473+
Cutpoints `c` must be ordered.
474+
475+
<!-- real; ordered_logistic_glm_lpmf; (int[] y | matrix x, vector beta, vector c); -->
476+
\index{{\tt \bfseries ordered\_logistic\_glm\_lpmf }!{\tt (int[] y \textbar\ matrix x, vector beta, vector c): real}|hyperpage}
477+
478+
`real` **`ordered_logistic_glm_lpmf`**`(int[] y | matrix x, vector beta, vector c)`<br>\newline
479+
The log ordered logistic probability mass of y, given linear predictors
480+
`x * beta`, and cutpoints c.
481+
The number of rows of the independent variable matrix `x` needs to match the length of the dependent variable vector `y`.
482+
The number of columns of independant variable row vector `x` needs to match the size of the weight vector `beta`.
483+
Cutpoints `c` must be ordered. If `x` and `y` are data (not parameters) this function can be executed on a GPU.
484+
485+
342486
## Ordered Probit Distribution
343487

344488
### Probability Mass Function

src/functions-reference/unbounded_continuous_distributions.Rmd

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,49 @@ dropping constant additive terms.
176176

177177
### Stan Functions
178178

179+
<!-- real; normal_id_glm_lpdf; (real y | matrix x, real alpha, vector beta, real sigma); -->
180+
\index{{\tt \bfseries normal\_id\_glm\_lpdf }!{\tt (real y \textbar\ matrix x, real alpha, vector beta, real sigma): real}|hyperpage}
181+
182+
`real` **`normal_id_glm_lpdf`**`(real y | matrix x, real alpha, vector beta, real sigma)`<br>\newline
183+
The log normal probability density of y given location `alpha+x*beta`
184+
and scale `sigma`, where a constant intercept `alpha`, `sigma` and dependent variable value `y` are
185+
used for all observations. The number of columns of `x` needs to match
186+
the length of the weight vector `beta`.
187+
If `x` and `y` are data (not parameters) this function can be executed on a GPU.
188+
189+
<!-- real; normal_id_glm_lpdf; (real y | matrix x, vector alpha, vector beta, real sigma); -->
190+
\index{{\tt \bfseries normal\_id\_glm\_lpdf }!{\tt (real y \textbar\ matrix x, vector alpha, vector beta, real sigma): real}|hyperpage}
191+
192+
`real` **`normal_id_glm_lpdf`**`(real y | matrix x, vector alpha, vector beta, real sigma)`<br>\newline
193+
The log normal probability density of y given location `alpha+x*beta`
194+
and scale `sigma`, where a constant `sigma` and dependent variable value`y` are used for all
195+
observations and an intercept `alpha` is used that is allowed to vary
196+
with the different observations. The number of rows of the independent
197+
variable matrix `x` needs to match the length of the intercept
198+
vector `alpha` and the number of columns of `x` needs to match
199+
the length of the weight vector `beta`.
200+
If `x` and `y` are data (not parameters) this function can be executed on a GPU.
201+
202+
<!-- real; normal_id_glm_lpdf; (vector y | row_vector x, real alpha, vector beta, real sigma); -->
203+
\index{{\tt \bfseries normal\_id\_glm\_lpdf }!{\tt (vector y \textbar\ row\_vector x, real alpha, vector beta, real sigma): real}|hyperpage}
204+
205+
`real` **`normal_id_glm_lpdf`**`(vector y | row_vector x, real alpha, vector beta, real sigma)`<br>\newline
206+
The log normal probability density of y given location `alpha+x*beta`
207+
and scale `sigma`, where a constant intercept `alpha`, `sigma` and independent variable values `x` are
208+
used for all observations. The number of columns of `x` needs to match
209+
the length of the weight vector `beta`.
210+
211+
<!-- real; normal_id_glm_lpdf; (vector y | row_vector x, vector alpha, vector beta, real sigma); -->
212+
\index{{\tt \bfseries normal\_id\_glm\_lpdf }!{\tt (vector y \textbar\ row\_vector x, vector alpha, vector beta, real sigma): real}|hyperpage}
213+
214+
`real` **`normal_id_glm_lpdf`**`(vector y | row_vector x, vector alpha, vector beta, real sigma)`<br>\newline
215+
The log normal probability density of y given location `alpha+x*beta`
216+
and scale `sigma`, where a constant `sigma` and independent variable values `x` are used for all
217+
observations and an intercept `alpha` is used that is allowed to vary
218+
with the different observations. The length of the dependent
219+
variable vector `y` needs to match the length of the intercept vector `alpha` and the number of columns of `x` needs to match
220+
the length of the weight vector `beta`.
221+
179222
<!-- real; normal_id_glm_lpdf; (vector y | matrix x, real alpha, vector beta, real sigma); -->
180223
\index{{\tt \bfseries normal\_id\_glm\_lpdf }!{\tt (vector y \textbar\ matrix x, real alpha, vector beta, real sigma): real}|hyperpage}
181224

@@ -186,6 +229,7 @@ used for all observations. The number of rows of the independent
186229
variable matrix `x` needs to match the length of the dependent
187230
variable vector `y` and the number of columns of `x` needs to match
188231
the length of the weight vector `beta`.
232+
If `x` and `y` are data (not parameters) this function can be executed on a GPU.
189233

190234
<!-- real; normal_id_glm_lpdf; (vector y | matrix x, vector alpha, vector beta, real sigma); -->
191235
\index{{\tt \bfseries normal\_id\_glm\_lpdf }!{\tt (vector y \textbar\ matrix x, vector alpha, vector beta, real sigma): real}|hyperpage}
@@ -196,8 +240,9 @@ and scale `sigma`, where a constant `sigma` is used for all
196240
observations and an intercept `alpha` is used that is allowed to vary
197241
with the different observations. The number of rows of the independent
198242
variable matrix `x` needs to match the length of the dependent
199-
variable vector `y` and the number of columns of `x` needs to match
243+
variable vector `y` and the length of intercept vector alpha. The number of columns of `x` needs to match
200244
the length of the weight vector `beta`.
245+
If `x` and `y` are data (not parameters) this function can be executed on a GPU.
201246

202247
## Exponentially Modified Normal Distribution
203248

0 commit comments

Comments
 (0)