Skip to content

Distributions (polykin.distributions)¤

convolve_moments_self ¤

convolve_moments_self(
    q: FloatVectorLike, order: int
) -> FloatVector

Compute the moments of the k-th order convolution of a distribution with itself.

If \(P^{(k)}\) is the \(k\)-th order convolution of \(Q\) with itself, defined as:

\[\begin{aligned} P^{(1)}_n &= Q*Q = \sum_{m=0}^{n} Q_{n-m} Q_{m} \\ P^{(2)}_n &= (Q*Q)*Q = \sum_{m=0}^{n} Q_{n-m} P^{(1)}_{m} \\ P^{(3)}_n &= ((Q*Q)*Q)*Q = \sum_{m=0}^{n} Q_{n-m} P^{(2)}_{m} \\ ... \end{aligned}\]

then the moments of \(P^{(k)}\) are related to the moments of \(Q\) by:

\[\begin{aligned} p_0 &= q_0^{k+1} \\ p_1 &= (k+1) q_0^k q_1 \\ p_2 &= (k+1) q_0^{k-1} (k q_1^2 +q_0 q_2) \\ \ldots \end{aligned}\]

where \(p_i\) and \(q_i\) denote the \(i\)-th moments of \(P^{(k)}\) and \(Q\), respectively.

PARAMETER DESCRIPTION
q

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

TYPE: FloatVectorLike(N)

order

Order of the convolution.

TYPE: int

RETURNS DESCRIPTION
FloatVector(N)

Moments of \(P^{(k)}=(Q*Q)*...\), denoted \((p_0, p_1, \ldots)\).

Examples:

>>> from polykin.distributions import convolve_moments_self
>>> convolve_moments_self([1e0, 1e2, 2e4], 2)
array([1.0e+00, 3.0e+02, 1.2e+05])
Source code in src/polykin/distributions/misc.py
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
def convolve_moments_self(
    q: FloatVectorLike,
    order: int
) -> FloatVector:
    r"""Compute the moments of the k-th order convolution of a distribution
    with itself.

    If $P^{(k)}$ is the $k$-th order convolution of $Q$ with itself, defined as:

    \begin{aligned}
    P^{(1)}_n &= Q*Q = \sum_{m=0}^{n} Q_{n-m} Q_{m} \\
    P^{(2)}_n &= (Q*Q)*Q = \sum_{m=0}^{n} Q_{n-m} P^{(1)}_{m} \\
    P^{(3)}_n &= ((Q*Q)*Q)*Q = \sum_{m=0}^{n} Q_{n-m} P^{(2)}_{m} \\
    ...
    \end{aligned}

    then the moments of $P^{(k)}$ are related to the moments of $Q$ by:

    \begin{aligned}
    p_0 &= q_0^{k+1}  \\
    p_1 &= (k+1) q_0^k q_1 \\
    p_2 &= (k+1) q_0^{k-1} (k q_1^2 +q_0 q_2) \\
    \ldots
    \end{aligned}

    where $p_i$ and $q_i$ denote the $i$-th moments of $P^{(k)}$ and $Q$,
    respectively.    

    Parameters
    ----------
    q : FloatVectorLike (N)
        Moments of $Q$, denoted $(q_0, q_1, \ldots)$.
    order : int
        Order of the convolution.

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

    Examples
    --------
    >>> from polykin.distributions import convolve_moments_self
    >>> convolve_moments_self([1e0, 1e2, 2e4], 2)
    array([1.0e+00, 3.0e+02, 1.2e+05])
    """

    q = np.asarray(q, dtype=float)

    if order == 0:
        return q.copy()

    N = len(q)
    if N <= 3:
        # Use closed-form expressions for the first three moments
        p = np.empty(N)
        if N > 0:
            p[0] = q[0]**(order + 1)
        if N > 1:
            p[1] = (order + 1) * q[0]**order * q[1]
        if N > 2:
            p[2] = (order + 1) * q[0]**(order - 1) * (order*q[1]**2 + q[0]*q[2])
    else:
        p = q.copy()
        for _ in range(order):
            p = convolve_moments(q, p)

    return p