resample Namespace Reference

Functions

def unWeighted_resample (weights, N)
 
def residual_resample (weights)
 
def stratified_resample (weights)
 
def systematic_resample (weights)
 
def multinomial_resample (weights)
 
def estimate_gmm (X, num_components, method='EM')
 

Function Documentation

◆ estimate_gmm()

def resample.estimate_gmm (   X,
  num_components,
  method = 'EM' 
)
145 def estimate_gmm(X, num_components, method='EM'):
146  # bring data into shogun representation (note that Shogun data is in column vector form, so transpose)
147  features_train=RealFeatures(X.T)
148 
149  # initialize GMM, passing the desired number of mixture components.
150  gmm = GMM(num_components)
151 
152  # train feature and sample data-points from the trained model.
153  gmm.set_features(features_train)
154  if method == 'EM': gmm.train_em()
155  elif method == 'SMEM': gmm.train_smem()
156 
157  # return GMM object and sampled data
158  return gmm, gmm.sample()
def estimate_gmm(X, num_components, method='EM')
Definition: resample.py:145

◆ multinomial_resample()

def resample.multinomial_resample (   weights)
 This is the naive form of roulette sampling where we compute the
cumulative sum of the weights and then use binary search to select the
resampled point based on a uniformly distributed random number. Run time
is O(n log n). You do not want to use this algorithm in practice; for some
reason it is popular in blogs and online courses so I included it for
reference.
Parameters
----------
weights : list-like of float
    list of weights as floats
Returns
-------
indexes : ndarray of ints
    array of indexes into the weights defining the resample. i.e. the
    index of the zeroth resample is indexes[0], etc.
124 def multinomial_resample(weights):
125  """ This is the naive form of roulette sampling where we compute the
126  cumulative sum of the weights and then use binary search to select the
127  resampled point based on a uniformly distributed random number. Run time
128  is O(n log n). You do not want to use this algorithm in practice; for some
129  reason it is popular in blogs and online courses so I included it for
130  reference.
131  Parameters
132  ----------
133  weights : list-like of float
134  list of weights as floats
135  Returns
136  -------
137  indexes : ndarray of ints
138  array of indexes into the weights defining the resample. i.e. the
139  index of the zeroth resample is indexes[0], etc.
140  """
141  cumulative_sum = np.cumsum(weights)
142  cumulative_sum[-1] = 1. # avoid round-off errors: ensures sum is exactly one
143  return np.searchsorted(cumulative_sum, random(len(weights)))
144 
def multinomial_resample(weights)
Definition: resample.py:124

◆ residual_resample()

def resample.residual_resample (   weights)
34 def residual_resample(weights):
35  N = len(weights)
36  indexes = np.zeros(N, 'i')
37 
38  # take int(N*w) copies of each weight, which ensures particles with the
39  # same weight are drawn uniformly
40  num_copies = (np.floor(N*np.asarray(weights))).astype(int)
41  k = 0
42  for i in range(N):
43  for _ in range(num_copies[i]): # make n copies
44  indexes[k] = i
45  k += 1
46 
47  # use multinormal resample on the residual to fill up the rest. This
48  # maximizes the variance of the samples
49  residual = weights - num_copies # get fractional part
50  residual /= sum(residual) # normalize
51  cumulative_sum = np.cumsum(residual)
52  cumulative_sum[-1] = 1. # avoid round-off errors: ensures sum is exactly one
53  indexes[k:N] = np.searchsorted(cumulative_sum, random(N-k))
54 
55  return indexes
56 
57 
58 
def residual_resample(weights)
Definition: resample.py:34

Referenced by tools.getGMMFromPosterior().

◆ stratified_resample()

def resample.stratified_resample (   weights)
 Performs the stratified resampling algorithm used by particle filters.
This algorithms aims to make selections relatively uniformly across the
particles. It divides the cumulative sum of the weights into N equal
divisions, and then selects one particle randomly from each division. This
guarantees that each sample is between 0 and 2/N apart.
Parameters
----------
weights : list-like of float
    list of weights as floats
Returns
-------
indexes : ndarray of ints
    array of indexes into the weights defining the resample. i.e. the
    index of the zeroth resample is indexes[0], etc.
59 def stratified_resample(weights):
60  """ Performs the stratified resampling algorithm used by particle filters.
61  This algorithms aims to make selections relatively uniformly across the
62  particles. It divides the cumulative sum of the weights into N equal
63  divisions, and then selects one particle randomly from each division. This
64  guarantees that each sample is between 0 and 2/N apart.
65  Parameters
66  ----------
67  weights : list-like of float
68  list of weights as floats
69  Returns
70  -------
71  indexes : ndarray of ints
72  array of indexes into the weights defining the resample. i.e. the
73  index of the zeroth resample is indexes[0], etc.
74  """
75 
76  N = len(weights)
77  # make N subdivisions, and chose a random position within each one
78  positions = (random(N) + range(N)) / N
79 
80  indexes = np.zeros(N, 'i')
81  cumulative_sum = np.cumsum(weights)
82  i, j = 0, 0
83  while i < N:
84  if positions[i] < cumulative_sum[j]:
85  indexes[i] = j
86  i += 1
87  else:
88  j += 1
89  return indexes
90 
91 
def stratified_resample(weights)
Definition: resample.py:59

◆ systematic_resample()

def resample.systematic_resample (   weights)
 Performs the systemic resampling algorithm used by particle filters.
This algorithm separates the sample space into N divisions. A single random
offset is used to to choose where to sample from for all divisions. This
guarantees that every sample is exactly 1/N apart.
Parameters
----------
weights : list-like of float
    list of weights as floats
Returns
-------
indexes : ndarray of ints
    array of indexes into the weights defining the resample. i.e. the
    index of the zeroth resample is indexes[0], etc.
92 def systematic_resample(weights):
93  """ Performs the systemic resampling algorithm used by particle filters.
94  This algorithm separates the sample space into N divisions. A single random
95  offset is used to to choose where to sample from for all divisions. This
96  guarantees that every sample is exactly 1/N apart.
97  Parameters
98  ----------
99  weights : list-like of float
100  list of weights as floats
101  Returns
102  -------
103  indexes : ndarray of ints
104  array of indexes into the weights defining the resample. i.e. the
105  index of the zeroth resample is indexes[0], etc.
106  """
107  N = len(weights)
108 
109  # make N subdivisions, and choose positions with a consistent random offset
110  positions = (random() + np.arange(N)) / N
111 
112  indexes = np.zeros(N, 'i')
113  cumulative_sum = np.cumsum(weights)
114  i, j = 0, 0
115  while i < N:
116  if positions[i] < cumulative_sum[j]:
117  indexes[i] = j
118  i += 1
119  else:
120  j += 1
121  return indexes
122 
123 
def systematic_resample(weights)
Definition: resample.py:92

◆ unWeighted_resample()

def resample.unWeighted_resample (   weights,
  N 
)
23 def unWeighted_resample(weights,N):
24  # take int(N*w) copies of each weight, which ensures particles with the same weight are drawn uniformly
25  num_copies = (np.floor(N*np.asarray(weights))).astype(int)
26  indexes = np.zeros(sum(num_copies), 'i')
27  k = 0
28  for i in range(len(weights)):
29  for _ in range(num_copies[i]): # make n copies
30  indexes[k] = i
31  k += 1
32  return indexes
33 
def unWeighted_resample(weights, N)
Definition: resample.py:23

Referenced by tools.resampledParamsTable().