Example #1
0
def get_pmf_after_prob_length_low_memory(pmf, p):
    """
    This single function acts as a low memory version of the following two
    functions combined.
    - get_pmfs_after_fixed_lengths(pmf)
    - get_pmf_after_prob_length(pmfs_fixed_lenghts, p)
    The lower memory usage, O(trunc) instead of O(trunc^2), is achieved by not
    storing all the intermediate results from `get_pmfs_after_fixed_lengths()`.

    Parameters
    ----------
    pmf : 1D numpy array
        Input pmf (possibly truncated) of some random variable X,
        such that pmf[x] = Pr(X = x).
    p : float
        Probability such that the lenght of this sum ~ geom(p).

    Returns
    -------
    1D numpy array
        Probability mass function `pmf` such that
        pmf[x] = Pr(sum_{j=1}^N X_j = x), with N ~ geom(p)
    """
    trunc = len(pmf)
    pmf_temp = np.copy(pmf)
    pmf_final = np.zeros(trunc)
    for s in range(1, trunc):
        if (scipy_geom.pmf(s, p) == 0):
            break
        pmf_final = np.add(pmf_final, scipy_geom.pmf(s, p) * pmf_temp)
        pmf_temp = sum_distributions(pmf_temp, pmf)[:trunc]
    return pmf_final
Example #2
0
def plot_geom(n, p):
    plt.plot(range(1, n + 1),
             geom.pmf(range(1, n + 1), p),
             alpha=0.6,
             color='gray')
    plt.plot(range(1, n + 1),
             geom.pmf(range(1, n + 1), p),
             'o',
             label='$p = %s$' % p)
Example #3
0
    def init_transition_matrix(self):
        max_step     = self.max_step
        max_n        = self.allele_range + max_step
        min_n        = -self.allele_range - max_step
        N            = 2*self.allele_range + 2*max_step + 1
        trans_matrix = numpy.zeros((N, N))
        steps        = numpy.arange(1, 2*max_n+1, 1)
        step_probs   = geom.pmf(steps, self.p_geom)

        # Fill in transition matrix                                                                                                                                             
        for i in xrange(min_n+1, max_n):
            up_prob      = min(1, max(0, 0.5*(1-self.beta*self.p_geom*i)))
            down_prob    = 1.0-up_prob
            lrem         = sum(step_probs[i-min_n-1:])
            rrem         = sum(step_probs[max_n-i-1:])
            trans_matrix[:,i-min_n] = numpy.hstack((numpy.array([self.mu*down_prob*lrem]),
                                                    self.mu*down_prob*numpy.array(step_probs[:i-min_n-1][::-1]),
                                                    numpy.array([1-self.mu]),
                                                    self.mu*up_prob*numpy.array(step_probs[:max_n-i-1]),
                                                    numpy.array([self.mu*up_prob*rrem])))

        # Add boundaries to prevent probability leakage
        trans_matrix[:,0]     = 0
        trans_matrix[0,0]     = 1
        trans_matrix[:,N-1]   = 0
        trans_matrix[N-1,N-1] = 1
                        
        # Convert to matrix, or numpy functions won't do appropriate thing, and save for later use
        self.trans_matrix = numpy.matrix(trans_matrix)

        # Save various matrix-related variables
        self.min_n = min_n
        self.max_n = max_n
        self.N     = N
Example #4
0
    def construct_priors_v1(self, truncate=False):
        # construct array of arrays with different length of ranges, we need this to construct a 2D matrix that will be
        # passed to the geometric PMF function of scipy
        # !!! NOTE: halting_steps are already increased but not our step
        current_step = self.step + 1
        R = np.array([
            np.arange(1, i + 1) for i in self.halting_steps.data.cpu().numpy()
        ])
        if current_step > 1:
            R = np.vstack([
                np.lib.pad(a, (0, (current_step - len(a))),
                           'constant',
                           constant_values=0) for a in R
            ])
        if self.type_prior == "geometric":
            g_priors = geom.pmf(R, p=(1 - self.prior_shape_param1))

        else:
            raise ValueError(
                "Unknown prior distribution {}. Only 1) geometric and 2) neg-binomial "
                "are supported".format(self.type_prior))
        # print("Halting steps {}".format(np.array_str(self.halting_steps.data.cpu().numpy())))
        # print("R")
        # print("{}".format(np.array(R)))
        if truncate:
            g_priors = 1. / np.sum(g_priors) * g_priors
        g_priors = Variable(torch.from_numpy(g_priors).double())
        if self.q_t.is_cuda:
            g_priors = g_priors.cuda()
        return g_priors
