-
Notifications
You must be signed in to change notification settings - Fork 2
/
mhd.py
73 lines (58 loc) · 2.25 KB
/
mhd.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
import numpy as np
def multivariate_hypergeometric_expected_vector(n,m):
"""
Expected value of multivariate hypergeometric distribution.
------------PARAMETERS------------
@param n : Number of draws.
@param m : Vector containing the number of items in each category.
@returns : Vector of the same length of m containing the expected values.
----------------------------------
"""
m = np.asarray(m, float)
return n * (m / m.sum())
def multivariate_hypergeometric_covariance_matrix(n,m):
"""
Covariant matrix of multivariate hypergeometric distribution.
------------PARAMETERS------------
@param n : Number of draws.
@param m : Vector containing the number of items in each category.
@returns : The covariance matrix of the same length of m containing the expected values.
----------------------------------
"""
N = sum(m)
gamma = n*(N-n)/(N-1)/N/N
F = len(m)
cov = np.ndarray(shape=(F-1,F-1),dtype=float)
#gamma = 1
for i in range(F-1):
for j in range(i+1,F-1):
cov[i][j]=-m[i]*m[j]
cov[j][i]=-m[i]*m[j]
for i in range(F-1):
cov[i][i]=m[i]*(N-m[i])
return cov*gamma
def multivariate_hypergeometric_sampling(n, m, simul=1, r_seed=False):
"""
Creating the sampling of unbaised selection process from the multivariate Sampling distribution
------------PARAMETERS------------
@param simul : Number of simulations.
@param n : Number of draws per simulation.
@param m : Vector containing the number of items in each category.
@returns : A list of vectors containing the number of items sampled from each category during the simulations
----------------------------------
"""
if r_seed:
#random initialization
np.random.seed(r_seed)
#the number of category
F = len(m)
N = sum(m)
gamma = n*(N-n)/(N-1)/N/N
sampled_vects=[]
# creating the urn with the different items to be sampled
urn = np.repeat(np.arange(F), m)
for k in range(simul):
draw = np.array([urn[i] for i in np.random.permutation(len(urn))[:n]])
sampled_vect = np.asarray([np.sum(draw == i) for i in range(F)])
sampled_vects.append(sampled_vect)
return sampled_vects