# std::chi_squared_distribution

> C++ Atlas: `cppatlas.com/w/cpp/numeric/random/chi_squared_distribution` | Source: https://en.cppreference.com/w/cpp/numeric/random/chi_squared_distribution | Domain: Numerics | Standards: C++11, C++20

The chi_squared_distribution produces random numbers x>0 according to the Chi-squared distribution:

## Official Specification Summary (cppreference)

```cpp
// Defined in header <random>

template< class RealType = double >
class chi_squared_distribution; // (since C++11)
```

The `chi_squared_distribution` produces random numbers x>0 according to the Chi-squared distribution:

  f(x;n) = x^((n/2)-1) *e*-x/2
  Γ(n/2) 2^(n/2)

Γ is the Gamma function (See also `std::tgamma`) and n are the degrees of freedom (default 1).

`std::chi_squared_distribution` satisfies all requirements of *RandomNumberDistribution*.

### Template parameters

- `RealType` - The result type generated by the generator. The effect is undefined if this is not one of `float`, `double`, or `long double`.

### Member types

**Member type — Definition**

- `result_type` (C++11): `RealType`
- `param_type` (C++11): the type of the parameter set, see *RandomNumberDistribution*.

### Member functions

- (constructor) (C++11): constructs new distribution (public member function)
- `reset` (C++11): resets the internal state of the distribution (public member function)

**Generation**

- `operator()` (C++11): generates the next random number in the distribution (public member function)

**Characteristics**

- `n` (C++11): returns the degrees of freedom (n) distribution parameter (public member function)
- `param` (C++11): gets or sets the distribution parameter object (public member function)
- `min` (C++11): returns the minimum potentially generated value (public member function)
- `max` (C++11): returns the maximum potentially generated value (public member function)

### Non-member functions

- `operator==` (C++11), `operator!=` (C++11)(removed in C++20): compares two distribution objects (function)
- `operator<<` (C++11), `operator>>`: performs stream input and output on pseudo-random number distribution (function template)

### Example

```cpp
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <random>
#include <vector>

template<int Height = 5, int BarWidth = 1, int Padding = 1, int Offset = 0, class Seq>
void draw_vbars(Seq&& s, const bool DrawMinMax = true)
{
    static_assert(0 < Height and 0 < BarWidth and 0 <= Padding and 0 <= Offset);

    auto cout_n = [](auto&& v, int n = 1)
    {
        while (n-- > 0)
            std::cout << v;
    };

    const auto [min, max] = std::minmax_element(std::cbegin(s), std::cend(s));

    std::vector<std::div_t> qr;
    for (typedef decltype(*std::cbegin(s)) V; V e : s)
        qr.push_back(std::div(std::lerp(V(0), 8 * Height,
                                        (e - *min) / (*max - *min)), 8));

    for (auto h{Height}; h-- > 0; cout_n('\n'))
    {
        cout_n(' ', Offset);

        for (auto dv : qr)
        {
            const auto q{dv.quot}, r{dv.rem};
            unsigned char d[]{0xe2, 0x96, 0x88, 0}; // Full Block: '█'
            q < h ? d[0] = ' ', d[1] = 0 : q == h ? d[2] -= (7 - r) : 0;
            cout_n(d, BarWidth), cout_n(' ', Padding);
        }

        if (DrawMinMax && Height > 1)
            Height - 1 == h ? std::cout << "┬ " << *max:
                          h ? std::cout << "│ "
                            : std::cout << "┴ " << *min;
    }
}

int main()
{
    std::random_device rd{};
    std::mt19937 gen{rd()};

    auto χ2 = [&gen](const float dof)
    {
        std::chi_squared_distribution<float> d{dof /* n */};

        const int norm = 1'00'00;
        const float cutoff = 0.002f;

        std::map<int, int> hist{};
        for (int n = 0; n != norm; ++n)
            ++hist[std::round(d(gen))];

        std::vector<float> bars;
        std::vector<int> indices;
        for (auto const& [n, p] : hist)
            if (float x = p * (1.0 / norm); cutoff < x)
            {
                bars.push_back(x);
                indices.push_back(n);
            }

        std::cout << "dof = " << dof << ":\n";

        for (draw_vbars<4, 3>(bars); int n : indices)
            std::cout << std::setw(2) << n << "  ";
        std::cout << "\n\n";
    };

    for (float dof : {1.f, 2.f, 3.f, 4.f, 6.f, 9.f})
        χ2(dof);
}
```

Possible output:

```text
dof = 1:
███                                 ┬ 0.5271
███                                 │
███ ███                             │
███ ███ ▇▇▇ ▃▃▃ ▂▂▂ ▁▁▁ ▁▁▁ ▁▁▁ ▁▁▁ ┴ 0.003
 0   1   2   3   4   5   6   7   8

dof = 2:
    ███                                     ┬ 0.3169
▆▆▆ ███ ▃▃▃                                 │
███ ███ ███ ▄▄▄                             │
███ ███ ███ ███ ▇▇▇ ▄▄▄ ▃▃▃ ▂▂▂ ▁▁▁ ▁▁▁ ▁▁▁ ┴ 0.004
 0   1   2   3   4   5   6   7   8   9  10

dof = 3:
    ███ ▃▃▃                                         ┬ 0.2439
    ███ ███ ▄▄▄                                     │
▃▃▃ ███ ███ ███ ▇▇▇ ▁▁▁                             │
███ ███ ███ ███ ███ ███ ▆▆▆ ▄▄▄ ▃▃▃ ▂▂▂ ▁▁▁ ▁▁▁ ▁▁▁ ┴ 0.0033
 0   1   2   3   4   5   6   7   8   9  10  11  12

dof = 4:
    ▂▂▂ ███ ▃▃▃                                                 ┬ 0.1864
    ███ ███ ███ ███ ▂▂▂                                         │
    ███ ███ ███ ███ ███ ▅▅▅ ▁▁▁                                 │
▅▅▅ ███ ███ ███ ███ ███ ███ ███ ▆▆▆ ▄▄▄ ▃▃▃ ▂▂▂ ▂▂▂ ▁▁▁ ▁▁▁ ▁▁▁ ┴ 0.0026
 0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15

dof = 6:
            ▅▅▅ ▇▇▇ ███ ▂▂▂                                                 ┬ 0.1351
        ▅▅▅ ███ ███ ███ ███ ▇▇▇ ▁▁▁                                         │
    ▁▁▁ ███ ███ ███ ███ ███ ███ ███ ▅▅▅ ▂▂▂                                 │
▁▁▁ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ▅▅▅ ▄▄▄ ▃▃▃ ▂▂▂ ▁▁▁ ▁▁▁ ▁▁▁ ┴ 0.0031
 0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18

dof = 9:
            ▅▅▅ ▇▇▇ ███ ███ ▄▄▄ ▂▂▂                                                 ┬ 0.1044
        ▃▃▃ ███ ███ ███ ███ ███ ███ ▅▅▅ ▁▁▁                                         │
    ▄▄▄ ███ ███ ███ ███ ███ ███ ███ ███ ███ ▆▆▆ ▃▃▃                                 │
▄▄▄ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ███ ▆▆▆ ▄▄▄ ▃▃▃ ▂▂▂ ▁▁▁ ▁▁▁ ▁▁▁ ┴ 0.0034
 2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22
```

### External links

|   |   |
| --- | --- |
| 1. | Weisstein, Eric W. "Chi-Squared Distribution." From MathWorld — A Wolfram Web Resource. |
| 2. | Chi-squared distribution — From Wikipedia. |

---
Attribution: content from https://en.cppreference.com/w/cpp/numeric/random/chi_squared_distribution (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.