Example #5
0
def computePrior(ltl, lamb=0.9, conjoin=False):
    """ Returns the log-prior of current LTL sample

        INPUTS:
            ltl   - current ltl dict
            lamb    - lambda parameter for geometric distribution
            conjoin   - whether or not conjunctions of templates are being considered

        OUTPUT:
            log P(ltl)

     """

    # LTL template prior
    log_template = math.log(ltl['prob'])

    if conjoin:
        # Complexity based on number of conjunctions
        num_conjuncts = len(ltl['props_list'])
        complexity = geom.pmf(num_conjuncts, 1 - lamb)
        # complexity = poisson(3).pmf(num_conjuncts)
        try:
            log_complexity = math.log(complexity)
        except ValueError:
            log_complexity = -1000
        return log_template + log_complexity
    else:
        return log_template
Example #6
0
    def add_damage_rev(self, geom_p, scale_min, scale_max):

        insert = list(self.revcom)
        insertlen = len(self.revcom)
        x = np.arange(1, insertlen + 1)
        geom_dist = geom.pmf(x, geom_p)
        # geom_dist = scale(geom.pmf(x, geom_p), scale_min, scale_max)

        for j in range(0, insertlen):
            pos = j
            opp_pos = insertlen - 1 - j

            # C -> T deamination - deamination
            if npr.rand() <= scale_max:
                if insert[pos] == "C" and geom_dist[j] >= npr.rand():
                    insert[pos] = "T"

            # C -> T deamination - baseline
            if npr.rand() <= scale_min:
                if insert[pos] == "C":
                    insert[pos] = "T"

            # G -> A deamination
            if npr.rand() <= scale_max:
                if insert[opp_pos] == "G" and geom_dist[j] >= npr.rand():
                    insert[opp_pos] = "A"

            # C -> T deamination - baseline
            if npr.rand() <= scale_min:
                if insert[pos] == "G":
                    insert[pos] = "A"

        self.revcom = "".join(insert)
        return (self.revcom)
Example #7
0
    def init_transition_matrix(self):
        max_step     = self.max_step
        max_n        = self.allele_range + max_step
        min_n        = -self.allele_range - max_step
        N            = 2*self.allele_range + 2*max_step + 1
        trans_matrix = numpy.zeros((N, N))
        steps        = numpy.arange(1, 2*max_n+1, 1)
        step_probs   = geom.pmf(steps, self.p_geom)

        # Fill in transition matrix                                                                                                                                             
        for i in xrange(min_n+1, max_n):
            up_prob      = min(1, max(0, 0.5*(1-self.beta*self.p_geom*i)))
            down_prob    = 1.0-up_prob
            lrem         = sum(step_probs[i-min_n-1:])
            rrem         = sum(step_probs[max_n-i-1:])
            mutrate      = 10**(numpy.log10(self.mu)+self.len_coeff*(i-self.a0))
            trans_matrix[:,i-min_n] = numpy.hstack((numpy.array([mutrate*down_prob*lrem]),
                                                    mutrate*down_prob*numpy.array(step_probs[:i-min_n-1][::-1]),
                                                    numpy.array([1-mutrate]),
                                                    mutrate*up_prob*numpy.array(step_probs[:max_n-i-1]),
                                                    numpy.array([mutrate*up_prob*rrem])))

        # Add boundaries to prevent probability leakage
        trans_matrix[:,0]     = 0
        trans_matrix[0,0]     = 1
        trans_matrix[:,N-1]   = 0
        trans_matrix[N-1,N-1] = 1
                        
        # Convert to matrix, or numpy functions won't do appropriate thing, and save for later use
        self.trans_matrix = numpy.matrix(trans_matrix)

        # Save various matrix-related variables
        self.min_n = min_n
        self.max_n = max_n
        self.N     = N
Example #8
0
def disperse_obs_dual_geom(obs, max_range, p):
    '''
    In: observation array, maximum range away from observation dispersed, p_geometric
    Out: new observation array weighted by probability at that base
    '''

    T = np.size(obs, axis=0)

    mut_range = 50
    geom_parameter = 0.05
    half_prob_vec = np.zeros(mut_range + 1)
    mut_prob_vector = np.zeros(mut_range * 2 + 1)

    for i in range(1, mut_range + 2):
        half_prob_vec[i - 1] = geom.pmf(i, geom_parameter)

    mut_prob_vector[mut_range:] = half_prob_vec
    mut_prob_vector[0:mut_range] = np.flip(half_prob_vec[1:], axis=0)
    mut_prob_vector /= np.sum(mut_prob_vector)

    obs_out = np.zeros(T)

    for t in range(0, T):
        value = obs[t]
        obs_out[t - mut_range:t + mut_range] = value * mut_prob_vector

    return obs_out
