forked from mwaskom/Psych216
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
75 lines (62 loc) · 1.58 KB
/
utils.py
1
2
3
4
5
6
7
8
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
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
from __future__ import division
import numpy as np
from scipy import stats
def bootstrap(a, n_boot=10000, stat_func=np.mean):
"""Resample an array with replacement and calculate a summary stat.
Parameters
----------
a: array
data to resample
n_boot: int
number of resamples
stat_func: callable
function to call on each resampled dataset
"""
boot_dist = np.zeros(n_boot)
n = len(a)
for i in xrange(n_boot):
sample = a[np.random.randint(0, n, n)]
boot_dist[i] = stat_func(sample)
return boot_dist
def percentiles(a, pcts):
"""Like scoreatpercentile but can take and return array of percentiles.
Parameters
----------
a: array
data
pcts: sequence of percentile values
percentile or percentiles to find score at
Returns
-------
scores: array
array of scores at requested percentiles
"""
try:
scores = np.zeros(len(pcts))
except TypeError:
pcts = [pcts]
scores = np.zeros(1)
for i, p in enumerate(pcts):
scores[i] = stats.scoreatpercentile(a, p)
return scores
def pmf_hist(a, bins=10):
"""Return arguments to plt.bar for pmf-like histogram of an array.
Parameters
----------
a: array-like
array to make histogram of
bins: int
number of bins
Returns
-------
x: array
left x position of bars
h: array
height of bars
w: float
width of bars
"""
n, x = np.histogram(a, bins)
h = n / n.sum()
w = x[1] - x[0]
return x[:-1], h, w