-
Notifications
You must be signed in to change notification settings - Fork 60
pseudo-p significance calculation #281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
9b80c2f
draft significance.py
JosiahParry 5e6b05b
multiply two-sided by 2
JosiahParry 1783ae9
add the two-sided percentile-based test and directed test with array …
ljwolf 65552ca
fix imports
ljwolf 920719c
add folding-based p-value
ljwolf d9ea095
swap to strict inequality
ljwolf c6e3a8a
add example and ruff
ljwolf fa2deaa
update significance directions
ljwolf 1c59fa7
adding one below is sufficient
26a51c9
update significance implementation for final merge
ljwolf d3278d0
just report p-value, keep calculation internal to method
ljwolf 3ff32b6
move to njit implementation
ljwolf b9f6161
move to significance machinery for crand
ljwolf 3b2ee7d
review by @martinfleis: prep for tests and benchmarking
ljwolf 708ac80
fix item() extraction in inner permutation loop
ljwolf 54d5f7d
iterate to calculate the percentages
ljwolf 7a2dcdf
update significance testing tests
ljwolf 9d38da3
Merge branch 'main' into calc-sig
ljwolf 1b53098
Merge branch 'main' of github.com:pysal/esda into calc-sig
ljwolf 9de1b1e
make sure weights types are cast correctly for matmul
ljwolf 1295dfc
add warning suppression when islands result in zero seI
ljwolf 239e737
fix typing, shaping, and iteration issues in significance
ljwolf d53a905
update notebook, removing warning filter and numba disclaimer
ljwolf 8dc453d
fix shaping and test validity
ljwolf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import numpy as np | ||
| import warnings | ||
|
|
||
| try: | ||
| from numba import njit | ||
| except (ImportError, ModuleNotFoundError): | ||
| from libpysal.common import jit as njit | ||
|
|
||
|
|
||
| def calculate_significance(test_stat, reference_distribution, alternative="two-sided"): | ||
| """ | ||
| Calculate a pseudo p-value from a reference distribution. | ||
|
|
||
| Pseudo-p values are calculated using the formula (M + 1) / (R + 1). Where R is the number of simulations | ||
| and M is the number of times that the simulated value was equal to, or more extreme than the observed test statistic. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| test_stat: float or numpy.ndarray | ||
| The observed test statistic, or a vector of observed test statistics | ||
| reference_distribution: numpy.ndarray | ||
| A numpy array containing simulated test statistics as a result of conditional permutation. | ||
| alternative: string | ||
| One of 'two-sided', 'lesser', 'greater', 'folded', or 'directed'. Indicates the alternative hypothesis. | ||
| - 'two-sided': the observed test statistic is in either tail of the reference distribution. This is an un-directed alternative hypothesis. | ||
| - 'folded': the observed test statistic is an extreme value of the reference distribution folded about its mean. This is an un-directed alternative hypothesis. | ||
| - 'lesser': the observed test statistic is small relative to the reference distribution. This is a directed alternative hypothesis. | ||
| - 'greater': the observed test statistic is large relative to the reference distribution. This is a directed alternative hypothesis. | ||
| - 'directed': the observed test statistic is in either tail of the reference distribution, but the tail is selected depending on the test statistic. This is a directed alternative hypothesis, but the direction is chosen dependent on the data. This is not advised, and included solely to reproduce past results. | ||
|
|
||
| Notes | ||
| ----- | ||
|
|
||
| the directed p-value is half of the two-sided p-value, and corresponds to running the | ||
| lesser and greater tests, then picking the smaller significance value. This is not advised, | ||
| since the p-value will be uniformly too small. | ||
| """ | ||
| reference_distribution = np.atleast_2d(reference_distribution) | ||
| n_samples, p_permutations = reference_distribution.shape | ||
| test_stat = np.atleast_2d(test_stat).reshape(n_samples, -1) | ||
| if alternative not in ( | ||
| 'folded', | ||
| 'two-sided', | ||
| 'greater', | ||
| 'lesser', | ||
| 'directed' | ||
| ): | ||
| raise ValueError( | ||
| f"alternative='{alternative}' provided, but is not" | ||
| f" one of the supported options: 'two-sided', 'greater', 'lesser', 'directed', 'folded')" | ||
| ) | ||
| return _permutation_significance( | ||
| test_stat, | ||
| reference_distribution, | ||
| alternative=alternative | ||
| ) | ||
|
|
||
| @njit(parallel=False, fastmath=False) | ||
| def _permutation_significance(test_stat, reference_distribution, alternative='two-sided'): | ||
| reference_distribution = np.atleast_2d(reference_distribution) | ||
| n_samples, p_permutations = reference_distribution.shape | ||
| if alternative == "directed": | ||
| larger = (reference_distribution >= test_stat).sum(axis=1) | ||
| low_extreme = (p_permutations - larger) < larger | ||
| larger[low_extreme] = p_permutations - larger[low_extreme] | ||
| p_value = (larger + 1.0) / (p_permutations + 1.0) | ||
| elif alternative == "lesser": | ||
| p_value = (np.sum(reference_distribution <= test_stat, axis=1) + 1) / ( | ||
| p_permutations + 1 | ||
| ) | ||
| elif alternative == "greater": | ||
| p_value = (np.sum(reference_distribution >= test_stat, axis=1) + 1) / ( | ||
| p_permutations + 1 | ||
| ) | ||
| elif alternative == "two-sided": | ||
| # find percentile p at which the test statistic sits | ||
| # find "synthetic" test statistic at 1-p | ||
| # count how many observations are outisde of (p, 1-p) | ||
| # including the test statistic and its synthetic pair | ||
| lows = np.empty(n_samples).astype(reference_distribution.dtype) | ||
| highs = np.empty(n_samples).astype(reference_distribution.dtype) | ||
| for i in range(n_samples): | ||
| percentile_i = (reference_distribution[i] <= test_stat).mean()*100 | ||
| p_low = np.minimum(percentile_i, 100-percentile_i) | ||
| lows[i] = np.percentile( | ||
| reference_distribution[i], | ||
| p_low | ||
| ) | ||
| highs[i] = np.percentile( | ||
| reference_distribution[i], | ||
| 100 - p_low | ||
| ) | ||
| n_outside = (reference_distribution <= lows[:,None]).sum(axis=1) | ||
| n_outside += (reference_distribution >= highs[:,None]).sum(axis=1) | ||
| p_value = (n_outside + 1) / (p_permutations + 1) | ||
| elif alternative == "folded": | ||
| means = np.empty((n_samples,1)).astype(reference_distribution.dtype) | ||
| for i in range(n_samples): | ||
| means[i] = reference_distribution[i].mean() | ||
| folded_test_stat = np.abs(test_stat - means) | ||
| folded_reference_distribution = np.abs(reference_distribution - means) | ||
| p_value = ((folded_reference_distribution >= folded_test_stat).sum(axis=1) + 1) / ( | ||
| p_permutations + 1 | ||
| ) | ||
| else: | ||
| p_value = np.ones((n_samples, ))*np.nan | ||
| return p_value | ||
|
|
||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: describe the adjustment here