Example #9
0
def GetStepSizeProb(a1, a2, beta, p):
    step_size = (a2-a1)
    up_prob = max([0.01,0.5*(1-beta*p*a1)]) # Minimum value is 0.01 (allow for minimal probability of expansion at large alleles)
    up_prob = min(up_prob, 0.99) # Maximum value is 0.99 (allow for minimal probability of contraction at small alleles)
    down_prob = 1-up_prob
    if step_size>0: dir_prob = up_prob
    else: dir_prob = down_prob
    step_prob = geom.pmf(abs(step_size), p)
    return dir_prob*step_prob
 def __set_ground_distribution(self):
     """Sets the waiting time pmf of T_0 to geom(pgen), and the sets the
     constant Werner parameter on the ground level if set.
     """
     for t in range(self.trunc+1):
         self.pmf[0,t] = scipy_geom.pmf(t, self.params['pgen'])
     self.pmf_total[0] = self.pmf[0]
     if(self.params['w0'] is not None):
         self.wern[0].fill(self.params['w0'])
Example #11
0
    def plot(self):
        p = 0.5
        mean, var, skew, kurt = geom.stats(p, moments='mvsk')

        x = np.arange(geom.ppf(0.01, p), geom.ppf(0.99, p))
        ax.plot(x, geom.pmf(x, p), 'bo', ms=8, label='geom pmf')
        ax.vlines(x, 0, geom.pmf(x, p), colors='b', lw=5, alpha=0.5)

        rv = geom(p)
        ax.vlines(x,
                  0,
                  rv.pmf(x),
                  colors='k',
                  linestyles='-',
                  lw=1,
                  label='frozen pmf')
        ax.legend(loc='best', frameon=False)
        plt.show()
Example #12
0
def GetStepSizeProb(a1, a2, beta, p):
    step_size = (a2 - a1)
    up_prob = max([0.01, 0.5 * (1 - beta * p * a1)])  # Minimum value is 0.01
    up_prob = min(up_prob, 0.99)  # Maximum value is 0.99
    down_prob = 1 - up_prob
    if step_size > 0: dir_prob = up_prob
    else: dir_prob = down_prob
    step_prob = geom.pmf(abs(step_size), p)
    return dir_prob * step_prob
 def get_duration_pdf(self, duration=None, state=None):
     """
     checked. Computes probability of remaining in state for given duration. 
     """
     if self.duration_distribution_name == 'poisson':
         return poisson.pmf(duration, self.state_duration_means[state])
     if self.duration_distribution_name == 'geometric':
         # Note there are two "geometric distributions" https://en.wikipedia.org/wiki/Geometric_distribution
         # this is scipy's default and we found it to perform better on real-world data, but it's easy to swap out for the other if you prefer.
         return geom.pmf(duration, 1. / self.state_duration_means[state])
Example #14
0
def stat_test(miss, params):
    cnt = sorted(Counter(miss).items(), key=lambda t: t[0])
    print(','.join([str(k) for k, _ in cnt]))
    x, freq = zip(*cnt)
    x, freq = np.array(x), np.array(freq)
    freq = freq / sum(freq)
    pmf = geom.pmf(x, *params)
    _, pvalue = chisquare(freq, pmf)
    # logmsg('p-value = %f', pvalue)
    return pmf[1], pvalue
Example #15
0
def pdcdsh_sampling(**kwargs):
    """Generates U_1, Z_2, ..., Z_n until U_1 < P(Z_1 = n - sum_{i>=2} i*Z_i) / P(Z_1 = 0)

    Probabilistic divide-and-conquer deterministic second half method (PDCDSH) by Arratia and DeSalvo.
    Z_i is Geometric 1-x^i, where x can be any number between 0 and 1, but best to use x=exp(-pi / sqrt(6n)).
    For integer partition, using index i=1 is optimal.

    kwargs needs to have 'target', 'tilt', and optionally 'size' for number of samples (by default 1).
    """

    size = 1 if 'size' not in kwargs else kwargs['size']

    sample_list = []
    count_list = []
    n = kwargs['target']
    x = kwargs['tilt']
    for i in range(size):
        partition = {}
        counts = 0
        keep_going = True
        while keep_going is True:

            geom_rvs = [
                int(numpy.floor(numpy.log(u) / ((i + 1) * numpy.log(x))))
                for i, u in enumerate(uniform().rvs(n))
            ]
            partition = {(i + 2): y
                         for i, y in enumerate(geom_rvs[1:]) if y != 0}

            U = uniform().rvs(size=1)
            residual = int(n - numpy.sum([x * y
                                          for x, y in partition.items()]))
            if U < geom.pmf(residual, 1 - x, -1) / geom.pmf(0, 1 - x, -1):
                keep_going = False
                if residual > 0:
                    partition[1] = residual
            counts += 1

        sample_list.append(partition)
        count_list.append(counts)

    return [sample_list, count_list]
