Skip to content

Commit

Permalink
made separate normalization function
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver K. Ernst committed Jun 20, 2019
1 parent dfc8630 commit 1c9a50c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 40 deletions.
50 changes: 24 additions & 26 deletions samplePairsGaussian/prob_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,12 @@ def set_logging_level(self, level):



def compute_probs_first_particle(self, with_normalization=False):
"""Compute probabilities for drawing the first particle out of n possible particles.
def compute_un_probs_first_particle(self):
"""Compute un-normalized probabilities for drawing the first particle out of n possible particles.
After running this, the following arguments will be set:
probs_first_particle
are_probs_first_particle_normalized
max_prob_first_particle
Args:
with_normalization (bool): whether the probabilities will be normalized
"""

# Check there are sufficient particles
Expand Down Expand Up @@ -128,21 +125,20 @@ def compute_probs_first_particle(self, with_normalization=False):

# Not normalized probs for first particle
self.probs_first_particle = np.bincount(self._uti0filter,self._gauss,minlength=self.n) + np.bincount(self._uti1filter,self._gauss,minlength=self.n)
self.are_probs_first_particle_normalized = False
self.max_prob_first_particle = max(self.probs_first_particle)

# Normalization
if with_normalization:
self.are_probs_first_particle_normalized = True
norm = np.sum(self.probs_first_particle)
self.probs_first_particle /= norm
self.max_prob_first_particle = 1.0
else:
self.are_probs_first_particle_normalized = False
self.max_prob_first_particle = max(self.probs_first_particle)

def normalize_probs_first_particle(self):
"""Normalize the probs for the first particle
"""

self.are_probs_first_particle_normalized = True
norm = np.sum(self.probs_first_particle)
self.probs_first_particle /= norm
self.max_prob_first_particle = 1.0

def compute_probs_second_particle(self, idx_first_particle, with_normalization=False):
"""Compute probabilities for drawing the second particle for a given first particle.
def compute_un_probs_second_particle(self, idx_first_particle):
"""Compute un-normalized probabilities for drawing the second particle for a given first particle.
After running this, the following arguments will be set:
idxs_possible_second_particle
no_idxs_possible_second_particle
Expand All @@ -152,7 +148,6 @@ def compute_probs_second_particle(self, idx_first_particle, with_normalization=F
Args:
idx_first_particle (int): the index of the first particle in 0,1,...,n-1
with_normalization (bool): whether the probabilities will be normalized
"""

# Not normalized probs for second particle probs
Expand All @@ -166,11 +161,14 @@ def compute_probs_second_particle(self, idx_first_particle, with_normalization=F
# Probs
self.probs_second_particle = np.concatenate((self._gauss[true_false_0],self._gauss[true_false_1]))

# Normalization
if with_normalization:
self.are_probs_second_particle_normalized = True
norm = np.sum(self.probs_second_particle)
self.probs_second_particle /= norm
else:
self.are_probs_second_particle_normalized = False
self.max_prob_second_particle = max(self.probs_second_particle)
self.are_probs_second_particle_normalized = False
self.max_prob_second_particle = max(self.probs_second_particle)

def normalize_probs_second_particle(self):
"""Normalize the probs for the second particle
"""

self.are_probs_second_particle_normalized = True
norm = np.sum(self.probs_second_particle)
self.probs_second_particle /= norm
self.max_prob_second_particle = 1.0
19 changes: 5 additions & 14 deletions samplePairsGaussian/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,11 @@ def cdf_sample_first_particle(self,compute_probs=True):

# Form probabilities
if compute_probs:
self.prob_calculator.compute_probs_first_particle(with_normalization=True)
self.prob_calculator.compute_probs_first_particle()

# Ensure normalized
if self.prob_calculator.are_probs_first_particle_normalized == False:
self.prob_calculator.are_probs_first_particle_normalized == True
norm = np.sum(self.prob_calculator.probs_first_particle)
self.prob_calculator.probs_first_particle /= norm
self.max_prob_first_particle = 1.0
self.prob_calculator.normalize_probs_first_particle()

# Choose
self.idx_first_particle = np.random.choice(range(0,self.prob_calculator.n), 1, p=self.prob_calculator.probs_first_particle)[0]
Expand All @@ -217,14 +214,11 @@ def cdf_sample_second_particle(self,compute_probs=True):

# Form probabilities
if compute_probs:
self.prob_calculator.compute_probs_second_particle(self.idx_first_particle,with_normalization=True)
self.prob_calculator.compute_probs_second_particle(self.idx_first_particle)

# Ensure normalized
if self.prob_calculator.are_probs_second_particle_normalized == False:
self.prob_calculator.are_probs_second_particle_normalized == True
norm = np.sum(self.prob_calculator.probs_second_particle)
self.prob_calculator.probs_second_particle /= norm
self.max_prob_second_particle = 1.0
self.prob_calculator.normalize_probs_second_particle()

# Choose
self.idx_second_particle = np.random.choice(self.prob_calculator.idxs_possible_second_particle, 1, p=self.prob_calculator.probs_second_particle)[0]
Expand All @@ -250,10 +244,7 @@ def cdf_sample_pair(self, compute_probs_first_particle=True):

# Ensure normalized
if self.prob_calculator.are_probs_first_particle_normalized == False:
self.prob_calculator.are_probs_first_particle_normalized == True
norm = np.sum(self.prob_calculator.probs_first_particle)
self.prob_calculator.probs_first_particle /= norm
self.max_prob_first_particle = 1.0
self.prob_calculator.normalize_probs_first_particle()

# Turn off logging temp
level = self._logger.level
Expand Down

0 comments on commit 1c9a50c

Please sign in to comment.