Skip to content

sequence_stats

phyllotaxis_analysis.sequence_stats Link

circular_std_dev Link

circular_std_dev(kappa)

Calculates the circular standard deviation of a von Mises distribution.

The von Mises distribution is a continuous probability distribution on the circle. It is also known as the circular normal distribution or Tikhonov distribution.

This function calculates the circular standard deviation of a von Mises distribution, given its concentration parameter kappa (> 0).

Refer to scipy.stats.vonmises for more information on the von Mises distribution.

Parameters:

Name Type Description Default
kappa float

The concentration parameter of the von Mises distribution (> 0)

required

Returns:

Type Description
float

The circular standard deviation of the distribution

See Also

scipy.stats.vonmises : Probability density function, cumulative distribution function, etc. of the von Mises distribution.

References

.. [1] Gatto, A., & Jammalamadaka, seq. R. (2007). Circular statistics. Handbook of statistics, 26, 1-45.

Source code in src/phyllotaxis_analysis/sequence_stats.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def circular_std_dev(kappa):
    """
    Calculates the circular standard deviation of a von Mises distribution.

    The von Mises distribution is a continuous probability distribution on the circle.
    It is also known as the circular normal distribution or Tikhonov distribution.

    This function calculates the circular standard deviation of a
    von Mises distribution, given its concentration parameter `kappa` (> 0).

    Refer to `scipy.stats.vonmises` for more information on the von Mises distribution.

    Parameters
    ----------
    kappa : float
        The concentration parameter of the von Mises distribution (> 0)

    Returns
    -------
    float
        The circular standard deviation of the distribution

    See Also
    --------
    scipy.stats.vonmises : Probability density function, cumulative distribution function, etc.
    of the von Mises distribution.

    References
    ----------
    .. [1] Gatto, A., & Jammalamadaka, seq. R. (2007). Circular statistics. Handbook of statistics, 26, 1-45.
    """
    return 180.0 / np.pi * (np.sqrt(-2 * np.log(i1(kappa) / i0(kappa))))

log_of_prob_of_angle Link

log_of_prob_of_angle(x, mu, kappa, theoretical_angles)

Calculate the logarithmic probability of assigning a theoretical angle to a measured angle.

This function computes the log-probability of assigning a theoretical angle mu to a measured angle x, given a concentration parameter kappa and a list of theoretical angles t_angles. The result is the log of the von Mises probability density function evaluated at x and mu, normalized by the sum of the von Mises densities over all theoretical angles.

Parameters:

Name Type Description Default
x float

The measured angle in radians.

required
mu float

The theoretical angle in radians.

required
kappa float

The concentration parameter (kappa) of the von Mises distribution.

required
theoretical_angles list[float]

A list of theoretical angles in radians.

required

Returns:

Type Description
float

The logarithmic probability of assigning mu to x.

Examples:

>>> import numpy as np
>>> from phyllotaxis_analysis.sequence_stats import log_of_prob_of_angle
>>> x = 1.57  # measured angle in radians
>>> mu = 1.57  # theoretical angle in radians
>>> kappa = 1.0  # concentration parameter
>>> t_angles = [0.0, 1.57, 3.14]  # list of theoretical angles
>>> log_of_prob_of_angle(x, mu, kappa, t_angles)
-1.0986079206693713
Source code in src/phyllotaxis_analysis/sequence_stats.py
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
def log_of_prob_of_angle(x, mu, kappa, theoretical_angles):
    """
    Calculate the logarithmic probability of assigning a theoretical angle to a measured angle.

    This function computes the log-probability of assigning a theoretical angle `mu` to a measured angle `x`, given a concentration parameter `kappa` and a list of theoretical angles `t_angles`. The result is the log of the von Mises probability density function evaluated at `x` and `mu`, normalized by the sum of the von Mises densities over all theoretical angles.

    Parameters
    ----------
    x : float
        The measured angle in radians.
    mu : float
        The theoretical angle in radians.
    kappa : float
        The concentration parameter (kappa) of the von Mises distribution.
    theoretical_angles : list[float]
        A list of theoretical angles in radians.

    Returns
    -------
    float
        The logarithmic probability of assigning `mu` to `x`.

    Examples
    --------
    >>> import numpy as np
    >>> from phyllotaxis_analysis.sequence_stats import log_of_prob_of_angle
    >>> x = 1.57  # measured angle in radians
    >>> mu = 1.57  # theoretical angle in radians
    >>> kappa = 1.0  # concentration parameter
    >>> t_angles = [0.0, 1.57, 3.14]  # list of theoretical angles
    >>> log_of_prob_of_angle(x, mu, kappa, t_angles)
    -1.0986079206693713
    """
    sum_val = sum(von_mises_pdf(y, mu, kappa) for y in theoretical_angles)
    return float(np.log(von_mises_pdf(x, mu, kappa)) - np.log(sum_val))

