# \#stan-math

**URL:** https://discourse.mc-stan.org/tag/stan-math/21.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)

---

## [Stan Math library in R package, installation error on Windows](https://discourse.mc-stan.org/t/stan-math-library-in-r-package-installation-error-on-windows/38474)

<div class="topic-metadata">

**Author:** [@heogden](https://discourse.mc-stan.org/u/heogden)\
**Replies:** 3\
**Last updated:** [January 13, 2025, 1:48pm UTC](https://discourse.mc-stan.org/t/stan-math-library-in-r-package-installation-error-on-windows/38474 "2025-01-13T13:48:06Z")

</div>

Hi all, I am trying to use Stan Math C++ library in an R package (for automatic differentiation). It is working fine on Linux, but Windows users reported an error installing the package. I made a simpler package stanmat…

---

## [Most Numerically Stable Way to Calculate log\_Phi](https://discourse.mc-stan.org/t/most-numerically-stable-way-to-calculate-log-phi/37278)

<div class="topic-metadata">

**Author:** [@eweine](https://discourse.mc-stan.org/u/eweine)\
**Replies:** 8\
**Last updated:** [November 18, 2024, 4:02pm UTC](https://discourse.mc-stan.org/t/most-numerically-stable-way-to-calculate-log-phi/37278 "2024-11-18T16:02:49Z")

</div>

Hey all, I need to calculate \\log(\\Phi(y)) in a numerically stable way (where \\Phi is the CDF of a standard normal distribution), where y may be a very large or small number. What is the best way to do this in stan math…

---

## [Issue getting Hessian with stan math](https://discourse.mc-stan.org/t/issue-getting-hessian-with-stan-math/36602)

<div class="topic-metadata">

**Author:** [@eweine](https://discourse.mc-stan.org/u/eweine)\
**Replies:** 1\
**Last updated:** [September 17, 2024, 2:43pm UTC](https://discourse.mc-stan.org/t/issue-getting-hessian-with-stan-math/36602 "2024-09-17T14:43:54Z")

</div>

Hey all, I’m having trouble getting a hang of the autodiff capabilities of stan math. I’m attempting to derive the gradient and the Hessian of the function f(L) = Tr(LL^{T}A), where A is a known matrix and L is a lower…

---

## [Contributing code to Stan](https://discourse.mc-stan.org/t/contributing-code-to-stan/33746)

<div class="topic-metadata">

**Author:** [@chvandorp](https://discourse.mc-stan.org/u/chvandorp)\
**Replies:** 3\
**Last updated:** [January 16, 2024, 3:39pm UTC](https://discourse.mc-stan.org/t/contributing-code-to-stan/33746 "2024-01-16T15:39:00Z")

</div>

Hi all, I recently submitted a new distribution to the Stan codebase (the Dirichlet-multinomial distribution). I wrote down my experiences in a blog post. This is definitely not a tutorial, and I am not an expert at all…

---

## [How to use the specific stan math branches function in rstan?](https://discourse.mc-stan.org/t/how-to-use-the-specific-stan-math-branches-function-in-rstan/33117)

<div class="topic-metadata">

**Author:** [@swg](https://discourse.mc-stan.org/u/swg)\
**Replies:** 2\
**Last updated:** [November 2, 2023, 7:53am UTC](https://discourse.mc-stan.org/t/how-to-use-the-specific-stan-math-branches-function-in-rstan/33117 "2023-11-02T07:53:30Z")

</div>

Short summary of the problem: Operating System: windows 11 Interface Version: rstan 2.32.3 Compiler/Toolkit: rtools 43 I want to use the wiener\_full\_lpdf function in rstan, when I run my code in rstudio, it returns …

---

## [Wrap boost::math::chebyshev\_transform into stan::var?](https://discourse.mc-stan.org/t/wrap-boost-chebyshev-transform-into-stan-var/33109)

<div class="topic-metadata">

**Author:** [@sjvrensburg](https://discourse.mc-stan.org/u/sjvrensburg)\
**Replies:** 2\
**Last updated:** [November 2, 2023, 6:10am UTC](https://discourse.mc-stan.org/t/wrap-boost-chebyshev-transform-into-stan-var/33109 "2023-11-02T06:10:39Z")

</div>

I CLEARLY NEED MORE SLEEP… Obviously, zstr.adj() is not going to give me what I want, I’m supposed to access r.adj() Sorry, everyone! Hi! I am approximating a univariate function (not available in closed-form) with Boos…

---

## [.grad() with Eigen types](https://discourse.mc-stan.org/t/grad-with-eigen-types/32533)

<div class="topic-metadata">

**Author:** [@drezap](https://discourse.mc-stan.org/u/drezap)\
**Replies:** 1\
**Last updated:** [August 24, 2023, 5:15am UTC](https://discourse.mc-stan.org/t/grad-with-eigen-types/32533 "2023-08-24T05:15:56Z")

</div>

I should be able to do something like the following, correct? Given some f that we can suppose worked with std::vector types: Eigen::Matrix\<stan::math::var, -1, 1\> x; stan::math::var fx; fx = stan::math::f(x); Eigen::Ma…

---

## [Function Signature for stan::math::hessian, first arg](https://discourse.mc-stan.org/t/function-signature-for-stan-hessian-first-arg/31341)

<div class="topic-metadata">

**Author:** [@drezap](https://discourse.mc-stan.org/u/drezap)\
**Replies:** 5\
**Last updated:** [June 14, 2023, 6:52am UTC](https://discourse.mc-stan.org/t/function-signature-for-stan-hessian-first-arg/31341 "2023-06-14T06:52:23Z")

</div>

What’s the function signature need to look like for the first argument of stan::math::hessian? I have a signature like: template \<typename T\_x\> inline const return\_type\_t\<T\_x\> f2(const std::vector\<T\_x\> &x) and t…

---

## [Segfault, C++, autodiff](https://discourse.mc-stan.org/t/segfault-c-autodiff/31317)

<div class="topic-metadata">

**Author:** [@drezap](https://discourse.mc-stan.org/u/drezap)\
**Replies:** 2\
**Last updated:** [May 8, 2023, 4:17pm UTC](https://discourse.mc-stan.org/t/segfault-c-autodiff/31317 "2023-05-08T16:17:39Z")

</div>

Quick question… I’m segfaulting somewhere, and I’d like this to work. I have a debugger open right now, but it’s not very helpful. What can I use/do to see exactly what my issue is? Use Valgrind or some memory analysis …

---

## [Stan Math Library with a Debugger](https://discourse.mc-stan.org/t/stan-math-library-with-a-debugger/30885)

<div class="topic-metadata">

**Author:** [@drezap](https://discourse.mc-stan.org/u/drezap)\
**Replies:** 1\
**Last updated:** [March 28, 2023, 1:58pm UTC](https://discourse.mc-stan.org/t/stan-math-library-with-a-debugger/30885 "2023-03-28T13:58:20Z")

</div>

I have some code built on top of the Stan/math library with unit tests in gtest that I need to run a debugger on. I’m wondering if anyone’s figured out how to use GDB with google test on top of the stan math library? T…

---

## [Building Stan math library stand alone, centOS](https://discourse.mc-stan.org/t/building-stan-math-library-stand-alone-centos/29866)

<div class="topic-metadata">

**Author:** [@drezap](https://discourse.mc-stan.org/u/drezap)\
**Replies:** 21\
**Last updated:** [March 21, 2023, 1:18pm UTC](https://discourse.mc-stan.org/t/building-stan-math-library-stand-alone-centos/29866 "2023-03-21T13:18:43Z")

</div>

Yeah, just trying to get the stan math library to compile on a centOS server. It worked, then it didn’t. Pretty sure the issue is setting the c++14 compiler? Cross posting this from a github thread: Able to install and…

---

## [Any guidance for how to use Stan math library autodiff? (new to C++)](https://discourse.mc-stan.org/t/any-guidance-for-how-to-use-stan-math-library-autodiff-new-to-c/30394)

<div class="topic-metadata">

**Author:** [@CerulloE](https://discourse.mc-stan.org/u/CerulloE)\
**Replies:** 9\
**Last updated:** [March 17, 2023, 6:27pm UTC](https://discourse.mc-stan.org/t/any-guidance-for-how-to-use-stan-math-library-autodiff-new-to-c/30394 "2023-03-17T18:27:57Z")

</div>

I am working on implementing a HMC-within-Gibbs algorithm for a model I am working on\*\* and am coding it using R and C++ (via Rcpp). For the HMC part I started off using numerical differentiation to calculate the gradien…

---

## [How do define a hazard function in rstan](https://discourse.mc-stan.org/t/how-do-define-a-hazard-function-in-rstan/30265)

<div class="topic-metadata">

**Author:** [@evihas](https://discourse.mc-stan.org/u/evihas)\
**Replies:** 2\
**Last updated:** [February 4, 2023, 10:14pm UTC](https://discourse.mc-stan.org/t/how-do-define-a-hazard-function-in-rstan/30265 "2023-02-04T22:14:03Z")

</div>

Hi everyone, I am trying to write a rstan code for parameter calibration. In my calibration model, there is a hazard function h(t) which uses to derive the age t. This model comes from the paper MILC: A microsimulation …

---

## [Stan::math's quad\_form and Eigen matrix multiplication](https://discourse.mc-stan.org/t/stan-maths-quad-form-and-eigen-matrix-multiplication/30598)

<div class="topic-metadata">

**Author:** [@jmh530](https://discourse.mc-stan.org/u/jmh530)\
**Replies:** 4\
**Last updated:** [March 4, 2023, 3:55am UTC](https://discourse.mc-stan.org/t/stan-maths-quad-form-and-eigen-matrix-multiplication/30598 "2023-03-04T03:55:49Z")

</div>

I have a question regarding the stan::math implementation of quad\_form What interests me - in the matrix-matrix version - is the return b.transpose() \* a \* b; return part of what looks to be something like a C++ lambda …

---

## [Incorrect formula for correlation matrix transform](https://discourse.mc-stan.org/t/incorrect-formula-for-correlation-matrix-transform/24664)

<div class="topic-metadata">

**Author:** [@hhau](https://discourse.mc-stan.org/u/hhau)\
**Replies:** 6\
**Last updated:** [March 3, 2023, 7:23am UTC](https://discourse.mc-stan.org/t/incorrect-formula-for-correlation-matrix-transform/24664 "2023-03-03T07:23:38Z")

</div>

I am mapping a set of unconstrained values to a correlation matrix and working off 10.9 Correlation matrices | Stan Reference Manual in the manual (for which I am very grateful, as the LKJ papers are not at all easy to u…

---

## [Questing regarding implementing a new function using existing Stan functions](https://discourse.mc-stan.org/t/questing-regarding-implementing-a-new-function-using-existing-stan-functions/28697)

<div class="topic-metadata">

**Author:** [@mathDR](https://discourse.mc-stan.org/u/mathDR)\
**Replies:** 7\
**Last updated:** [November 10, 2022, 5:10pm UTC](https://discourse.mc-stan.org/t/questing-regarding-implementing-a-new-function-using-existing-stan-functions/28697 "2022-11-10T17:10:49Z")

</div>

Hello. I am working on adding a new function to the stan-math library and had a question regarding usage of existing templated Stan functions. If we are suppling gradients/hessians for our function, is it better to “un…

---

## [Trouble building Stan Math Library](https://discourse.mc-stan.org/t/trouble-building-stan-math-library/29309)

<div class="topic-metadata">

**Author:** [@drezap](https://discourse.mc-stan.org/u/drezap)\
**Replies:** 3\
**Last updated:** [November 2, 2022, 7:02pm UTC](https://discourse.mc-stan.org/t/trouble-building-stan-math-library/29309 "2022-11-02T19:02:57Z")

</div>

Dear all, I’m trying to build the Stan math library alone on a linux machine, using the command at the following tutorial: Stan Math Library: Stan Math Library Docs (mc-stan.org). Running command in the second block of …

---

## [Converting Stan data types to C/C++ primative types](https://discourse.mc-stan.org/t/converting-stan-data-types-to-c-c-primative-types/28944)

<div class="topic-metadata">

**Author:** [@matthew\_ay](https://discourse.mc-stan.org/u/matthew_ay)\
**Replies:** 4\
**Last updated:** [September 29, 2022, 5:12am UTC](https://discourse.mc-stan.org/t/converting-stan-data-types-to-c-c-primative-types/28944 "2022-09-29T05:12:06Z")

</div>

Hello all, Thanks to @WardBrian’s help on a previous post, I was able to connect an external header file to my Stan model during compilation with Cmdstan. However, I am running into an issue when trying to integrate my…

---

## [Marginalizing a double binomial-Poisson hierarchical distribution](https://discourse.mc-stan.org/t/marginalizing-a-double-binomial-poisson-hierarchical-distribution/27801)

<div class="topic-metadata">

**Author:** [@Matthew\_Spencer1](https://discourse.mc-stan.org/u/Matthew_Spencer1)\
**Replies:** 7\
**Last updated:** [June 16, 2022, 6:08pm UTC](https://discourse.mc-stan.org/t/marginalizing-a-double-binomial-poisson-hierarchical-distribution/27801 "2022-06-16T18:08:44Z")

</div>

I want to fit a model involving a binomial-Poisson hiearchical distribution with two sets of conditionally independent binomial counts: y\_i \\sim \\text{Poisson}(\\lambda\_i), z\_{1, i} \\sim \\text{binomial}(y\_i, \\kappa), z…

---

## [Call Stan-math gradient() from separate threads =\> segfault](https://discourse.mc-stan.org/t/call-stan-math-gradient-from-separate-threads-segfault/27458)

<div class="topic-metadata">

**Author:** [@roualdes](https://discourse.mc-stan.org/u/roualdes)\
**Replies:** 10\
**Last updated:** [May 16, 2022, 4:21pm UTC](https://discourse.mc-stan.org/t/call-stan-math-gradient-from-separate-threads-segfault/27458 "2022-05-16T16:21:49Z")

</div>

tl;dr Calling a C program, which uses stan::math::gradient, on separate threads from Julia segfaults and I don’t understand why, while a similar program (called similarly) without stan::math::gradient runs fine. Does an…

---

## [How to use function 'eigenvalues' from the stan math library](https://discourse.mc-stan.org/t/how-to-use-function-eigenvalues-from-the-stan-math-library/17111)

<div class="topic-metadata">

**Author:** [@alex\_cab](https://discourse.mc-stan.org/u/alex_cab)\
**Replies:** 7\
**Last updated:** [May 10, 2022, 5:27pm UTC](https://discourse.mc-stan.org/t/how-to-use-function-eigenvalues-from-the-stan-math-library/17111 "2022-05-10T17:27:18Z")

</div>

Dear community I am kind of new in Stan. I need to calculate eigenvalues and eigenvectors of non-symmetric matrices. I saw that there are two functions in the stan math library called ‘eigenvalues’ and ‘eigenvectors’. I…

---

## [Aberrant behavior in dynamically bounded parameters, reparameterized GEV distribution](https://discourse.mc-stan.org/t/aberrant-behavior-in-dynamically-bounded-parameters-reparameterized-gev-distribution/22726)

<div class="topic-metadata">

**Author:** [@danibarna](https://discourse.mc-stan.org/u/danibarna)\
**Replies:** 5\
**Last updated:** [May 3, 2022, 2:45pm UTC](https://discourse.mc-stan.org/t/aberrant-behavior-in-dynamically-bounded-parameters-reparameterized-gev-distribution/22726 "2022-05-03T14:45:31Z")

</div>

The support of the GEV distribution changes dependent on the value of the shape parameter: \\xi \> 0: \\hspace{1cm} y \\in \\left\[\\mu - \\frac{\\sigma}{\\xi}, +\\infty\\right) \\hspace{0.25cm} \\implies \\hspace{0.25cm} \\text{min(y)…

---

## [HMC: Does precision of derivatives affect validity of results?](https://discourse.mc-stan.org/t/hmc-does-precision-of-derivatives-affect-validity-of-results/27147)

<div class="topic-metadata">

**Author:** [@valmapra](https://discourse.mc-stan.org/u/valmapra)\
**Replies:** 4\
**Last updated:** [April 26, 2022, 10:15pm UTC](https://discourse.mc-stan.org/t/hmc-does-precision-of-derivatives-affect-validity-of-results/27147 "2022-04-26T22:15:29Z")

</div>

Hi all, We’re currently tuning a LPDF where calculating the (manually coded) derivatives is quite computationally expensive (i.e doing numerical integration). Until now, I haven’t noticed a real difference in the result…

---

## [Understanding the Stan code base - A blog post series](https://discourse.mc-stan.org/t/understanding-the-stan-code-base-a-blog-post-series/25517)

<div class="topic-metadata">

**Author:** [@jtimonen](https://discourse.mc-stan.org/u/jtimonen)\
**Replies:** 1\
**Last updated:** [January 14, 2022, 4:36am UTC](https://discourse.mc-stan.org/t/understanding-the-stan-code-base-a-blog-post-series/25517 "2022-01-14T04:36:59Z")

</div>

I have for some time wanted to understand the rather large C++ source code base of Stan. I created a blog post about studying it at https://jtimonen.github.io/posts/post-01/ and it is supposed to be part of a series that…

---

## ["Undefined control sequence \\operatorname" error in stan math doxygen output](https://discourse.mc-stan.org/t/undefined-control-sequence-operatorname-error-in-stan-math-doxygen-output/23974)

<div class="topic-metadata">

**Author:** [@ssog](https://discourse.mc-stan.org/u/ssog)\
**Replies:** 3\
**Last updated:** [August 20, 2021, 9:41am UTC](https://discourse.mc-stan.org/t/undefined-control-sequence-operatorname-error-in-stan-math-doxygen-output/23974 "2021-08-20T09:41:19Z")

</div>

Hi, all I built the API manual got an “Undefined control sequence \\operatorname” error. I built the manuls in new ubuntu environment. Before “make doxygen”, I did the following command sudo apt install doxygen texl…

---

## [How to use log\_sum\_exp in C++ without stan::math::var?](https://discourse.mc-stan.org/t/how-to-use-log-sum-exp-in-c-without-stan-var/23695)

<div class="topic-metadata">

**Author:** [@ADFun](https://discourse.mc-stan.org/u/ADFun)\
**Replies:** 3\
**Last updated:** [August 1, 2021, 11:25am UTC](https://discourse.mc-stan.org/t/how-to-use-log-sum-exp-in-c-without-stan-var/23695 "2021-08-01T11:25:23Z")

</div>

I am trying to use the log\_sum\_exp function in my Rcpp function, but cannot compile the .cpp file using sourceCpp unless I use the stan::math::var type. Example .cpp file as follows: // \[\[Rcpp::depends(rstan)\]\] #inclu…

---

## [Cryptic error message:Expression: index \>= 0 && index \< size() - assertion failed - Solved](https://discourse.mc-stan.org/t/cryptic-error-message-index-0-index-size-assertion-failed-solved/18826)

<div class="topic-metadata">

**Author:** [@GustafRydevik](https://discourse.mc-stan.org/u/GustafRydevik)\
**Replies:** 3\
**Last updated:** [October 23, 2020, 2:02pm UTC](https://discourse.mc-stan.org/t/cryptic-error-message-index-0-index-size-assertion-failed-solved/18826 "2020-10-23T14:02:47Z")

</div>

Sorry - I seem to spam the fora today. I think I’m nearly there with my hierarchical ODE model, but not sure how to start debugging this error message (generated by cmdstan): Chain 1 Assertion failed! Chain 1 Chain 1 …

---

## [Handling symmetric matrices](https://discourse.mc-stan.org/t/handling-symmetric-matrices/18302)

<div class="topic-metadata">

**Author:** [@syclik](https://discourse.mc-stan.org/u/syclik)\
**Replies:** 6\
**Last updated:** [October 2, 2020, 8:49pm UTC](https://discourse.mc-stan.org/t/handling-symmetric-matrices/18302 "2020-10-02T20:49:46Z")

</div>

\[This is a continuation of the discussion on GitHub in a PR. It made sense to start discussing it on a forum instead of a closed PR. Start of the discussion: stan-dev/math#2096\] @bbbales2 and I were talking about symmet…

---

## [Monthly Math development meeting: 08/20/2020, 10 am EDT](https://discourse.mc-stan.org/t/monthly-math-development-meeting-08-20-2020-10-am-edt/17534)

<div class="topic-metadata">

**Author:** [@syclik](https://discourse.mc-stan.org/u/syclik)\
**Replies:** 8\
**Last updated:** [August 20, 2020, 3:02pm UTC](https://discourse.mc-stan.org/t/monthly-math-development-meeting-08-20-2020-10-am-edt/17534 "2020-08-20T15:02:59Z")

</div>

Hey all, We have our monthly Math meetings on the third Thursday of the month. The next Math meeting is Thursday, August 20, 2020, 10 am Eastern time. This will last no more than one hour; if we need more time, we will…

---

## [Monthly Math development meeting: 05/21/2020](https://discourse.mc-stan.org/t/monthly-math-development-meeting-05-21-2020/14368)

<div class="topic-metadata">

**Author:** [@syclik](https://discourse.mc-stan.org/u/syclik)\
**Replies:** 8\
**Last updated:** [May 21, 2020, 2:51pm UTC](https://discourse.mc-stan.org/t/monthly-math-development-meeting-05-21-2020/14368 "2020-05-21T14:51:00Z")

</div>

Hey all, We have our monthly Math meetings on the third Thursday of the month. The next Math meeting is Thursday, May 21, 2020, 10 am Eastern time. This will last no more than one hour; if we need more time, we will sch…

[Next page](https://discourse.mc-stan.org/tag/stan-math/21.md?match_all_tags=true&page=1&tags%5B%5D=stan-math)
