# \#user-defined-functions

**URL:** https://discourse.mc-stan.org/tag/user-defined-functions/150.md

[Latest](https://discourse.mc-stan.org/latest.md) · [Categories](https://discourse.mc-stan.org/categories.md) · [Tags](https://discourse.mc-stan.org/tags.md)

---

## [Following up on several discussions of simplex adjustments and ragged arrays of simplexes](https://discourse.mc-stan.org/t/following-up-on-several-discussions-of-simplex-adjustments-and-ragged-arrays-of-simplexes/36218)

<div class="topic-metadata">

**Author:** [@alsosortofadam](https://discourse.mc-stan.org/u/alsosortofadam)\
**Replies:** 13\
**Last updated:** [December 10, 2024, 11:24pm UTC](https://discourse.mc-stan.org/t/following-up-on-several-discussions-of-simplex-adjustments-and-ragged-arrays-of-simplexes/36218 "2024-12-10T23:24:53Z")

</div>

I am trying to code a model that requires defining ragged arrays of simplexes. But for simplicity, I want to ask how to code a vector transformation that applies the same constraints as a simplex (without using an explic…

---

## [Log\_dirichlet\_rng(alpha) and multinomial\_log\_rng(log\_theta)](https://discourse.mc-stan.org/t/log-dirichlet-rng-alpha-and-multinomial-log-rng-log-theta/37065)

<div class="topic-metadata">

**Author:** [@alsosortofadam](https://discourse.mc-stan.org/u/alsosortofadam)\
**Replies:** 3\
**Last updated:** [October 29, 2024, 10:52pm UTC](https://discourse.mc-stan.org/t/log-dirichlet-rng-alpha-and-multinomial-log-rng-log-theta/37065 "2024-10-29T22:52:50Z")

</div>

Hi! I’m working with a multinomial-Dirichlet model in which I transformed everything to the log form to address fitting issues. I have the \_lpdf/\_lpmf functions specified correctly, but had to specify the model in a way…

---

## [Complicated variance-covariance matrix not positive definite](https://discourse.mc-stan.org/t/complicated-variance-covariance-matrix-not-positive-definite/34999)

<div class="topic-metadata">

**Author:** [@spencergordon](https://discourse.mc-stan.org/u/spencergordon)\
**Replies:** 2\
**Last updated:** [May 3, 2024, 9:39pm UTC](https://discourse.mc-stan.org/t/complicated-variance-covariance-matrix-not-positive-definite/34999 "2024-05-03T21:39:25Z")

</div>

I am modeling a set of quantiles from a Gaussian mixture distribution, and it involves calculating the quantile function of a Gaussian mixture as well as populating a variance-covariance matrix with values that are a fun…

---

## [Custom function to reallocate probability mass from certain integers to others](https://discourse.mc-stan.org/t/custom-function-to-reallocate-probability-mass-from-certain-integers-to-others/34988)

<div class="topic-metadata">

**Author:** [@dholstius](https://discourse.mc-stan.org/u/dholstius)\
**Replies:** 0\
**Last updated:** [May 1, 2024, 11:01pm UTC](https://discourse.mc-stan.org/t/custom-function-to-reallocate-probability-mass-from-certain-integers-to-others/34988 "2024-05-01T23:01:43Z")

</div>

Summary I’m new to Stan and having some success modeling with brms. This is mostly a Stan question with some brms. Thank you in advance for your patience! I’d like to fit a custom distribution that behaves like a zero-i…

---

## [Failed to Initialize Error (Log Probability Evaluates to Negative Infinity log(0))](https://discourse.mc-stan.org/t/failed-to-initialize-error-log-probability-evaluates-to-negative-infinity-log-0/34386)

<div class="topic-metadata">

**Author:** [@skylerjove20](https://discourse.mc-stan.org/u/skylerjove20)\
**Replies:** 7\
**Last updated:** [April 26, 2024, 9:05am UTC](https://discourse.mc-stan.org/t/failed-to-initialize-error-log-probability-evaluates-to-negative-infinity-log-0/34386 "2024-04-26T09:05:59Z")

</div>

Hoping to get some help understanding the source of this zero probability error. My model is relatively straightforward after understanding the purpose of my user-defined function. findx() takes two variable values (1)…

---

## [Identifier expected after sized type in local (or model block) variable declaration. (No transformations/constraints allowed.)](https://discourse.mc-stan.org/t/identifier-expected-after-sized-type-in-local-or-model-block-variable-declaration-no-transformations-constraints-allowed/34046)

<div class="topic-metadata">

**Author:** [@jgcolman](https://discourse.mc-stan.org/u/jgcolman)\
**Replies:** 2\
**Last updated:** [February 7, 2024, 10:09am UTC](https://discourse.mc-stan.org/t/identifier-expected-after-sized-type-in-local-or-model-block-variable-declaration-no-transformations-constraints-allowed/34046 "2024-02-07T10:09:21Z")

</div>

I am having trouble writing a user-defined function. Here is such a function that does work: functions{ array\[,\] real foo(real mu, real sigma){ array\[2,2\] real v; v\[1,1\] = sigma + mu;; v\[1,2\] = sigm…

---

## [Inverse cdf of a mixture of normal distributions](https://discourse.mc-stan.org/t/inverse-cdf-of-a-mixture-of-normal-distributions/32044)

<div class="topic-metadata">

**Author:** [@spencergordon](https://discourse.mc-stan.org/u/spencergordon)\
**Replies:** 1\
**Last updated:** [July 12, 2023, 5:56am UTC](https://discourse.mc-stan.org/t/inverse-cdf-of-a-mixture-of-normal-distributions/32044 "2023-07-12T05:56:45Z")

</div>

If I have a mixture distribution with two normal components like so wN(\\mu\_1, \\sigma^2\_1) + (1 - w)N(\\mu\_2, \\sigma^2\_2) it is relatively simple to calculate the pdf and the cdf of this distribution. I know that the cd…

---

## [Help Wanted: Custom Multivariate pdf For State Space Model](https://discourse.mc-stan.org/t/help-wanted-custom-multivariate-pdf-for-state-space-model/32020)

<div class="topic-metadata">

**Author:** [@PatrickStar](https://discourse.mc-stan.org/u/PatrickStar)\
**Replies:** 2\
**Last updated:** [July 9, 2023, 12:47am UTC](https://discourse.mc-stan.org/t/help-wanted-custom-multivariate-pdf-for-state-space-model/32020 "2023-07-09T00:47:58Z")

</div>

Hello, I’m trying to fit a state space model of the form; x\_t = Gx\_{t-1} + \\epsilon\_s y\_t = Fx\_t + \\epsilon\_o I would like to specify a user-defined PDF (multivariate) on the errors, \\epsilon\_s, \\epsilon\_o. Current…

---

## [How to call user defined functions arguments in the model?](https://discourse.mc-stan.org/t/how-to-call-user-defined-functions-arguments-in-the-model/31939)

<div class="topic-metadata">

**Author:** [@niro](https://discourse.mc-stan.org/u/niro)\
**Replies:** 2\
**Last updated:** [June 30, 2023, 6:45pm UTC](https://discourse.mc-stan.org/t/how-to-call-user-defined-functions-arguments-in-the-model/31939 "2023-06-30T18:45:09Z")

</div>

I have tried this function in many ways. The first problem I got is that in stan we can not use real values as indexing. So I tried to fix that using integer arrays. But now I can not run the model because I do not know …

---

## [Use predictor functions from other libraries with brms](https://discourse.mc-stan.org/t/use-predictor-functions-from-other-libraries-with-brms/29318)

<div class="topic-metadata">

**Author:** [@mikegee](https://discourse.mc-stan.org/u/mikegee)\
**Replies:** 2\
**Last updated:** [November 2, 2022, 4:14pm UTC](https://discourse.mc-stan.org/t/use-predictor-functions-from-other-libraries-with-brms/29318 "2022-11-02T16:14:32Z")

</div>

I suspect this question has been asked before, but I couldn’t find it after searching. I’m interested in using brms to fit several models provided by the rTPC package such as the following two: \> modifiedgaussian\_2006 …

---

## [User-defined recursive function error](https://discourse.mc-stan.org/t/user-defined-recursive-function-error/29073)

<div class="topic-metadata">

**Author:** [@ASvalova](https://discourse.mc-stan.org/u/ASvalova)\
**Replies:** 4\
**Last updated:** [October 9, 2022, 2:33pm UTC](https://discourse.mc-stan.org/t/user-defined-recursive-function-error/29073 "2022-10-09T14:33:17Z")

</div>

Dear reader, I am trying to fit a b-spline model, where I create a recursive function basis, however it doesn’t seem to be able to find itself or, potentially, go deep enough to reach the base case (degree=0) and solve…

---

## [Struggling with user-defined custom c++ function for binning real data into sequence of integer counts](https://discourse.mc-stan.org/t/struggling-with-user-defined-custom-c-function-for-binning-real-data-into-sequence-of-integer-counts/27448)

<div class="topic-metadata">

**Author:** [@wcc](https://discourse.mc-stan.org/u/wcc)\
**Replies:** 7\
**Last updated:** [May 16, 2022, 7:46am UTC](https://discourse.mc-stan.org/t/struggling-with-user-defined-custom-c-function-for-binning-real-data-into-sequence-of-integer-counts/27448 "2022-05-16T07:46:02Z")

</div>

In brief, I’m trying to write a custom c++ function that takes a vector of reals and returns an array of counts given some parameters for data binning (start, end, bin-width). I’m new to c++ and to Stan and will be using…

---

## [Custom beta quantile function stuck in iteration 1](https://discourse.mc-stan.org/t/custom-beta-quantile-function-stuck-in-iteration-1/26534)

<div class="topic-metadata">

**Author:** [@saratirvenracs](https://discourse.mc-stan.org/u/saratirvenracs)\
**Replies:** 6\
**Last updated:** [February 24, 2022, 11:37pm UTC](https://discourse.mc-stan.org/t/custom-beta-quantile-function-stuck-in-iteration-1/26534 "2022-02-24T23:37:50Z")

</div>

Hi, I am using the code below to elicit the posterior distribution of 5 parameters (4 beta distribution parameters and one mean-zero Gaussian error term)- (aBR, bBR), (aGP, bGP), sigma\_e (Error) that explain the choices …

---

## [How to estimate a truncated, user-defined distribution?](https://discourse.mc-stan.org/t/how-to-estimate-a-truncated-user-defined-distribution/25906)

<div class="topic-metadata">

**Author:** [@jollyjumper](https://discourse.mc-stan.org/u/jollyjumper)\
**Replies:** 2\
**Last updated:** [January 8, 2022, 6:02pm UTC](https://discourse.mc-stan.org/t/how-to-estimate-a-truncated-user-defined-distribution/25906 "2022-01-08T18:02:02Z")

</div>

Hello, I want to estimate the parameter of a truncated distribution that is not prebuilt in Stan. In order to understand the process, I follow Annis, Miller, Palmeri (2016) in their approach to user-define the Exponenti…

---

## [Implementing Quasi-Bayes with adaptive covariance estimation](https://discourse.mc-stan.org/t/implementing-quasi-bayes-with-adaptive-covariance-estimation/25644)

<div class="topic-metadata">

**Author:** [@PSchmidt](https://discourse.mc-stan.org/u/PSchmidt)\
**Replies:** 1\
**Last updated:** [December 17, 2021, 6:59pm UTC](https://discourse.mc-stan.org/t/implementing-quasi-bayes-with-adaptive-covariance-estimation/25644 "2021-12-17T18:59:46Z")

</div>

What is the best way to handle adaptive input in the log-likelihood specification? I am working on a Quasi-Bayesian/Laplace-type approach to Generalized Method of Moments estimation. The basic idea is to substitute the …

---

## [Asymmetric Laplace distribution](https://discourse.mc-stan.org/t/asymmetric-laplace-distribution/22750)

<div class="topic-metadata">

**Author:** [@JamieHogg-depo](https://discourse.mc-stan.org/u/JamieHogg-depo)\
**Replies:** 8\
**Last updated:** [May 28, 2021, 7:56am UTC](https://discourse.mc-stan.org/t/asymmetric-laplace-distribution/22750 "2021-05-28T07:56:38Z")

</div>

I am trying to implement M-quantile regression in Stan. I am aware that the optimization problem for quantile regression is equivalent to using the asymmetric Laplace distribution (skewed double exponential). I see that …

---

## [Help with custom probability distribution: Multivariate Power Exponential](https://discourse.mc-stan.org/t/help-with-custom-probability-distribution-multivariate-power-exponential/21612)

<div class="topic-metadata">

**Author:** [@cwolfe](https://discourse.mc-stan.org/u/cwolfe)\
**Replies:** 3\
**Last updated:** [April 6, 2021, 4:32pm UTC](https://discourse.mc-stan.org/t/help-with-custom-probability-distribution-multivariate-power-exponential/21612 "2021-04-06T16:32:44Z")

</div>

Hello! I am looking for assistance in coding a custom probability distribution. In particular, I am trying to use the multivariate generalization of the power exponential distribution defined in Gomez-Villegas and Marin…

---

## ["Variable does not exist" error for user-defined function in ode\_rk45](https://discourse.mc-stan.org/t/variable-does-not-exist-error-for-user-defined-function-in-ode-rk45/18960)

<div class="topic-metadata">

**Author:** [@zenkavi](https://discourse.mc-stan.org/u/zenkavi)\
**Replies:** 6\
**Last updated:** [October 29, 2020, 8:20pm UTC](https://discourse.mc-stan.org/t/variable-does-not-exist-error-for-user-defined-function-in-ode-rk45/18960 "2020-10-29T20:20:10Z")

</div>

Hi, I was trying to replicate the Lotka-Volterra case study as described here updating using the new ODE interface as described here. However at line vector\[2\] z\[N\] = ode\_rk45(dz\_dt, z\_init, 0, ts, alpha, beta, gamma, …

---

## [Simple harmonic oscillator ODE stan model from the Stan docs says function is not defined](https://discourse.mc-stan.org/t/simple-harmonic-oscillator-ode-stan-model-from-the-stan-docs-says-function-is-not-defined/18529)

<div class="topic-metadata">

**Author:** [@ctesta01](https://discourse.mc-stan.org/u/ctesta01)\
**Replies:** 2\
**Last updated:** [October 9, 2020, 3:15am UTC](https://discourse.mc-stan.org/t/simple-harmonic-oscillator-ode-stan-model-from-the-stan-docs-says-function-is-not-defined/18529 "2020-10-09T03:15:59Z")

</div>

Hi all, I’m trying to reproduce the Stan model from here: https://mc-stan.org/docs/2\_24/stan-users-guide/measurement-error-models.html I’ve saved the model as model.stan: functions { vector sho(real t, …

---

## [Getting environment variables](https://discourse.mc-stan.org/t/getting-environment-variables/17645)

<div class="topic-metadata">

**Author:** [@maedoc](https://discourse.mc-stan.org/u/maedoc)\
**Replies:** 8\
**Last updated:** [August 28, 2020, 10:15pm UTC](https://discourse.mc-stan.org/t/getting-environment-variables/17645 "2020-08-28T22:15:59Z")

</div>

Sometimes it’d be useful to pass integers or reals to a CmdStan program which are somewhat independent of the data set, e.g. grain size for reduce\_sum or rel\_tol for the ODE solver. I wonder what others think of the ide…

---

## [Error compiling own fuctions](https://discourse.mc-stan.org/t/error-compiling-own-fuctions/17527)

<div class="topic-metadata">

**Author:** [@hasibul212003](https://discourse.mc-stan.org/u/hasibul212003)\
**Replies:** 2\
**Last updated:** [August 18, 2020, 8:28pm UTC](https://discourse.mc-stan.org/t/error-compiling-own-fuctions/17527 "2020-08-18T20:28:10Z")

</div>

When trying to compile my own functions I am getting the following error: make: \*\*\* \[/usr/lib/R/etc/Makeconf:177: file18e76c0c9bb1.o\] Error 1 Error in Rcpp::sourceCpp(code = paste(code, collapse = “\\n”), …) : Error 1 …

---

## [Using integration to fit the difference of gamma distributed random variable](https://discourse.mc-stan.org/t/using-integration-to-fit-the-difference-of-gamma-distributed-random-variable/16039)

<div class="topic-metadata">

**Author:** [@bnicenboim](https://discourse.mc-stan.org/u/bnicenboim)\
**Replies:** 15\
**Last updated:** [July 4, 2020, 6:36am UTC](https://discourse.mc-stan.org/t/using-integration-to-fit-the-difference-of-gamma-distributed-random-variable/16039 "2020-07-04T06:36:48Z")

</div>

Hi, I’m trying to fit data that represents the difference between latent random variable that are gamma distributed (with the same rate). I’m obviously not succeeding. I generate my data like this in r. Basically I wa…

---

## [Syntax of if-else](https://discourse.mc-stan.org/t/syntax-of-if-else/16099)

<div class="topic-metadata">

**Author:** [@jgcolman](https://discourse.mc-stan.org/u/jgcolman)\
**Replies:** 3\
**Last updated:** [June 19, 2020, 2:16pm UTC](https://discourse.mc-stan.org/t/syntax-of-if-else/16099 "2020-06-19T14:16:03Z")

</div>

I have not been able to find a full statement of the syntax for if-else. My observation is that only certain kinds of statements are allowed after if, but what are they? If I write this user-defined function functions…

---

## [How to unpack a vector and avoid type mismatches in a user-defined function](https://discourse.mc-stan.org/t/how-to-unpack-a-vector-and-avoid-type-mismatches-in-a-user-defined-function/15979)

<div class="topic-metadata">

**Author:** [@jgcolman](https://discourse.mc-stan.org/u/jgcolman)\
**Replies:** 4\
**Last updated:** [June 16, 2020, 3:44pm UTC](https://discourse.mc-stan.org/t/how-to-unpack-a-vector-and-avoid-type-mismatches-in-a-user-defined-function/15979 "2020-06-16T15:44:33Z")

</div>

I am writing a user-defined function to take a vector of unknown length and to return a vector of the same length the elements of which are the result of applying a simple function to each element of the input. My probl…

---

## [Pystan with a user defined function (model)](https://discourse.mc-stan.org/t/pystan-with-a-user-defined-function-model/15498)

<div class="topic-metadata">

**Author:** [@kerenme](https://discourse.mc-stan.org/u/kerenme)\
**Replies:** 2\
**Last updated:** [June 9, 2020, 5:35pm UTC](https://discourse.mc-stan.org/t/pystan-with-a-user-defined-function-model/15498 "2020-06-09T17:35:45Z")

</div>

Dear Stan community, hello! I have a python agent based model named “flee” that I’m using to simulate the movement of agents from different nodes. I run the flee model for a specific scenario from the terminal via the f…