def stat_test(miss, params):
    cnt = sorted(Counter(miss).items(), key=lambda t: t[0])
    x, freq = zip(*cnt)
    x, freq = np.array(x), np.array(freq)
    freq = freq / sum(freq)
    # TODO: freq key to match https://oeis.org/A186323?
    print(','.join([str(k) for k, _ in cnt]))
    print(','.join(map(str, freq)))
    pmf = geom.pmf(x, *params)
    _, pvalue = chisquare(freq, pmf)
    logmsg('p-value = %f', pvalue)
    return pmf[1], pvalue
Example #17
0
    def test_geom(self):
        from scipy.stats import geom
        import matplotlib.pyplot as plt
        fig, ax = plt.subplots(1, 1)

        p = 0.5
        mean, var, skew, kurt = geom.stats(p, moments='mvsk')

        x = np.arange(geom.ppf(0.01, p), geom.ppf(0.99, p))
        ax.plot(x, geom.pmf(x, p), 'bo', ms=8, label='geom pmf')
        ax.vlines(x, 0, geom.pmf(x, p), colors='b', lw=5, alpha=0.5)

        rv = geom(p)
        ax.vlines(x,
                  0,
                  rv.pmf(x),
                  colors='k',
                  linestyles='-',
                  lw=1,
                  label='frozen pmf')
        ax.legend(loc='best', frameon=False)
        self.assertEqual(str(ax), "AxesSubplot(0.125,0.11;0.775x0.77)")
def biased_permutation(nItems=20,nBiased=10,bias=0.35,addRepsTotal=10,minReps=1):
    nBiased = min(nItems,nBiased)
    excess = np.round(geom.pmf(np.arange(1,nBiased+1),bias)*addRepsTotal)

    perm = []
    nReps = np.ones(nItems,dtype=np.int32)*minReps
    for i in range(nItems):
        if i < nBiased:
            nReps[i] += excess[i].astype(np.int32)
        perm.extend(list(np.ones(nReps[i],dtype=np.int32)*i))
    revPerm = [(nItems - 1) - x for x in perm]

    return np.random.permutation(revPerm),nReps
Example #19
0
def gsn_likelihood(X,mu,sig,p):
	ret=1.0
	for x in X:
		val=0.0
		for t in range(1,100):
			val += norm.pdf(x,loc=t*mu,scale=math.sqrt(t)*sig)*geom.pmf(t,p)
			# print("norm=",norm.pdf(x,loc=t*mu,scale=math.sqrt(t)*sig),"geo=",p*(1-p)**(t-1))
		# print("val=",val)	
		if val>1e-5:
			ret*=val
	# print("ret=",ret)		
	if ret==1.0:
		return 0
	else:
		return ret	
 def prob(self, pid, cid):
     # return distribution p(tag, phrase | context) as vector of length |tags|
     phrase = phrase_type_list[pid]
     dist = zeros(num_tags)
     for t in range(num_tags):
         prob = self.tagDist[cid][t]
         f = self.phraseLengthDist[t]
         prob *= geom.pmf(len(phrase), f)
         if len(phrase) == 1:
             prob *= self.phraseSingleDist[t][phrase[0]]
         else:
             prob *= self.phraseLeftDist[t][phrase[0]]
             prob *= self.phraseRightDist[t][phrase[-1]]
         dist[t] = prob
     return dist
 def prob(self, pid, cid):
     # return distribution p(tag, phrase | context) as vector of length |tags|
     phrase = phrase_type_list[pid]
     dist = zeros(num_tags)
     for t in range(num_tags):
         prob = self.tagDist[cid][t]
         f = self.phraseLengthDist[t]
         prob *= geom.pmf(len(phrase), f)
         if len(phrase) == 1:
             prob *= self.phraseSingleDist[t][phrase[0]]
         else:
             prob *= self.phraseLeftDist[t][phrase[0]]
             prob *= self.phraseRightDist[t][phrase[-1]]
         dist[t] = prob
     return dist
