# Variance, Moments, and Moment-Generating Intuition

`D00-F06-A08` · Financial Mathematics, Statistics, and Data Foundations → Probability and Random Variables · archetype `record-transform` · difficulty 1/5 · verification **verified**

Full page: https://docs.thefintechbuilder.com/foundations/probability-and-random-variables/variance-moments-and-moment-generating-intuition/
Agent skill: `npx skills add IslamBaraka90/Fintech-Algorithms-Library` — https://docs.thefintechbuilder.com/guides/agent-skill/

## Install and import

```bash
npm install fintech-algorithms
```

```ts
import { varianceMomentsAndMomentGeneratingIntuition } from "fintech-algorithms/foundations/probability-and-random-variables/variance-moments-and-moment-generating-intuition";
```

## Signature

```ts
varianceMomentsAndMomentGeneratingIntuition(input)
```

Computes the first two raw moments of a discrete random variable and derives its variance as the second raw moment less the square of the first.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `input` | `{ pA: number; pB: number; pAB: number; randomValues: number[]; probabilities: number[] }` | yes | `randomValues` is the support and `probabilities` the mass on each point, position by position. The family-wide `pA`, `pB`, and `pAB` are validated on every call even though this topic does not use their values. · pA: 0 <= pA <= 1, pB: 0 <= pB <= 1, pAB: 0 <= pAB <= min(pA, pB), probabilities: same length as `randomValues`, every entry nonnegative, summing to 1 within 1e-12 |

## Returns

`{ firstRawMoment: number; secondRawMoment: number; variance: number }`

`firstRawMoment` is the expected value, `secondRawMoment` is the probability-weighted sum of squares, and `variance` is `secondRawMoment - firstRawMoment ** 2`.

## Errors

- When `input` is null, an array, or not an object — throws TypeError
- When `pA`, `pB`, or `pAB` falls outside [0, 1], or `pAB` exceeds `min(pA, pB)` — throws RangeError
- When `randomValues` or `probabilities` is missing, empty, or holds a non-finite entry — throws RangeError
- When `randomValues` and `probabilities` differ in length, a probability is negative, or the probabilities do not sum to one within 1e-12 — throws RangeError

## Complexity

Time `O(n)`, space `O(n)`.

## Worked example

This is the worked example published in the article, replayed by the test suite on every run. The output cannot drift.

### Input

`input`:

```json
{
  "pA": 0.6,
  "pB": 0.5,
  "pAB": 0.3,
  "outcomes": ["up", "flat", "down"],
  "event": ["up", "flat"],
  "prior": 0.01,
  "sensitivity": 0.9,
  "falsePositiveRate": 0.05,
  "randomValues": [0, 1, 2],
  "probabilities": [0.2, 0.5, 0.3],
  "randomVariableKind": "discrete",
  "joint": [
    {
      "x": 0,
      "y": 0,
      "p": 0.3
    },
    {
      "x": 0,
      "y": 1,
      "p": 0.2
    },
    {
      "x": 1,
      "y": 0,
      "p": 0.1
    }
  ],
  "conditionY": 1
}
```

### Call

```ts
varianceMomentsAndMomentGeneratingIntuition(input)
```

### Returns

object with 2 fields: firstRawMoment, secondRawMoment

```json
{
  "firstRawMoment": 1.1,
  "secondRawMoment": 1.7
}
```

## Verification and provenance

Tier: **verified** (via input-expected).

The worked example below is the figure published in this algorithm's article, replayed and asserted by the test suite on every build. The arithmetic cannot drift without the build failing.

Both tiers guarantee the signature. Full explanation: https://docs.thefintechbuilder.com/guides/verification/

Generated from the docs.json payload shipped inside fintech-algorithms@0.13.0.
The signature and parameter list are checked against the compiled implementation at build time,
so a description that contradicts the code fails the build rather than reaching this file.

## Links

- Article (how it works, step by step): https://thefintechbuilder.com/foundations/probability-and-random-variables/variance-moments-and-moment-generating-intuition/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/foundations/probability-and-random-variables/variance-moments-and-moment-generating-intuition/impl.ts
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/foundations/llms.txt
