Skip to content

Hypothesis Testing

rayleigh_test(alpha=None, w=None, r=None, n=None, verbose=False)

Rayleigh's Test for Circular Uniformity.

  • H0: The data in the population are distributed uniformly around the circle.
  • H1: THe data in the population are not disbutrited uniformly around the circle.

This method is for ungrouped data. For testing uniformity with grouped data, use chisquare_test() or scipy.stats.chisquare().

Parameters:

Name Type Description Default
alpha Union[ndarray, None]

Angles in radian.

None
w Union[ndarray, None]

Frequencies of angles.

None
r Union[float, None]

Resultant vector length from descriptive.circ_mean().

None
n Union[int, None]

Sample size.

None
verbose bool

Print formatted results.

False

Returns:

Name Type Description
z float

Test Statistics (Rayleigh's Z).

p float

P-value.

Reference

P625, Section 27.1, Example 27.1 of Zar, 2010

Source code in pycircstat2/hypothesis.py
21
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
def rayleigh_test(
    alpha: Union[np.ndarray, None] = None,
    w: Union[np.ndarray, None] = None,
    r: Union[float, None] = None,
    n: Union[int, None] = None,
    verbose: bool = False,
) -> tuple:
    """
    Rayleigh's Test for Circular Uniformity.

    - H0: The data in the population are distributed uniformly around the circle.
    - H1: THe data in the population are not disbutrited uniformly around the circle.

    This method is for ungrouped data. For testing uniformity with
    grouped data, use chisquare_test() or scipy.stats.chisquare().

    Parameters
    ----------

    alpha: np.array or None
        Angles in radian.

    w: np.array or None.
        Frequencies of angles.

    r: float or None
        Resultant vector length from `descriptive.circ_mean()`.

    n: int or None
        Sample size.

    verbose: bool
        Print formatted results.

    Returns
    -------
    z: float
        Test Statistics (Rayleigh's Z).

    p: float
        P-value.

    Reference
    ---------
    P625, Section 27.1, Example 27.1 of Zar, 2010
    """

    if r is None:
        assert isinstance(
            alpha, np.ndarray
        ), "If `r` is None, then `alpha` (and `w`) is needed."
        if w is None:
            w = np.ones_like(alpha)
        n = np.sum(w)
        r = circ_r(alpha, w)

    if n is None:
        raise ValueError("Sample size `n` is missing.")

    R = n * r
    z = R**2 / n  # eq(27.2)
    pval = np.exp(
        np.sqrt(1 + 4 * n + 4 * (n**2 - R**2)) - (1 + 2 * n)
    )  # eq(27.4)

    if verbose:
        print("Rayleigh's Test of Uniformity")
        print("-----------------------------")
        print("H0: ρ = 0")
        print("HA: ρ ≠ 0")
        print("")
        print(f"Test Statistics: {z:.5f}")
        print(f"P-value: {pval:.5f} {significance_code(pval)}")

    return z, pval

chisquare_test(w, verbose=False)

Chi-Square Goodness of Fit for Circular data.

  • H0: The data in the population are distributed uniformly around the circle.
  • H1: THe data in the population are not disbutrited uniformly around the circle.

For method is for grouped data.

Parameters:

Name Type Description Default
w ndarray

Frequencies of angles

required
verbose

Print formatted results.

False

Returns:

Name Type Description
chi2 float

The chi-squared test statistic.

pval float

The p-value of the test.

Note

It's a wrapper of scipy.stats.chisquare()

Reference

P662-663, Section 27.17, Example 27.23 of Zar, 2010

Source code in pycircstat2/hypothesis.py
 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
142
143
144
def chisquare_test(w: np.ndarray, verbose=False):
    """Chi-Square Goodness of Fit for Circular data.

    - H0: The data in the population are distributed uniformly around the circle.
    - H1: THe data in the population are not disbutrited uniformly around the circle.

    For method is for grouped data.

    Parameters
    ----------
    w: np.ndarray
        Frequencies of angles

    verbose: bool
        Print formatted results.

    Returns
    -------
    chi2: float
        The chi-squared test statistic.
    pval: float
        The p-value of the test.

    Note
    ----
    It's a wrapper of scipy.stats.chisquare()

    Reference
    ---------
    P662-663, Section 27.17, Example 27.23 of Zar, 2010
    """
    from scipy.stats import chisquare

    res = chisquare(w)
    chi2 = res.statistic
    pval = res.pvalue

    if verbose:
        print("Chi-Square Test of Uniformity")
        print("-----------------------------")
        print("H0: uniform")
        print("HA: not uniform")
        print("")
        print(f"Test Statistics: {chi2:.5f}")
        print(f"P-value: {pval:.5f} {significance_code(pval)}")

    return chi2, pval

V_test(angle, alpha=None, w=None, mean=None, r=None, n=None, verbose=False)

Modified Rayleigh Test for Uniformity versus a Specified Angle.

  • H0: The population is uniformly distributed around the circle (i.e., H0: ρ=0)
  • H1: The population is not uniformly distributed around the circle (i.e., H1: ρ!=0), but has a mean of 90 degree.

Parameters:

Name Type Description Default
angle Union[int, float]

Angle in radian to be compared with mean angle.

required
alpha Union[ndarray, None]

Angles in radian.

None
w Union[ndarray, None]

Frequencies of angles.

None
mean float

Circular mean from descriptive.circ_mean(). Needed if alpha is None.

None
r float

Resultant vector length from descriptive.circ_mean(). Needed if alpha is None.

None
n int

Sample size. Needed if alpha is None.

None
verbose bool

Print formatted results.

False

Returns:

Name Type Description
V float

Test Statistics.

u float

circular mean.

p float

P-value.

Reference

P627, Section 27.1, Example 27.2 of Zar, 2010

Source code in pycircstat2/hypothesis.py
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
def V_test(
    angle: Union[int, float],
    alpha: Union[np.ndarray, None] = None,
    w: Union[np.ndarray, None] = None,
    mean: float = None,
    r: float = None,
    n: int = None,
    verbose: bool = False,
) -> tuple:
    """
    Modified Rayleigh Test for Uniformity versus a Specified Angle.

    - H0: The population is uniformly distributed around the circle (i.e., H0: ρ=0)
    - H1: The population is not uniformly distributed around the circle (i.e., H1: ρ!=0),
        but has a mean of 90 degree.

    Parameters
    ----------
    angle: float or int
        Angle in radian to be compared with mean angle.

    alpha: np.array or None
        Angles in radian.

    w: np.array or None.
        Frequencies of angles.

    mean: float or None
        Circular mean from `descriptive.circ_mean()`. Needed if `alpha` is None.

    r: float or None
        Resultant vector length from `descriptive.circ_mean()`. Needed if `alpha` is None.

    n: int or None
        Sample size. Needed if `alpha` is None.

    verbose: bool
        Print formatted results.

    Returns
    -------

    V: float
        Test Statistics.
    u: float
        circular mean.
    p: float
        P-value.

    Reference
    ---------
    P627, Section 27.1, Example 27.2 of Zar, 2010
    """

    if mean is None or r is None or n is None:
        assert isinstance(
            alpha, np.ndarray
        ), "If `mean`, `r` or `n` is None, then `alpha` (and `w`) is needed."
        if w is None:
            w = np.ones_like(alpha)
        n = np.sum(w)
        mean, r = circ_mean_and_r(alpha, w)

    R = n * r
    V = R * np.cos(mean - angle)  # eq(27.5)
    u = V * np.sqrt(2 / n)  # eq(27.6)
    pval = 1 - norm().cdf(u)

    if verbose:
        print("Modified Rayleigh's Test of Uniformity")
        print("--------------------------------------")
        print("H0: ρ = 0")
        print("HA: ρ ≠ 0 and μ = {angle:.5f} rad")
        print("")
        print(f"Test Statistics: {V:.5f}")
        print(f"P-value: {pval:.5f} {significance_code(pval)}")

    return V, u, pval

one_sample_test(angle=0, alpha=None, w=None, lb=None, ub=None, verbose=False)

To test wheter the population mean angle is equal to a specified value, which is achieved by observing whether the angle lies within the 95% CI.

  • H0: The population has a mean of μ
  • H1: The population mean is not μ

Parameters:

Name Type Description Default
angle Union[int, float]

Angle in radian to be compared with mean angle.

0
alpha Union[ndarray, None]

Angles in radian.

None
w Union[ndarray, None]

Frequencies of angles

None
lb float

Lower bound of circular mean from descriptive.circ_mean_ci().

None
ub float

Upper bound of circular mean from descriptive.circ_mean_ci().

None
verbose bool

Print formatted results.

False

Returns:

Name Type Description
reject bool

Reject or not reject the null hypothesis.

Reference

P628, Section 27.1, Example 27.3 of Zar, 2010

Source code in pycircstat2/hypothesis.py
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
def one_sample_test(
    angle: Union[int, float] = 0,
    alpha: Union[np.ndarray, None] = None,
    w: Union[np.ndarray, None] = None,
    lb: float = None,
    ub: float = None,
    verbose: bool = False,
) -> bool:
    """
    To test wheter the population mean angle is equal to a specified value,
    which is achieved by observing whether the angle lies within the 95% CI.

    - H0: The population has a mean of μ
    - H1: The population mean is not μ

    Parameters
    ----------

    angle: float or int
        Angle in radian to be compared with mean angle.

    alpha: np.array or None
        Angles in radian.

    w: np.array or None.
        Frequencies of angles

    lb: float
        Lower bound of circular mean from `descriptive.circ_mean_ci()`.

    ub: float
        Upper bound of circular mean from `descriptive.circ_mean_ci()`.

    verbose: bool
        Print formatted results.

    Returns
    -------
    reject: bool
        Reject or not reject the null hypothesis.

    Reference
    ---------
    P628, Section 27.1, Example 27.3 of Zar, 2010
    """

    if lb is None or ub is None:
        assert isinstance(
            alpha, np.ndarray
        ), "If `ub` or `lb` is None, then `alpha` (and `w`) is needed."
        if w is None:
            w = np.ones_like(alpha)
        lb, ub = circ_mean_ci(alpha=alpha, w=w)

    if lb < angle < ub:
        reject = False  # not able reject null (mean angle == angle)
    else:
        reject = True  # reject null (mean angle == angle)

    if verbose:
        print("One-Sample Test for the Mean Angle")
        print("----------------------------------")
        print(f"H0: μ = μ0")
        print(f"HA: μ ≠ μ0 and μ0 = {angle:.5f} rad")
        print("")
        if reject:
            print(
                f"Reject H0:\nμ0 = {angle:.5f} lies outside the 95% CI of μ ({np.array([lb, ub]).round(5)})"
            )
        else:
            print(
                f"Failed to reject H0:\nμ0 = {angle:.5f} lies within the 95% CI of μ ({np.array([lb, ub]).round(5)})"
            )

    return reject

omnibus_test(alpha, scale=1, verbose=False)

A simple alternative to the Rayleigh test, aka Hodges-Ajne test, which does not assume sampling from a specific distribution. This is called an "omnibus test" because it works well for unimodal, bimodal, and multimodal distributions (for ungrouped data).

  • H0: The population is uniformly distributed around the circle
  • H1: The population is not uniformly distributed.

Parameters:

Name Type Description Default
alpha ndarray

Angles in radian.

required
scale int

Scale factor for the number of lines to be tested.

1
verbose bool

Print formatted results.

False

Returns:

Name Type Description
A float

Test statistics

pval float

p-value.

Reference

P629-630, Section 27.2, Example 27.4 of Zar, 2010

Source code in pycircstat2/hypothesis.py
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
def omnibus_test(
    alpha: np.ndarray,
    scale: int = 1,
    verbose: bool = False,
) -> float:
    """
    A simple alternative to the Rayleigh test, aka Hodges-Ajne test,
    which does not assume sampling from a specific distribution. This
    is called an "omnibus test" because it works well for unimodal,
    bimodal, and multimodal distributions (for ungrouped data).

    - H0: The population is uniformly distributed around the circle
    - H1: The population is not uniformly distributed.

    Parameters
    ----------
    alpha: np.array or None
        Angles in radian.

    scale: int
        Scale factor for the number of lines to be tested.

    verbose: bool
        Print formatted results.

    Returns
    -------
    A: float
        Test statistics

    pval: float
        p-value.

    Reference
    ---------
    P629-630, Section 27.2, Example 27.4 of Zar, 2010
    """

    lines = np.linspace(0, np.pi, scale * 360)
    n = len(alpha)

    lines_rotated = angrange((lines[:, None] - alpha)).round(5)

    # # count number of points on the right half circle, excluding the boundaries
    right = n - np.logical_and(
        lines_rotated > 0.0, lines_rotated < np.round(np.pi, 5)
    ).sum(1)
    m = int(np.min(right))
    pval = (
        (n - 2 * m)
        * math.factorial(n)
        / (math.factorial(m) * math.factorial(n - m))
        / 2 ** (n - 1)
    )
    A = np.pi * np.sqrt(n) / (2 * (n - 2 * m))

    if verbose:
        print('Hodges-Ajne ("omnibus") Test for Uniformity')
        print("-------------------------------------------")
        print("H0: uniform")
        print("HA: not unifrom")
        print("")
        print(f"Test Statistics: {A:.5f}")
        print(f"P-value: {pval:.5f} {significance_code(pval)}")
    return A, pval

batschelet_test(angle, alpha, verbose=False)

Modified Hodges-Ajne Test for Uniformity versus a specified Angle (for ungrouped data).

  • H0: The population is uniformly distributed around the circle.
  • H1: The population is not uniformly distributed around the circle, but is concentrated around a specified angle.

Parameters:

Name Type Description Default
angle float

A specified angle.

required
alpha ndarray

Angles in radian.

required
verbose bool

Print formatted results.

False

Returns:

Name Type Description
pval float

p-value

Reference

P630-631, Section 27.2, Example 27.5 of Zar, 2010

Source code in pycircstat2/hypothesis.py
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
def batschelet_test(
    angle: float,
    alpha: np.ndarray,
    verbose: bool = False,
) -> float:
    """Modified Hodges-Ajne Test for Uniformity versus a specified Angle
    (for ungrouped data).

    - H0: The population is uniformly distributed around the circle.
    - H1: The population is not uniformly distributed around the circle, but
        is concentrated around a specified angle.

    Parameters
    ----------
    angle: np.array
        A specified angle.

    alpha: np.array or None
        Angles in radian.

    verbose: bool
        Print formatted results.

    Returns
    -------
    pval: float
        p-value

    Reference
    ---------
    P630-631, Section 27.2, Example 27.5 of Zar, 2010
    """

    from scipy.stats import binomtest

    n = len(alpha)
    angle_diff = angrange(((angle + 0.5 * np.pi) - alpha)).round(5)
    m = np.logical_and(angle_diff > 0.0, angle_diff < np.round(np.pi, 5)).sum()
    C = n - m
    pval = binomtest(C, n=n, p=0.5).pvalue

    if verbose:
        print("Batschelet Test for Uniformity")
        print("------------------------------")
        print("H0: uniform")
        print(f"HA: not unifrom but concentrated around θ = {angle:.5f} rad")
        print("")
        print(f"Test Statistics: {C}")
        print(f"P-value: {pval:.5f} {significance_code(pval)}")

    return C, pval

symmetry_test(alpha, median=None, verbose=False)

Non-parametric test for symmetry around the median. Works by performing a Wilcoxon sign rank test on the differences to the median. Also known as Wilcoxon paired-sample test.

  • H0: the population is symmetrical around the median
  • HA: the population is not symmetrical around the median

Parameters:

Name Type Description Default
alpha ndarray

Angles in radian.

required
median Union[int, float, None]

Median computed by descriptive.median().

None
verbose bool

Print formatted results.

False

Returns:

Name Type Description
test_statistic float

Test statistic

pval float

p-value

Reference

P631-632, Section 27.3, Example 27.6 of Zar, 2010

Source code in pycircstat2/hypothesis.py
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
def symmetry_test(
    alpha: np.ndarray,
    median: Union[int, float, None] = None,
    verbose: bool = False,
) -> tuple[float, float]:
    """Non-parametric test for symmetry around the median. Works by performing a
    Wilcoxon sign rank test on the differences to the median. Also known as
    Wilcoxon paired-sample test.

    - H0: the population is symmetrical around the median
    - HA: the population is not symmetrical around the median

    Parameters
    ----------
    alpha: np.array
        Angles in radian.

    median: float or None.
        Median computed by `descriptive.median()`.

    verbose: bool
        Print formatted results.

    Returns
    -------
    test_statistic: float
        Test statistic
    pval: float
        p-value

    Reference
    ---------
    P631-632, Section 27.3, Example 27.6 of Zar, 2010
    """

    if median is None:
        median = circ_median(alpha=alpha)

    d = (alpha - median).round(5)
    res = wilcoxon(d, alternative="two-sided")
    test_statistic = res.statistic
    pval = res.pvalue

    if verbose:
        print("Symmetry Test")
        print("------------------------------")
        print(f"H0: symmetrical around median")
        print(f"HA: not symmetrical around median")
        print("")
        print(f"Test Statistics: {test_statistic:.5f}")
        print(f"P-value: {pval:.5f} {significance_code(pval)}")

    return test_statistic, pval

watson_williams_test(circs, verbose=False)

The Watson-Williams Test for multiple samples.

  • H0: All samples are from populations with the same mean angle
  • H1: All samples are not from populations with the same mean angle

Parameters:

Name Type Description Default
circs list

A list of Circular objects.

required
verbose bool

Print formatted results.

False

Returns:

Name Type Description
F float

F value

pval float

p-value

Reference

P632-636, Section 27.4, Example 27.7/8 of Zar, 2010

Source code in pycircstat2/hypothesis.py
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
def watson_williams_test(circs: list, verbose: bool = False) -> tuple:
    """The Watson-Williams Test for multiple samples.

    - H0: All samples are from populations with the same mean angle
    - H1: All samples are not from populations with the same mean angle

    Parameters
    ----------
    circs: list (k, )
        A list of Circular objects.

    verbose: bool
        Print formatted results.

    Returns
    -------
    F: float
        F value

    pval: float
        p-value

    Reference
    ---------
    P632-636, Section 27.4, Example 27.7/8 of Zar, 2010
    """

    k = len(circs)
    N = np.sum([circ.n for circ in circs])
    rw = np.mean([circ.r for circ in circs])

    K = 1 + 3 / 8 / circ_kappa(rw)

    Rs = [circ.R for circ in circs]
    R = N * circ_r(
        alpha=np.hstack([circ.alpha for circ in circs]),
        w=np.hstack([circ.w for circ in circs]),
    )
    F = K * (N - k) * (np.sum(Rs) - R) / (N - np.sum(Rs)) / (k - 1)
    pval = f.sf(F, k - 1, N - k)

    if verbose:
        print("The Watson-Williams Test for multiple samples")
        print("---------------------------------------------")
        print("H0: all samples are from populations with the same angle.")
        print("HA: all samples are not from populations with the same angle.")
        print("")
        print(f"Test Statistics: {F:.5f}")
        print(f"P-value: {pval:.5f} {significance_code(pval)}")

    return F, pval

watson_u2_test(circs, verbose=False)

Watson's U2 Test for nonparametric two-sample testing (with or without ties).

  • H0: The two samples came from the same population, or from two populations having the same direction.
  • H1: The two samples did not come from the same population, or from two populations having the same directions.

Use this instead of Watson-Williams two-sample test when at least one of the sampled populations is not unimodal or when there are other considerable departures from the assumptions of the latter test. It may be used on grouped data if the grouping interval is no greater than 5 degree.

Parameters:

Name Type Description Default
circs list

A list of Circular objects.

required
verbose bool

Print formatted results.

False

Returns:

Name Type Description
U2 float

U2 value

pval float

p value

Reference

P637-638, Section 27.5, Example 27.9 of Zar, 2010 P639-640, Section 27.5, Example 27.10 of Zar, 2010

Source code in pycircstat2/hypothesis.py
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
def watson_u2_test(circs: list, verbose: bool = False) -> tuple:
    """Watson's U2 Test for nonparametric two-sample testing
    (with or without ties).

    - H0: The two samples came from the same population,
        or from two populations having the same direction.
    - H1: The two samples did not come from the same population,
        or from two populations having the same directions.

    Use this instead of Watson-Williams two-sample test when at
    least one of the sampled populations is not unimodal or when
    there are other considerable departures from the assumptions
    of the latter test. It may be used on grouped data if the
    grouping interval is no greater than 5 degree.

    Parameters
    ----------
    circs: list
        A list of Circular objects.

    verbose: bool
        Print formatted results.

    Returns
    -------
    U2: float
        U2 value
    pval: float
        p value

    Reference
    ---------
    P637-638, Section 27.5, Example 27.9 of Zar, 2010
    P639-640, Section 27.5, Example 27.10 of Zar, 2010
    """

    from scipy.stats import rankdata

    def cumfreq(alpha, circ):
        indices = np.squeeze(
            [np.where(alpha == a)[0] for a in np.repeat(circ.alpha, circ.w)]
        )
        indices = np.hstack([0, indices, len(alpha)])
        freq_cumsum = (
            rankdata(np.repeat(circ.alpha, circ.w), method="max") / circ.n
        )
        freq_cumsum = np.hstack([0, freq_cumsum])

        tiles = np.diff(indices)
        cf = np.repeat(freq_cumsum, tiles)

        return cf

    a, t = np.unique(
        np.hstack([np.repeat(c.alpha, c.w) for c in circs]), return_counts=True
    )
    cfs = [cumfreq(a, c) for c in circs]
    d = np.diff(cfs, axis=0)

    N = np.sum([c.n for c in circs])
    U2 = (
        np.prod([c.n for c in circs])
        / N**2
        * (np.sum(t * d**2) - np.sum(t * d) ** 2 / N)
    )
    pval = 2 * np.exp(-19.74 * U2)
    # Approximated P-value from Watson (1961)
    # https://github.com/pierremegevand/watsons_u2/blob/master/watsons_U2_approx_p.m

    if verbose:
        print("Watson's U2 Test for two samples")
        print("---------------------------------------------")
        print("H0: The two samples are from populations with the same angle.")
        print(
            "HA: The two samples are not from populations with the same angle."
        )
        print("")
        print(f"Test Statistics: {U2:.5f}")
        print(f"P-value: {pval:.5f} {significance_code(pval)}")

    return U2, pval

wheeler_watson_test(circs, verbose=False)

The Wheeler and Watson Two/Multi-Sample Test.

  • H0: The two samples came from the same population, or from two populations having the same direction.
  • H1: The two samples did not come from the same population, or from two populations having the same directions.

Parameters:

Name Type Description Default
circs list

A list of Circular objects.

required
verbose bool

Print formatted results.

False

Returns:

Name Type Description
W float

W value

pval float

p value

Reference

P640-642, Section 27.5, Example 27.11 of Zar, 2010

Note

The current implementation doesn't consider ties in the data. Can be improved with P144, Pewsey et al. (2013)

Source code in pycircstat2/hypothesis.py
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
def wheeler_watson_test(circs: list, verbose: bool = False):
    """The Wheeler and Watson Two/Multi-Sample Test.

    - H0: The two samples came from the same population,
        or from two populations having the same direction.
    - H1: The two samples did not come from the same population,
        or from two populations having the same directions.

    Parameters
    ----------
    circs: list
        A list of Circular objects.

    verbose: bool
        Print formatted results.

    Returns
    -------
    W: float
        W value
    pval: float
        p value

    Reference
    ---------
    P640-642, Section 27.5, Example 27.11 of Zar, 2010

    Note
    ----
    The current implementation doesn't consider ties in the data.
    Can be improved with P144, Pewsey et al. (2013)
    """
    from scipy.stats import chi2

    def get_circrank(alpha, circ, N):
        rank_of_direction = (
            np.squeeze(
                [
                    np.where(alpha == a)[0]
                    for a in np.repeat(circ.alpha, circ.w)
                ]
            )
            + 1
        )
        circ_rank = 2 * np.pi / N * rank_of_direction
        return circ_rank

    N = np.sum([c.n for c in circs])
    a, t = np.unique(
        np.hstack([np.repeat(c.alpha, c.w) for c in circs]), return_counts=True
    )

    circ_ranks = [get_circrank(a, c, N) for c in circs]

    k = len(circ_ranks)

    if k == 2:
        C = np.sum(np.cos(circ_ranks[0]))
        S = np.sum(np.sin(circ_ranks[0]))
        W = 2 * (N - 1) * (C**2 + S**2) / np.prod([c.n for c in circs])

    elif k > 3:
        W = 0
        for i in range(k):
            circ_rank = circ_ranks[i]
            C = np.sum(np.cos(circ_rank))
            S = np.sum(np.sin(circ_rank))
            W += (C**2 + S**2) / circs[i].n
        W *= 2

    pval = chi2.sf(W, df=2 * (k - 1))

    if verbose:
        print("The Wheeler and Watson Two/Multi-Sample Test")
        print("---------------------------------------------")
        print("H0: All samples are from populations with the same angle.")
        print("HA: All samples are not from populations with the same angle.")
        print("")
        print(f"Test Statistics: {W:.5f}")
        print(f"P-value: {pval:.5f} {significance_code(pval)}")

    return W, pval

wallraff_test(circs, angle=float, verbose=False)

Wallraff test of angular distances / dispersion against a specified angle.

Parameters:

Name Type Description Default
circs list

A list of circular object

required
angle

A specified angle in radian.

float
verbose bool

Print formatted results.

False

Returns:

Name Type Description
U float

Test Statistics

pval float

P-value.

Reference

P637-638, Section 27.8, Example 27.13 of Zar, 2010

Source code in pycircstat2/hypothesis.py
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
def wallraff_test(circs: list, angle=float, verbose: bool = False):
    """Wallraff test of angular distances / dispersion against a specified angle.

    Parameters
    ----------
    circs: list
        A list of circular object

    angle: float
        A specified angle in radian.

    verbose: bool
        Print formatted results.

    Returns
    -------
    U: float
        Test Statistics

    pval: float
        P-value.

    Reference
    ---------
    P637-638, Section 27.8, Example 27.13 of Zar, 2010
    """

    assert (
        len(circs) == 2
    ), "Current implementation only supports two-sample comparision."

    angles = np.ones(len(circs)) * angle if isinstance(angle, float) else angle
    ns = [c.n for c in circs]
    ad = [
        angular_distance(a=c.alpha, b=angles[i]) for (i, c) in enumerate(circs)
    ]

    rs = rankdata(np.hstack(ad))

    N = np.sum(ns)

    # mann-whitney
    R1 = np.sum(rs[: ns[0]])
    U1 = np.prod(ns) + ns[0] * (ns[0] + 1) / 2 - R1
    U2 = np.prod(ns) - U1
    U = np.min([U1, U2])

    z = (U - np.prod(ns) / 2 + 0.5) / np.sqrt(np.prod(ns) * (N + 1) / 12)
    pval = 2 * norm.cdf(z)

    if verbose:
        print("Wallraff test of angular distances / dispersion")
        print("-----------------------------------------------")
        print("")
        print(f"Test Statistics: {U:.5f}")
        print(f"P-value: {pval:.5f} {significance_code(pval)}")

    return U, pval

kuiper_test(alpha, n_simulation=9999, seed=2046, verbose=False)

Kuiper's test for Circular Uniformity.

  • H0: The data in the population are distributed uniformly around the circle.
  • H1: THe data in the population are not disbutrited uniformly around the circle.

This method is for ungrouped data.

Parameters:

Name Type Description Default
alpha ndarray

Angles in radian.

required
n_simulation int

Number of simulation for the p-value. If n_simulation=1, the p-value is asymptotically approximated. If n_simulation>1, the p-value is simulated. Default is 9999.

9999
seed int

Random seed.

2046

Returns:

Name Type Description
V float

Test Statistics

pval flaot

Asymptotic p-value

Note

Implementation from R package Directional https://rdrr.io/cran/Directional/src/R/kuiper.R

Source code in pycircstat2/hypothesis.py
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
def kuiper_test(
    alpha: np.ndarray,
    n_simulation: int = 9999,
    seed: int = 2046,
    verbose: bool = False,
) -> tuple:
    """
    Kuiper's test for Circular Uniformity.

    - H0: The data in the population are distributed uniformly around the circle.
    - H1: THe data in the population are not disbutrited uniformly around the circle.

    This method is for ungrouped data.

    Parameters
    ----------

    alpha: np.array
        Angles in radian.

    n_simulation: int
        Number of simulation for the p-value.
        If n_simulation=1, the p-value is asymptotically approximated.
        If n_simulation>1, the p-value is simulated.
        Default is 9999.

    seed: int
        Random seed.

    Returns
    -------
    V: float
        Test Statistics
    pval: flaot
        Asymptotic p-value

    Note
    ----
    Implementation from R package `Directional`
    https://rdrr.io/cran/Directional/src/R/kuiper.R
    """

    def compute_V(alpha):
        alpha = np.sort(alpha) / (2 * np.pi)  #
        n = len(alpha)
        i = np.arange(1, n + 1)

        D_plus = np.max(i / n - alpha)
        D_minus = np.max(alpha - (i - 1) / n)
        f = np.sqrt(n) + 0.155 + 0.24 / np.sqrt(n)
        V = f * (D_plus + D_minus)
        return V, f

    n = n = len(alpha)
    Vo, f = compute_V(alpha)

    if n_simulation == 1:
        # asymptotic p-value
        m = np.arange(1, 50) ** 2
        a1 = 4 * m * Vo**2
        a2 = np.exp(-2 * m * Vo**2)
        b1 = 2 * (a1 - 1) * a2
        b2 = 8 * Vo / (3 * f) * m * (a1 - 3) * a2
        pval = np.sum(b1 - b2)
    else:
        np.random.seed(seed)
        x = np.sort(
            np.random.uniform(low=0, high=2 * np.pi, size=[n, n_simulation]), 0
        )
        Vs = np.array(([compute_V(x[:, i])[0] for i in range(n_simulation)]))
        pval = (np.sum(Vs > Vo) + 1) / (n_simulation + 1)

    if verbose:
        print("Kuiper's Test of Circular Uniformity")
        print("------------------------------------")
        print("")
        print(f"Test Statistic: {Vo:.4f}")
        print(f"P-value = {pval} {significance_code(pval)}")

    return Vo, pval

watson_test(alpha, n_simulation=9999, seed=2046, verbose=False)

Watson's Goodness-of-Fit Testing, aka Watson one-sample U2 test.

  • H0: The sample data come from a population distributed uniformly around the circle.
  • H1: The sample data do not come from a population distributed uniformly around the circle.

This method is for ungrouped data.

Parameters:

Name Type Description Default
alpha ndarray

Angles in radian.

required
n_simulation int

Number of simulation for the p-value. If n_simulation=1, the p-value is asymptotically approximated. If n_simulation>1, the p-value is simulated.

9999
seed int

Random seed.

2046

Returns:

Name Type Description
U2o float

Test Statistics

pval flaot

Asymptotic p-value

Note

Implementation from R package Directional https://rdrr.io/cran/Directional/src/R/watson.R

The code for simulated p-value in Directional (v5.7) seems to be just copied from kuiper(), thus yield in wrong results.

See Also

kuiper_test(); rao_spacing_test()

Source code in pycircstat2/hypothesis.py
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
def watson_test(
    alpha: np.ndarray,
    n_simulation: int = 9999,
    seed: int = 2046,
    verbose: bool = False,
) -> tuple:
    """
    Watson's Goodness-of-Fit Testing, aka Watson one-sample U2 test.

    - H0: The sample data come from a population distributed uniformly around the circle.
    - H1: The sample data do not come from a population distributed uniformly around the circle.

    This method is for ungrouped data.

    Parameters
    ----------

    alpha: np.array
        Angles in radian.

    n_simulation: int
        Number of simulation for the p-value.
        If n_simulation=1, the p-value is asymptotically approximated.
        If n_simulation>1, the p-value is simulated.

    seed: int
        Random seed.

    Returns
    -------
    U2o: float
        Test Statistics
    pval: flaot
        Asymptotic p-value

    Note
    ----
    Implementation from R package `Directional`
    https://rdrr.io/cran/Directional/src/R/watson.R

    The code for simulated p-value in Directional (v5.7) seems to be just copied from
    kuiper(), thus yield in wrong results.

    See Also
    --------
    kuiper_test(); rao_spacing_test()
    """

    def compute_U2(alpha):
        alpha = np.sort(alpha)
        n = len(alpha)
        i = np.arange(1, n + 1)

        u = alpha / 2 / np.pi
        # u2 = u**2
        # iu = i * u

        U2 = np.sum(((u - (i - 0.5) / n) - (np.sum(u) / n - 0.5)) ** 2) + 1 / (
            12 * n
        )
        return U2

    n = len(alpha)
    U2o = compute_U2(alpha)

    if n_simulation == 1:
        m = np.arange(1, 51)
        pval = 2 * sum((-1) ** (m - 1) * np.exp(-2 * m**2 * np.pi**2 * U2o))
    else:
        np.random.seed(seed)
        x = np.sort(
            np.random.uniform(low=0, high=2 * np.pi, size=[n, n_simulation]), 0
        )
        U2s = np.array(([compute_U2(x[:, i]) for i in range(n_simulation)]))
        pval = (np.sum(U2s > U2o) + 1) / (n_simulation + 1)

    if verbose:
        print("Watson's One-Sample U2 Test of Circular Uniformity")
        print("--------------------------------------------------")
        print("")
        print(f"Test Statistic: {U2o:.4f}")
        print(f"P-value = {pval} {significance_code(pval)}")

    return U2o, pval

rao_spacing_test(alpha, w=None, kappa=1000.0, n_simulation=9999, seed=2046, verbose=False)

Simulation based Rao's spacing test.

  • H0: The sample data come from a population distributed uniformly around the circle.
  • H1: The sample data do not come from a population distributed uniformly around the circle.

This method is for both grouped and ungrouped data.

Parameters:

Name Type Description Default
alpha ndarray

Angles in radian.

required
w Union[ndarray, None]

Frequencies

None
kappa float

Concentration parameter. Only use for grouped data.

1000.0
n_simulation int

Number of simulations.

9999
seed int

Random seed.

2046

Returns:

Name Type Description
Uo float

Test statistics

pval float

Simulation-based p-value

Reference

Landler et al. (2019) https://movementecologyjournal.biomedcentral.com/articles/10.1186/s40462-019-0160-x

Source code in pycircstat2/hypothesis.py
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
def rao_spacing_test(
    alpha: np.ndarray,
    w: Union[np.ndarray, None] = None,
    kappa: float = 1000.0,
    n_simulation: int = 9999,
    seed: int = 2046,
    verbose: bool = False,
) -> tuple:
    """Simulation based Rao's spacing test.

    - H0: The sample data come from a population distributed uniformly around the circle.
    - H1: The sample data do not come from a population distributed uniformly around the circle.

    This method is for both grouped and ungrouped data.

    Parameters
    ----------
    alpha: np.ndarray
        Angles in radian.

    w: np.ndarray or None
        Frequencies

    kappa: float
        Concentration parameter. Only use for grouped data.

    n_simulation: int
        Number of simulations.

    seed: int
        Random seed.

    Returns
    -------
    Uo: float
        Test statistics

    pval: float
        Simulation-based p-value

    Reference
    ---------
    Landler et al. (2019)
    https://movementecologyjournal.biomedcentral.com/articles/10.1186/s40462-019-0160-x
    """

    def compute_U(alpha):
        n = len(alpha)
        f = np.sort(alpha)
        T = np.hstack([f[1:] - f[:-1], 2 * np.pi - f[-1] + f[0]])
        U = 0.5 * np.sum(np.abs(T - (2 * np.pi / n)))
        return U

    if w is not None:
        n = np.sum(w)
        m = len(alpha)
        alpha = np.repeat(alpha, w)
    else:
        n = len(alpha)

    # p-value
    np.random.seed(seed)
    Uo = compute_U(alpha)
    if w is not None:  # noncontinous / grouped data
        Us = np.array(
            [
                compute_U(
                    angrange(
                        np.floor(
                            np.random.uniform(low=0, high=2 * np.pi, size=n)
                        )
                        * m
                        / (2 * np.pi)
                        * 2
                        * np.pi
                        / m
                        + vonmises(kappa=kappa).rvs(n)
                    )
                )
                for i in range(n_simulation)
            ]
        )
    else:  # continous / ungrouped data
        Us = np.array(
            [
                compute_U(np.random.uniform(low=0, high=2 * np.pi, size=n))
                for i in range(n_simulation)
            ]
        )

    counter = np.sum(Us > Uo)
    pval = counter / (n_simulation + 1)

    if verbose:
        print("Rao's Spacing Test of Circular Uniformity")
        print("-----------------------------------------")
        print("")
        print(f"Test Statistic: {Uo:.4f}")
        print(f"P-value = {pval}\n")

    return np.rad2deg(Uo), pval