Example #22
0
def generate_fixed_weights(exper, steps=None):

    if steps is None:
        steps = exper.args.optimizer_steps

    fixed_weights = None
    if exper.args.learner == 'meta' and (exper.args.version[0:2] == "V3"
                                         or exper.args.version[0:2] == "V5"):
        # Version 3.1 of MetaLearner uses a fixed geometric distribution as loss weights
        if exper.args.version == "V3.1":
            exper.meta_logger.info(
                "Model with fixed weights from geometric distribution p(t|{},{:.3f})"
                .format(steps, exper.config.ptT_shape_param))
            prior_probs = geom.pmf(np.arange(1, steps + 1),
                                   p=(1 - exper.config.ptT_shape_param))
            prior_probs = 1. / np.sum(prior_probs) * prior_probs
            prior_probs = np.array(
                [0.607, 0.3749, 0.1777, 0.0779, 0.0288, 0.0215, 0.0145])
            prior_probs = Variable(torch.from_numpy(prior_probs).float())
            if exper.args.cuda:
                prior_probs = prior_probs.cuda()

            fixed_weights = prior_probs.squeeze()
            exper.meta_logger.info(fixed_weights)

        elif exper.args.version == "V3.2":
            fixed_weights = Variable(torch.FloatTensor(steps),
                                     requires_grad=False)
            fixed_weights[:] = 1. / float(steps)
            exper.meta_logger.info(
                "Model with fixed uniform weights that sum to {:.1f}".format(
                    torch.sum(fixed_weights).data.cpu().squeeze()[0]))
        elif exper.args.version[0:2] == "V5":
            # in metaV5 we construct the loss-weights based on the RL approach for cumulative discounted reward
            # we take gamma = ptT_shape_param - that we normally use to construct the prior geometric distribution
            weights = [
                Variable(torch.FloatTensor([exper.config.ptT_shape_param**i]))
                for i in np.arange(steps)
            ]
            fixed_weights = torch.cat(weights)
    else:
        fixed_weights = Variable(torch.ones(steps))

    if exper.args.cuda and not fixed_weights.is_cuda:
        fixed_weights = fixed_weights.cuda()

    return fixed_weights
Example #23
0
    def construct_priors_v2(self):
        """
        Just get the p(t) values for the mini-batch using the indices of the "halting step" vector.
        Note: the prior is not truncated
        :return: prior values for optimizees at time step = halting step
        """

        if self.type_prior == "geometric":
            g_priors = geom.pmf(self.halting_steps.data.cpu().numpy(),
                                p=(1 - self.prior_shape_param1))
            # g_priors = nbinom.pmf(self.halting_steps.data.cpu().numpy(), 50, p=0.3)
        else:
            raise ValueError(
                "Unknown prior distribution {}. Only 1) geometric and 2) neg-binomial "
                "are supported".format(self.type_prior))
        g_priors = Variable(torch.from_numpy(g_priors).double())
        if self.rho_t.is_cuda:
            g_priors = g_priors.cuda()
        return g_priors
Example #24
0
def simulate_mentor_data(n_mentors: int, n_ventures: int, capacity_m: int,
                         capacity_v: int, proportion_of_uniform_dist: float,
                         pmf_proba_m: float) -> pd.DataFrame:
    '''
    Return a dataframe of simulated mentor data for each mentor, with mentor capacity and mentor rankings.

    n_mentor: number of mentors
    n_ventures: number of ventures
    capacity_m: capacity of mentors
    capacity_v: capacity of ventures
    proportion_of_uniform_dist: a float between (0,1) representing percentage of sample that is drawn from a uniform distribution
    pmf_proba_m: a float between (0,1) representing probability of success for the pmf of a geometric distribution

    '''
    mentor_col = np.arange(0, n_mentors).reshape(-1, 1)
    capacity_m_col = np.full(n_mentors, capacity_m).reshape(-1, 1)
    arr_m = np.empty((n_mentors, n_ventures))

    # First 60% of mentors will select from a uniform distribution of ventures
    split_index = int(arr_m.shape[0] * proportion_of_uniform_dist)
    # np.random.seed(42)
    arr_m[:split_index] = [
        np.random.choice(np.arange(n_ventures), n_ventures, replace=False)
        for i in range(0, split_index)
    ]

    # Next 40% of mentors will select from a geometric distribution of ventures
    proba = geom.pmf(np.arange(n_ventures), pmf_proba_m)

    # Ensure probability sums to 1. Each venture has non-zero probability to be selected.
    proba[0] += 1 - np.sum(proba)
    proba = sorted(proba, reverse=True)
    arr_m[split_index:] = [
        np.random.choice(a=np.arange(n_ventures),
                         size=n_ventures,
                         p=proba,
                         replace=False)
        for i in range(0, arr_m.shape[0] - split_index)
    ]
    return pd.DataFrame(
        np.concatenate([mentor_col, capacity_m_col, arr_m], axis=1))
