Skip to content

Distributions (polykin.distributions)¤

convolve_moments ¤

convolve_moments(
    q: FloatVectorLike, r: FloatVectorLike
) -> FloatVector

Compute the moments of the convolution of two distributions.

If \(P = Q * R\) is the convolution of distributions \(Q\) and \(R\), defined as:

\[ P_n = \sum_{m=0}^{n} Q_{n-m}R_{m} \]

and \(p_i\), \(q_i\) and \(r_i\) denote the \(i\)-th moments of \(P\), \(Q\) and \(R\), respectively,

\[\begin{aligned} p_i & = \sum_{n=0}^{\infty} n^i P_n \\ q_i & = \sum_{n=0}^{\infty} n^i Q_n \\ r_i & = \sum_{n=0}^{\infty} n^i R_n \end{aligned}\]

then the moments of \(P\) are related to the moments of \(Q\) and \(R\) by:

\[ p_i = \sum_{j=0}^{i} \binom{i}{j} q_j r_{i-j} \]
PARAMETER DESCRIPTION
q

Moments of \(Q\), denoted \((q_0, q_1, \ldots)\).

TYPE: FloatVectorLike(N)

r

Moments of \(R\), denoted \((r_0, r_1, \ldots)\).

TYPE: FloatVectorLike(N)

RETURNS DESCRIPTION
FloatVector(N)

Moments of \(P=Q*R\), denoted \((p_0, p_1, \ldots)\).

Examples:

>>> from polykin.distributions import convolve_moments
>>> convolve_moments([1.0, 1e2, 2e4], [1.0, 50.0, 5e4])
array([1.0e+00, 1.5e+02, 8.0e+04])
Source code in src/polykin/distributions/misc.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def convolve_moments(
    q: FloatVectorLike,
    r: FloatVectorLike,
) -> FloatVector:
    r"""Compute the moments of the convolution of two distributions.

    If $P = Q * R$ is the convolution of distributions $Q$ and $R$, defined as:

    $$ P_n = \sum_{m=0}^{n} Q_{n-m}R_{m} $$

    and $p_i$, $q_i$ and $r_i$ denote the $i$-th moments of $P$, $Q$ and $R$,
    respectively,

    \begin{aligned}
        p_i & = \sum_{n=0}^{\infty} n^i P_n \\
        q_i & = \sum_{n=0}^{\infty} n^i Q_n \\
        r_i & = \sum_{n=0}^{\infty} n^i R_n        
    \end{aligned}

    then the moments of $P$ are related to the moments of $Q$ and $R$ by:

    $$ p_i = \sum_{j=0}^{i} \binom{i}{j} q_j r_{i-j} $$

    Parameters
    ----------
    q : FloatVectorLike (N)
        Moments of $Q$, denoted $(q_0, q_1, \ldots)$.
    r : FloatVectorLike (N)
        Moments of $R$, denoted $(r_0, r_1, \ldots)$.

    Returns
    -------
    FloatVector (N)
        Moments of $P=Q*R$, denoted $(p_0, p_1, \ldots)$.

    Examples
    --------
    >>> from polykin.distributions import convolve_moments
    >>> convolve_moments([1.0, 1e2, 2e4], [1.0, 50.0, 5e4])
    array([1.0e+00, 1.5e+02, 8.0e+04])
    """
    if len(q) != len(r):
        raise ValueError("`q` and `r` must have the same length.")

    p = np.zeros(len(q))
    for i in range(len(q)):
        for j in range(i+1):
            p[i] += comb(i, j)*q[j]*r[i-j]

    return p