max_prob_angle Link

max_prob_angle(x, kappa, theoretical_angles)

Calculate the list of most probable theoretical angles for a given measured angle.

Given a measured angle x, a concentration parameter kappa, and a list of theoretical angles, this function computes the probability of x under each theoretical angle using the von Mises distribution and returns the theoretical angles with the highest probability.

Parameters:

Name Type Description Default
x float

The measured angle in radians.

required
kappa float

The concentration parameter (kappa) of the von Mises distribution. Higher values indicate a stronger concentration around the mean direction.

required
theoretical_angles list[float]

A list of candidate theoretical angles (in radians) to evaluate.

required

Returns:

Type Description
list[float]

A list of theoretical angles from theoreticalAngles that have the highest probability of matching the measured angle x. If multiple angles share the same maximum probability, all are returned.

Examples:

>>> from phyllotaxis_analysis.sequence_stats import max_prob_angle
>>> theoretical_angles = [0.5, 1.0, 1.5, 2.0]
>>> max_prob_angle(1.0, 5.0, theoretical_angles)
[0.5]
>>> max_prob_angle(1.25, 2.0, theoretical_angles)
[1.0, 1.5]  # Both angles have similar probability at lower kappa
Source code in src/phyllotaxis_analysis/sequence_stats.py
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
def max_prob_angle(x, kappa, theoretical_angles):
    """
    Calculate the list of most probable theoretical angles for a given measured angle.

    Given a measured angle `x`, a concentration parameter `kappa`, and a list of
    theoretical angles, this function computes the probability of `x` under each
    theoretical angle using the von Mises distribution and returns the theoretical
    angles with the highest probability.

    Parameters
    ----------
    x : float
        The measured angle in radians.
    kappa : float
        The concentration parameter (kappa) of the von Mises distribution.
        Higher values indicate a stronger concentration around the mean direction.
    theoretical_angles : list[float]
        A list of candidate theoretical angles (in radians) to evaluate.

    Returns
    -------
    list[float]
        A list of theoretical angles from `theoreticalAngles` that have the highest
        probability of matching the measured angle `x`. If multiple angles share
        the same maximum probability, all are returned.

    Examples
    --------
    >>> from phyllotaxis_analysis.sequence_stats import max_prob_angle
    >>> theoretical_angles = [0.5, 1.0, 1.5, 2.0]
    >>> max_prob_angle(1.0, 5.0, theoretical_angles)
    [0.5]
    >>> max_prob_angle(1.25, 2.0, theoretical_angles)
    [1.0, 1.5]  # Both angles have similar probability at lower kappa
    """
    prob_list = [prob_of_angle(x, t_angle, kappa, theoretical_angles) for t_angle in theoretical_angles]
    max_prob = max(prob_list)
    return [theoretical_angles[i] for i in range(len(prob_list)) if prob_list[i] == max_prob]

prob_of_angle Link

prob_of_angle(x, mu, kappa, theoretical_angles)

Calculate the probability of assigning a theoretical angle to a measured angle using the von Mises distribution.

The probability is computed as the ratio of the von Mises probability density function (PDF) evaluated at the measured angle x to the sum of the PDF evaluated at all theoretical angles in theoretical_angles.

Parameters:

Name Type Description Default
x array_like

Measured angle in radians.

required
mu float

Theoretical mean angle in radians.

required
kappa float

Concentration parameter (kappa > 0) controlling the spread of the distribution.

required
theoretical_angles list

List of theoretical angles in radians.

required

Returns:

Type Description
float

Probability of assigning the theoretical angle mu to the measured angle x.

Notes

The von Mises distribution is a continuous probability distribution on the circle, often used for directional data. The PDF is given by:

.. math::

f(x | \mu, \kappa) = \frac{e^{\kappa \cos(x - \mu)}}{2\pi I_0(\kappa)}

where :math:I_0(\kappa) is the modified Bessel function of the first kind.

Examples:

>>> import numpy as np
>>> from phyllotaxis_analysis.sequence_stats import prob_of_angle
>>> theoretical_angles = [0.0, np.pi/2, np.pi, 3*np.pi/2]
>>> prob_of_angle(np.pi/4,np.pi/2,1.0,theoretical_angles)
0.25000204924805614
Source code in src/phyllotaxis_analysis/sequence_stats.py
 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