Example #25
0
    def __init__(self, p_geom, p_down, p_up, tolerance=10**-6):
        self.p_geom = p_geom
        self.p_up = p_up
        self.p_down = p_down

        # Check validity of specified probabilities
        if p_geom <= 0 or p_geom > 1:
            exit("p_geom must be in (0, 1]")
        if p_up < 0 or p_up > 1:
            exit("p_up must be in [0, 1]")
        if p_down < 0:
            exit("p_down must be in [0, 1]")
        if p_up + p_down > 1:
            exit("p_up + p_down must be <= 1")

        if p_geom == 1.0:
            StutterModel.__init__(self, [-1, 0, 1],
                                  [p_down, 1.0 - p_down - p_up, p_up])
        else:
            # Determine number of steps required to achieve desired tolerance
            prob = p_geom
            tot_rem = 1.0
            max_step = 0
            while tot_rem > tolerance:
                tot_rem -= prob
                prob *= (1 - p_geom)
                max_step += 1

            # Compute step probabilities
            steps = numpy.arange(1, max_step + 1, 1)
            step_probs = geom.pmf(steps, self.p_geom)
            step_probs[-1] += geom.sf(steps[-1], self.p_geom)

            # Construct the model
            stutter_sizes = list(map(lambda x: -x,
                                     steps[::-1])) + [0] + list(steps)
            stutter_probs = list(p_down * step_probs[::-1]) + [
                1.0 - p_down - p_up
            ] + list(p_up * step_probs)
            StutterModel.__init__(self, stutter_sizes, stutter_probs)
Example #26
0
def get_pmf_after_prob_length(pmfs_fixed_lenghts, p):
    """
    Parameters
    ----------
    pmfs_fixed_lenghts : 2D (square) numpy array
        A square array `pmfs_fixed_lenghts` such that pmfs[k,x] =
        Pr(sum_{j=1}^k X_j = x), where X_j are i.i.d. random variables ~ X.
    p : float
        Probability such that the lenght of this sum ~ geom(p).

    Returns
    -------
    1D numpy array
        Probability mass function `pmf` such that
        pmf[x] = Pr(sum_{j=1}^N X_j = x), with N ~ geom(p)
    """
    trunc = len(pmfs_fixed_lenghts[0])
    pmf_final = np.zeros(trunc)
    for s in range(1, trunc):
        pmf_final = np.add(pmf_final,
                           scipy_geom.pmf(s, p) * pmfs_fixed_lenghts[s])
    return pmf_final
Example #27
0
 def angle(self,features,weights=None,method='sum',method_param=1.0):
     if method == 'sum':
         v1 = self.single.vector(features,weights=weights,method=method)
         v2 = self.double.vector(features,weights=weights,method=method)
         angle = self.find_angle(v1,v2)
     elif method == 'nn': # Nearest-Neighbor.  
         m1 = self.single.matrix(features,weights=weights)
         m2 = self.double.matrix(features,weights=weights)
         angles = []
         for i in range(m1.shape[0]):
             angles_i = []
             for j in range(m2.shape[0]):
                 one_angle = self.find_angle(m1[i,:],m2[j,:])
                 if np.isnan(one_angle):
                     one_angle = 1.0 
                 angles_i.append(one_angle)
             angles_i = np.array(sorted(angles_i))
             from scipy.stats import geom
             weights_i = geom.pmf(range(1,len(angles_i)+1),method_param)
             angles.append(np.dot(angles_i,weights_i))
         angle = np.abs(angles).mean()#circmean(angles)
     return angle
Example #28
0
    def angle(self, features, weights=None, method="sum", method_param=1.0):
        if method == "sum":
            v1 = self.single.vector(features, weights=weights, method=method)
            v2 = self.double.vector(features, weights=weights, method=method)
            angle = self.find_angle(v1, v2)
        elif method == "nn":  # Nearest-Neighbor.
            m1 = self.single.matrix(features, weights=weights)
            m2 = self.double.matrix(features, weights=weights)
            angles = []
            for i in range(m1.shape[0]):
                angles_i = []
                for j in range(m2.shape[0]):
                    one_angle = self.find_angle(m1[i, :], m2[j, :])
                    if np.isnan(one_angle):
                        one_angle = 1.0
                    angles_i.append(one_angle)
                angles_i = np.array(sorted(angles_i))
                from scipy.stats import geom

                weights_i = geom.pmf(range(1, len(angles_i) + 1), method_param)
                angles.append(np.dot(angles_i, weights_i))
            angle = np.abs(angles).mean()  # circmean(angles)
        return angle
Example #29
0
    def __init__(self, p_geom, p_down, p_up, tolerance=10**-6):
        self.p_geom = p_geom
        self.p_up   = p_up
        self.p_down = p_down

        # Check validity of specified probabilities
        if p_geom <= 0 or p_geom > 1:
            exit("p_geom must be in (0, 1]")
        if p_up < 0 or p_up > 1:
            exit("p_up must be in [0, 1]")
        if p_down < 0:
            exit("p_down must be in [0, 1]")
        if p_up + p_down > 1:
            exit("p_up + p_down must be <= 1")

        if p_geom == 1.0:
            StutterModel.__init__(self, [-1, 0, 1], [p_down, 1.0-p_down-p_up, p_up])
        else:
            # Determine number of steps required to achieve desired tolerance
            prob       = p_geom
            tot_rem    = 1.0
            max_step   = 0
            while tot_rem > tolerance:
                tot_rem  -= prob
                prob     *= (1-p_geom)
                max_step += 1

            # Compute step probabilities
            steps           = numpy.arange(1, max_step+1, 1)        
            step_probs      = geom.pmf(steps, self.p_geom)
            step_probs[-1] += geom.sf(steps[-1], self.p_geom)

            # Construct the model
            stutter_sizes = list(map(lambda x:-x, steps[::-1])) + [0] + list(steps)
            stutter_probs = list(p_down*step_probs[::-1]) + [1.0-p_down-p_up] + list(p_up*step_probs)
            StutterModel.__init__(self, stutter_sizes, stutter_probs)
