# Probability & Combinatorics functions

CalcTree includes built-in support for common statistical counting functions and probability-related tools via [MathJS](https://mathjs.org/docs/reference/functions.html#probability). These functions help with uncertainty analysis, risk assessment, and combinatorial calculations.

### Combinatorics Functions

These functions return counts of arrangements, selections, and partitions.

| Function                    | Description                                             | CalcTree Example                  |
| --------------------------- | ------------------------------------------------------- | --------------------------------- |
| `factorial(n)`              | n! — factorial of a number                              | `factorial(5)` → `120`            |
| `combinations(n, k)`        | Ways to choose k unordered items from n (no repetition) | `combinations(5, 2)` → `10`       |
| `combinationsWithRep(n, k)` | Choose k unordered items with repetition                | `combinationsWithRep(3, 2)` → `6` |
| `permutations(n [, k])`     | Ordered subsets of length k (default is n!)             | `permutations(4, 2)` → `12`       |
| `multinomial([a1, a2, …])`  | Ways to divide a set into subgroups of given sizes      | `multinomial([2, 1])` → `3`       |
| `bellNumbers(n)`            | Count of set partitions                                 | `bellNumbers(3)` → `5`            |
| `catalan(n)`                | Number of valid bracketings, rooted trees, etc.         | `catalan(3)` → `5`                |
| `composition(n, k)`         | Ways to write n as a sum of k positive integers         | `composition(5, 2)` → `4`         |
| `stirlingS2(n, k)`          | Stirling numbers of the second kind — set partitions    | `stirlingS2(4, 2)` → `7`          |

### Probability Functions

These functions support basic probability distributions and randomness.

| Function              | Description                                           | CalcTree Example                       |
| --------------------- | ----------------------------------------------------- | -------------------------------------- |
| `gamma(n)`            | Gamma function (generalization of factorial)          | `gamma(6)` → `120`                     |
| `lgamma(n)`           | Logarithm of gamma function                           | `lgamma(6)` → `~4.787...`              |
| `kldivergence(x, y)`  | Kullback-Leibler divergence between two distributions | `kldivergence([0.4, 0.6], [0.5, 0.5])` |
| `random()`            | Random number between 0 and 1 (uniform)               | `random()` → varies                    |
| `random(min, max)`    | Random number in given range                          | `random(10, 20)` → varies              |
| `randomInt(min, max)` | Random integer in range                               | `randomInt(1, 6)` → like a dice roll   |
| `pickRandom(arr)`     | Randomly select one or more values from a list        | `pickRandom([1, 2, 3])` → `2`          |

### Notes for CalcTree

* You can use `random()` in param formulas, but values will refresh on each evaluation.
* Combinatoric functions return exact values, useful for reliability and arrangement modeling.
* Use `gamma(n + 1)` as an alternative to `factorial(n)` for non-integers.

{% hint style="info" %}
📘 **Looking for more functions?**\
CalcTree’s expression engine is powered by [MathJS](https://mathjs.org/docs/reference/functions.html).\
For a full list of available functions, visit the [MathJS Function Reference](https://mathjs.org/docs/reference/functions.html).\
Most functions listed there are supported in CalcTree unless otherwise noted.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://calctree.gitbook.io/docs/calculations/creating-calculations/maths-equations/native-functions/probability-and-combinatorics-functions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
