-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added pearsons correlation and inferential tests. (#14)
- Loading branch information
Showing
4 changed files
with
66 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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,33 @@ | ||
import numpy as np | ||
|
||
from scipy import stats as sp | ||
|
||
|
||
__all__ = ['pearsons_correlation'] | ||
|
||
|
||
def pearsons_correlation(raw_data): | ||
"""Computes the correlation and statistics for a dataset. | ||
Args: | ||
raw_data: Data matrix [n_items, n_observations] | ||
Returns: | ||
dict: Dictionary of correlation, and critical rho values | ||
Notes: | ||
The integration is over the n_observations such that the output is | ||
of size [n_items, n_items] | ||
""" | ||
correlation = np.corrcoef(raw_data) | ||
|
||
# Compute the critical values for the 3 significance tests | ||
deg_of_freedom = raw_data.shape[1] - 2 | ||
t_critical = sp.t.isf([.025, .005, 0.0005] , deg_of_freedom) | ||
r_critical = np.sqrt(t_critical**2 / (t_critical**2 + deg_of_freedom)) | ||
|
||
return {'Correlation': correlation, | ||
'R critical': {'.05': r_critical[0], | ||
'.01': r_critical[1], | ||
'.001': r_critical[2]}, | ||
} |
This file contains 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,30 @@ | ||
import unittest | ||
|
||
import numpy as np | ||
|
||
from RyStats.inferential import pearsons_correlation | ||
|
||
|
||
class TestCorrelation(unittest.TestCase): | ||
"""Test Fixture for correlation.""" | ||
|
||
def test_pearsons_correlation(self): | ||
"""Testing pearsons correlation.""" | ||
rng = np.random.default_rng(34982750394857201981982375) | ||
n_items = 100 | ||
dataset = rng.standard_normal((n_items, 1000)) | ||
|
||
results = pearsons_correlation(dataset) | ||
|
||
# Get the number of valid correlations | ||
correlation = np.abs(results['Correlation']) | ||
r_critical = results['R critical']['.05'] | ||
|
||
|
||
significant_data = (np.count_nonzero(correlation > r_critical) | ||
- n_items) / (n_items * (n_items - 1)) | ||
|
||
self.assertAlmostEqual(significant_data, .05, delta=0.01) | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains 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