# Generalized Chi-squared distribution

**URL:** <https://discourse.julialang.org/t/generalized-chi-squared-distribution/106055>\
**Category:** Statistics\
**Tags:** distributions\
**Created:** [November 10, 2023, 11:58pm UTC](https://discourse.julialang.org/t/generalized-chi-squared-distribution/106055 "2023-11-10T23:58:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [November 10, 2023, 11:58pm UTC](https://discourse.julialang.org/t/generalized-chi-squared-distribution/106055/1 "2023-11-10T23:58:41Z")

</div>

Does anybody know some attempt to implement the [Generalized Chi-squared distribution](https://en.wikipedia.org/wiki/Generalized_chi-squared_distribution) in Julia? (First I looked for it in [Distributions.jl](https://github.com/JuliaStats/Distributions.jl), but I have not found it there or anywhere else.)

---

<div class="post-metadata">

**Author:** ![nateybear](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nateybear/32/20645_2.png) [@nateybear](https://discourse.julialang.org/u/nateybear)\
**Post date:** [November 11, 2023, 2:17am UTC](https://discourse.julialang.org/t/generalized-chi-squared-distribution/106055/2 "2023-11-11T02:17:29Z")

</div>

Not that I’m aware of, but depending on what you want to use it for, a sampler should be easy to implement! If you want to go down the route of copying the [R code](https://github.com/cran/CompQuadForm/tree/master) to create a new [Univariate Distribution](https://juliastats.org/Distributions.jl/stable/extends/#Univariate-Distribution) I’m sure it would be much appreciated by the package maintainers 🙂

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [November 11, 2023, 5:42pm UTC](https://discourse.julialang.org/t/generalized-chi-squared-distribution/106055/3 "2023-11-11T17:42:35Z")

</div>

Thanks. The R library seems to depend on C++ code that is not easy for me, and is GPL-licensed, so maybe it’s not a good choice. The [Python library](https://github.com/limix/chi2comb-py/tree/master) is MIT-licensed, but it seems also dependent on C++ and only implements the CDF, if I’m not mistaken. The [Matlab library](https://github.com/abhranildas/gx2), which is also MIT-licensed, might be another option, I think.

---

<div class="post-metadata">

**Author:** ![ParadaCarleton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paradacarleton/32/20005_2.png) [@ParadaCarleton](https://discourse.julialang.org/u/ParadaCarleton)\
**Post date:** [November 11, 2023, 6:02pm UTC](https://discourse.julialang.org/t/generalized-chi-squared-distribution/106055/4 "2023-11-11T18:02:21Z")

</div>

If the Matlab is MIT licensed, I’d definitely go for that! You can also try out the [Matlab-to-Julia transpiler](https://lakras.github.io/matlab-to-julia/) to make your job a little bit easier, although do note that it’s pretty rough and only gives a “first draft” that you’ll probably have to edit.

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [November 27, 2023, 10:57pm UTC](https://discourse.julialang.org/t/generalized-chi-squared-distribution/106055/5 "2023-11-27T22:57:08Z")

</div>

I have followed your suggestion:

> <https://github.com/JuliaStats/Distributions.jl/pull/1806>
>
> This extends the collection of univariate continuous distributions with the Gene…ralized chi-squared. All required methods are added, and some of the recommended ones, as indicated in https://juliastats.org/Distributions.jl/stable/extends/#Univariate-Distribution
> 
> \*\*Some notes:\*\*
> 
> 1. I started considering \[Abhranil Das' code for Matlab\](https://github.com/abhranildas/gx2) (MIT-licensed) as reference, although progressively departed from it, so that eventually there is little in the Julia code that can be attributed to him. I have kept a comment where I mention the inspiration to define the \`cdf\` function, using Davies' algorithm to integrate the characteristic function. But I don't know if a more explicit attribution in the code or in the license should be made.
> 2. There are several auxiliary functions that should not be part of the API, and I have encapsulated all of them in the module \`GChisqComputations\`, after the example of the Chernoff distribution.
> 3. That module includes two functions to calculate the characteristic function: \`cf\_explicit\` that implements the explicit formula, and \`cf\_inherit\` that composes the results of \`cf(::Normal)\` and \`cf(::NoncentralChisq)\`. Both are equivalent, and the code of the second is clearer, but \`cf(::GeneralizedChisq)\` calls the first one, because in a few tests that I have done, it is slightly (but not much) faster.
> 4. \`cdf\` and \`pdf\` are calculated by integration, using the Gil-Pelaez theorem (the algorithm for the CDF is given in Davies' paper, and the algorithm for the PDF has been indirectly derived from it). The integration is done using QuadGK.jl, which was already in the dependencies of Distributions.jl. The default tolerances of \`QuadGK.quadgk\` made the calculation of \`cdf\` at the median very slow, because there the integral is zero. Since in the ends of the distribution the integral is \`±π/2\`, which is in the order of magnitude of the unit, I have used a fixed absolute tolerance of \`eps(one(T))\` for the integration in the calculation of the CDF. I have not, however, changed the defaults for the calculation of the PDF.
> 5. \`quantile\` is calculated using \`Distributions.quantile\_newton\`. However, there is no analytic solution to find the mode, which is the recommended starting point, so this is searched using the following strategy: (1) start at the mean of the distribution, and use it if it meets the convergence criteria; (2) otherwise define a bracket between that point and another at one standard deviation from the mean, in the direction towards the target probability (and if the bracket does not contain the target probability, extend it until it does); (3) bisect the bracket until one of the ends meets the convergence criteria, and then use that as the starting point. This seems to work fine, but it's a self-made algorithm, and perhaps there is a more efficient one that might be used instead.

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [July 26, 2025, 7:37am UTC](https://discourse.julialang.org/t/generalized-chi-squared-distribution/106055/6 "2025-07-26T07:37:25Z")

</div>

Update for anyone else who might be interested: I have created a separate package for the Generalized chi-squared distribution, since the PR hasn’t been accepted after over a year and a half (although not either rejected, just waiting for some review or decision, it seems to me).

> **[GitHub - heliosdrm/GeneralizedChisqDistribution.jl: Generalized Chi-squared Distribution for Julia](https://github.com/heliosdrm/GeneralizedChisqDistribution.jl)**
>
> Generalized Chi-squared Distribution for Julia

I have not registered this package, but [I’m open to discuss it](https://github.com/heliosdrm/GeneralizedChisqDistribution.jl/issues/1).