def prob_of_angle(x, mu, kappa, theoretical_angles):
    r"""
    Calculate the probability of assigning a theoretical angle to a measured angle using the von Mises distribution.

    The probability is computed as the ratio of the von Mises probability density function (PDF) evaluated at the
    measured angle `x` to the sum of the PDF evaluated at all theoretical angles in `theoretical_angles`.

    Parameters
    ----------
    x : array_like
        Measured angle in radians.
    mu : float
        Theoretical mean angle in radians.
    kappa : float
        Concentration parameter (`kappa` > 0) controlling the spread of the distribution.
    theoretical_angles : list
        List of theoretical angles in radians.

    Returns
    -------
    float
        Probability of assigning the theoretical angle `mu` to the measured angle `x`.

    Notes
    -----
    The von Mises distribution is a continuous probability distribution on the circle, often used for directional data.
    The PDF is given by:

    .. math::

        f(x | \mu, \kappa) = \frac{e^{\kappa \cos(x - \mu)}}{2\pi I_0(\kappa)}

    where :math:`I_0(\kappa)` is the modified Bessel function of the first kind.

    Examples
    --------
    >>> import numpy as np
    >>> from phyllotaxis_analysis.sequence_stats import prob_of_angle
    >>> theoretical_angles = [0.0, np.pi/2, np.pi, 3*np.pi/2]
    >>> prob_of_angle(np.pi/4,np.pi/2,1.0,theoretical_angles)
    0.25000204924805614
    """
    sum_val = sum(von_mises_pdf(y, mu, kappa) for y in theoretical_angles)
    return von_mises_pdf(x, mu, kappa) / sum_val

von_mises_pdf Link

von_mises_pdf(x, mu, kappa)

Computes the Von Mises probability density function.

The Von Mises distribution is a continuous probability distribution over the circle. It is also known as the circular normal distribution or Tikhonov distribution.

Parameters:

Name Type Description Default
x array_like

Quantiles at which to evaluate the probability density function.

required
mu float

The location parameter of the Von Mises distribution. Represents the mean direction of the distribution.

required
kappa float

The concentration parameter of the Von Mises distribution. Controls how concentrated the distribution is around its mean.

required

Returns:

Type Description
array_like

The value of the probability density function at x. Has the same shape as input x.

References

.. [1] Von Mises, R., "Das analytische Direktionsproblem für Kreiskegel," Zeitschrift für Angewandte Mathematik und Mechanik, vol. 8, no. 5, pp. 321-329, 1928. .. [2] Mardia, K. V., J. T. Kent, and J. M. Bibby, "Multivariate Analysis," Academic Press, London, UK, pp. 45-46, 1979.

Examples:

>>> import numpy as np
>>> from phyllotaxis_analysis.sequence_stats import von_mises_pdf
>>> x = np.array([0, 90, 180, 270])
>>> mu = 0
>>> kappa = 1
>>> von_mises_pdf(x, mu, kappa)
array([0.00223265, 0.00219402, 0.00215606, 0.00219402])
Source code in src/phyllotaxis_analysis/sequence_stats.py
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
72
73
74
75
76
77
78
79
80
81
82
83
def von_mises_pdf(x, mu, kappa):
    """
    Computes the Von Mises probability density function.

    The Von Mises distribution is a continuous probability distribution over the circle. It
    is also known as the circular normal distribution or Tikhonov distribution.

    Parameters
    ----------
    x : array_like
        Quantiles at which to evaluate the probability density function.
    mu : float
        The location parameter of the Von Mises distribution. Represents the mean
        direction of the distribution.
    kappa : float
        The concentration parameter of the Von Mises distribution. Controls how
        concentrated the distribution is around its mean.

    Returns
    -------
    array_like
        The value of the probability density function at `x`. Has the same shape as input `x`.

    References
    ----------
    .. [1] Von Mises, R., "Das analytische Direktionsproblem für Kreiskegel,"
       Zeitschrift für Angewandte Mathematik und Mechanik, vol. 8, no. 5, pp. 321-329, 1928.
    .. [2] Mardia, K. V., J. T. Kent, and J. M. Bibby, "Multivariate Analysis,"
       Academic Press, London, UK, pp. 45-46, 1979.

    Examples
    --------
    >>> import numpy as np
    >>> from phyllotaxis_analysis.sequence_stats import von_mises_pdf
    >>> x = np.array([0, 90, 180, 270])
    >>> mu = 0
    >>> kappa = 1
    >>> von_mises_pdf(x, mu, kappa)
    array([0.00223265, 0.00219402, 0.00215606, 0.00219402])
    """
    return 1.0 / (360 * i0(kappa)) * np.exp(kappa * np.cos(np.radians(x) - np.radians(mu)) * (np.pi / 180))