Example #30
0
def geom_pmf(p, support):
    """ Return truncated geometric pmf with parameter p and length support """
    pmf = np.array([geom.pmf(x, p) for x in range(support)])
    return pmf
num_seats_def = 150
prng = np.random.RandomState(1)  #Pseudorandom number generator
demand_par_a_def = 60.2  #prng.randint(100, size=1).astype(float)
demand_par_b_def = 10.0  #prng.randint(5, size=1).astype(float)
demand_par_c_def = 1.0
epsilons_support_def = 10
pmin_def = 1.0

p = 0.3
prob_eps_rem = (1 - geom.cdf(epsilons_support_def + 1, p)) / (
    epsilons_support_def * 2 + 1)
prob_eps = []
for i in range(0, epsilons_support_def + 1):
    if i == 0:
        prob_eps.append(geom.pmf(i + 1, p) + prob_eps_rem)
    else:
        prob_eps.append(geom.pmf(i + 1, p) / 2 + prob_eps_rem)

prob_eps_rev = prob_eps.copy()
prob_eps_rev.reverse()
prob_eps_rev.pop()
prob_eps_def = prob_eps_rev + prob_eps

p_r_min_def = 0.8 * pmin_def
gamma_def = 0.9
num_stages_def = 12


class MDP():
    '''
Example #32
0
 def _pmf(self, x, l, p, w):
     return w * poisson.pmf(x, l) + (1 - w) * geom.pmf(x, p, loc=-1)
Example #33
0
        if q==0:
            low = high = 0
        if num_Np_fails<low or num_Np_fails>high:
            print 'N=%d, p=%.3f failed %d of %d checks, outside range (%d, %d)' % (N, p, num_Np_fails,
                                                                                   num_Np_checks, low, high)
print
failrate = float(numfails)/numchecks
low, high = norm.interval(alpha, loc=mu, scale=sqrt(sigma2))
print '%d/%d=%.2f%% failed at %d%%' % (numfails, numchecks, numfails*100.0/numchecks, 100*alpha)
print 'Expected mean=%d, std dev=%d (mean fail rate=%.2f%%)' % (mu, sqrt(sigma2), 100*mu/numchecks)
if low<=numfails<=high:
    print 'Overall passed at %d%%: within range (%d, %d)' % (alpha*100, low, high)
else:
    print 'Overall failed at %d%%: outside range (%d, %d)' % (alpha*100, low, high)

figure(figsize=(10, 6))
plotnum = 0
for p in p_range:
    if p==0 or p==1:
        continue
    plotnum += 1
    subplot(2, 3, plotnum)
    n = arange(1, isi_max[p])
    plot(n, isi[p][1:], '-g', lw=3, label='Observed')
    plot(n, geom.pmf(n, p)*num_isi[p], '-k', label='Expected')
    xlabel('intersynapse interval')
    yticks([])
    title('p = %.3f' % p)
tight_layout()
show()
    def generate_graph_data(self):
        ageGroup = self.tableModel.data[self.selected_item_index.row()][0]
        parameter = self.tableModel.data[self.selected_item_index.row()][1]
        p1 = self.temporaryParametersDict[ageGroup][parameter]["p1"]
        p2 = self.temporaryParametersDict[ageGroup][parameter]["p2"]

        distributionType = self.temporaryParametersDict[ageGroup][parameter][
            "distributionType"]
        xyDict = {"x": [], "y": []}
        try:
            if distributionType == 'Binomial':
                xyDict["x"] = np.arange(binom.ppf(0.01, int(p1), p2 / 100),
                                        binom.ppf(0.99, int(p1), p2 / 100))
                xyDict["y"] = binom.pmf(xyDict["x"], int(p1), p2 / 100)
            elif distributionType == 'Geometric':
                xyDict["x"] = np.arange(geom.ppf(0.01, p1 / 100),
                                        geom.ppf(0.99, p1 / 100))
                xyDict["y"] = geom.pmf(xyDict["x"], p1 / 100)
                if p2 != 0:
                    self.tableModel.setData(
                        self.selected_item_index.sibling(
                            self.selected_item_index.row(), 3), 0, Qt.EditRole)
            elif distributionType == 'Laplacian':
                xyDict["x"] = np.arange(dlaplace.ppf(0.01, p1 / 100),
                                        dlaplace.ppf(0.99, p1 / 100))
                xyDict["y"] = dlaplace.pmf(xyDict["x"], p1 / 100)
                if p2 != 0:
                    self.tableModel.setData(
                        self.selected_item_index.sibling(
                            self.selected_item_index.row(), 3), 0, Qt.EditRole)
            elif distributionType == 'Logarithmic':
                xyDict["x"] = np.arange(logser.ppf(0.01, p1 / 100),
                                        logser.ppf(0.99, p1 / 100))
                xyDict["y"] = logser.pmf(xyDict["x"], p1 / 100)
                if p2 != 0:
                    self.tableModel.setData(
                        self.selected_item_index.sibling(
                            self.selected_item_index.row(), 3), 0, Qt.EditRole)
            elif distributionType == 'Neg. binomial':
                xyDict["x"] = np.arange(nbinom.ppf(0.01, p1, p2 / 100),
                                        nbinom.ppf(0.99, p1, p2 / 100))
                xyDict["y"] = nbinom.pmf(xyDict["x"], p1, p2 / 100)
            elif distributionType == 'Planck':
                xyDict["x"] = np.arange(planck.ppf(0.01, p1 / 100),
                                        planck.ppf(0.99, p1 / 100))
                xyDict["y"] = planck.pmf(xyDict["x"], p1 / 100)
                if p2 != 0:
                    self.tableModel.setData(
                        self.selected_item_index.sibling(
                            self.selected_item_index.row(), 3), 0, Qt.EditRole)
            elif distributionType == 'Poisson':
                xyDict["x"] = np.arange(poisson.ppf(0.01, p1),
                                        poisson.ppf(0.99, p1))
                xyDict["y"] = poisson.pmf(xyDict["x"], p1)
                if p2 != 0:
                    self.tableModel.setData(
                        self.selected_item_index.sibling(
                            self.selected_item_index.row(), 3), 0, Qt.EditRole)
            elif distributionType == 'Uniform':
                if p1 - 0.5 * p2 < 0:
                    p2 = p1
                min = p1 - 0.5 * p2
                max = p1 + 0.5 * p2
                xyDict["x"] = np.arange(randint.ppf(0.01, min, max),
                                        randint.ppf(0.99, min, max))
                xyDict["y"] = randint.pmf(xyDict["x"], min, max)
            elif distributionType == 'Zipf (Zeta)':
                xyDict["x"] = np.arange(zipf.ppf(0.01, p1), zipf.ppf(0.99, p1))
                xyDict["y"] = zipf.pmf(xyDict["x"], p1)
                if p2 != 0:
                    self.tableModel.setData(
                        self.selected_item_index.sibling(
                            self.selected_item_index.row(), 3), 0, Qt.EditRole)
            self.update_graph(xyDict)
        except Exception as E:
            log.error(E)
Example #35
0
 def dist(self, pars):
     t = np.arange(1, 300)
     p = pars.get('p_stop', .1)
     pr = geom.pmf(t, p)
     return pr
beta = 4.67873  # Mathematica solution!

######## hospitalization times
hosp_time_dist = np.zeros(len(t))

#################### computation
for k in range(200):  ### sum over geometric distribution

    # determine deterministic hitting time of k infected individuals
    tdet = np.log((k + 1) * alpha * beta * p_surv) / alpha

    if (tdet <= 0): tdet = 0

    # add hospitalisation time distribution to tdet
    index = int(tdet / dt)
    hosp_time_dist[index:] += geom.pmf(k + 1, p_hosp) * gamma.pdf(
        t[0:(len(t) - index)], shape_hosp, scale=scale_hosp)

####### epidemic size distribution
dsize = 50
I = np.arange(dsize, 15001, dsize)
dens_I = np.zeros(len(I))

ind_old = 0
for i in range(len(I)):
    tdet_index = int(
        np.round(np.log(I[i] * alpha * beta * p_surv) / alpha / dt)) + 1

    dens_I[i] = np.sum(hosp_time_dist[ind_old:tdet_index] * dt) / dsize

    ind_old = tdet_index
Example #37
0
import numpy as np
from scipy.stats import geom
import matplotlib.pyplot as plt

fig, ax = plt.subplots(1, 1)
p = 0.5
mean, var, skew, kurt = geom.stats(p, moments='mvsk')
x = np.arange(geom.ppf(0.01, p),geom.ppf(0.99, p))
ax.plot(x, geom.pmf(x, p), 'bo', ms=8, label='geom pmf')
ax.vlines(x, 0, geom.pmf(x, p), colors='b', lw=5, alpha=0.5)
plt.show()