def KMR_Markov_matrix_simultaneous(N, p, epsilon): """ Generate the Markov matrix for the KMR model with *simultaneous* move Parameters ---------- N : int Number of players p : float Level of p-dominance of action 1, i.e., the value of p such that action 1 is the BR for (1-q, q) for any q > p, where q (1-q, resp.) is the prob that the opponent plays action 1 (0, resp.) epsilon : float Probability of mutation Returns ------- P : numpy.ndarray Markov matrix for the KMR model with simultaneous move Notes ----- For simplicity, the transition probabilities are computed under the assumption that a player is allowed to be matched to play with himself. """ P = np.empty((N+1, N+1), dtype=float) for n in range(N+1): P[n, :] = \ (n/N < p) * binom.pmf(range(N+1), N, epsilon/2) + \ (n/N == p) * binom.pmf(range(N+1), N, 1/2) + \ (n/N > p) * binom.pmf(range(N+1), N, 1-epsilon/2) return P
def nextround(playpos, dround): thisround = weightmatrix.playpos.filter(round == dround) nextround = weightmatrix.playpos.filter(round == dround+1) ##Setting picks by round if dRound % 2 == 0: m = 8 n = 4 else: m = 10 n = 5 ##Calculating weights based on binomial probability if dRound % 2 == 0: pickprob = [] for i in range(m+1): prob = 0 for l in range(max(0,i-4),min(4,i)+1): prob = prob + binom.pmf(l,n,thisround)*binom.pmf(i-l,m-n,nextround) pickprob.append(prob) else: pickprob = [] for i in range(m+1): prob = 0 for l in range(max(0,i-5),min(5,i)+1): prob = prob + binom.pmf(l,n,thisround)*binom.pmf(i-l,m-n,nextround) pickprob.append(prob) ##Setting probabilities to be cumulative, i.e. now it will describe the probability that ##that player will still be available by next round for i in range(1,len(pickprob)): pickprob[i] = pickprob[i-1] + pickprob[i] return pickprob
def plotMLE(): """ Plots a binomial distribution, and another with parameters estimated from samples from the first distribution. """ # Number of samples NumSamples = 10 # Number of independent experiments N = 10 # Probability of success mu = 0.2 # Plot the true binomial distribution x = xrange(N + 1) plt.plot(binom.pmf(x, N, mu), label="True Distribution") # Estimate the success probability from samples muML = sum(np.random.binomial(N, mu, NumSamples)) * 1.0 / N / NumSamples # Plot the maximum likelihood distribution plt.plot(binom.pmf(x, N, muML), label="MLE") plt.legend() plt.show()
def KMR_2x2_P_simultaneous(N, p, epsilon): P = np.empty((N+1, N+1), dtype=float) for x in range(N+1): P[x,:] = / (x/N < p) * binom.pmf(range(N+1), N, epsilon/2) + / (x/N == p) * binom.pmf(range(N+1), N, 1/2) + / (x/N > p) * binom.pmf(range(N+1), N, 1-epsilon/2) +/
def set_move(self): self.X = np.zeros((self.n+1,self.n+1)) if self.mode == 'sequential': # 逐次改訂 expay0 = np.empty(2) expay1 = np.empty(2) for k in range(1,self.n): #直前まで0だった人が選ばれた時の行動選択による期待利得 expay0[0] = self.one_pay[0][0]*(self.n-k-1)/(self.n-1)+self.one_pay[0][1]*k/(self.n-1) expay0[1] = self.one_pay[1][0]*(self.n-k-1)/(self.n-1)+self.one_pay[1][1]*k/(self.n-1) #直前まで1だった人が選ばれた時の行動選択による期待利得 expay1[0] = self.one_pay[0][0]*(self.n-k)/(self.n-1)+self.one_pay[0][1]*(k-1)/(self.n-1) expay1[1] = self.one_pay[1][0]*(self.n-k)/(self.n-1)+self.one_pay[1][1]*(k-1)/(self.n-1) if expay1[0] > expay1[1]: self.X[k][k-1]=(k/self.n)*(1-self.epsi*0.5) #k人からk-1人になる確率 self.X[k][k]=(k/self.n)*self.epsi*0.5 elif expay1[0]==expay1[1]: self.X[k][k-1]=(k/self.n)*0.5 self.X[k][k]=(k/self.n)*0.5 else: self.X[k][k-1]= (k/self.n)*self.epsi*0.5 self.X[k][k] = (k/self.n)*(1-self.epsi*0.5) if expay0[1]>expay0[0]: self.X[k][k+1]=((self.n-k)/self.n)*(1-self.epsi*0.5) #k人からk+1人になる確率 self.X[k][k] += ((self.n-k)/self.n)*self.epsi*0.5 #X[k][k]は上でも定めているので上書きでなく加えている elif expay0[1]==expay0[0]: self.X[k][k+1] = ((self.n-k)/self.n)*0.5 self.X[k][k] += ((self.n-k)/self.n)*0.5 else: self.X[k][k+1] = ((self.n-k)/self.n)*self.epsi*0.5 self.X[k][k] += ((self.n-k)/self.n)*(1-self.epsi*0.5) self.X[0][0] = 1-self.epsi*0.5 self.X[0][1] = self.epsi*0.5 self.X[self.n][self.n-1] = self.epsi*0.5 self.X[self.n][self.n] = 1-self.epsi*0.5 elif self.mode == 'simultaneous': # 同時改訂 list=[] for i in range(self.n+1): list.append(i) expay = np.empty(2) for k in range(0,self.n+1): #各人の行動選択による期待利得 expay[0] = self.one_pay[0][0]*(self.n-k)/(self.n)+self.one_pay[0][1]*k/(self.n) expay[1] = self.one_pay[1][0]*(self.n-k)/(self.n)+self.one_pay[1][1]*k/(self.n) if expay[0] > expay[1]: self.X[k] = binom.pmf(list,self.n,self.epsi) elif expay[0] == expay[1]: self.X[k] = binom.pmf(list,self.n,0.5) else: self.X[k] = binom.pmf(list,self.n,1-self.epsi) else: print 'The mode '+ self.mode+'is Unknown. Check your input.'
def compute_posteriors(c, n, p_H1=0.5, iterations=1000, verbose=True): p_H2 = 1.0 - p_H1 p_data_given_H1 = binomial_distribution.pmf(c, n, 0.5) if verbose: print "p(data|H_1) =", p_data_given_H1 # Montecarlo: low = 0.5 high = 1.0 pi = np.random.uniform(low=0.5, high=1.0, size=iterations) p_data_given_H2 = (binomial_distribution.pmf(c, n, pi) * 1.0 / (high-low)).mean() if verbose: print "p(data|H_2) =", p_data_given_H2 p_H1_given_data = (p_data_given_H1 * p_H1) / (p_data_given_H1 * p_H1 + p_data_given_H2 * p_H2) p_H2_given_data = (p_data_given_H2 * p_H2) / (p_data_given_H1 * p_H1 + p_data_given_H2 * p_H2) return p_H1_given_data, p_H2_given_data
def calculate(self, option): if(option.payoffType is PayoffType.call): for i in range(node_number): spot_T = spot * (u ** (nreps - i)) * (d ** (i)) call_T += option.payoff(spot_T, strike) * binom.pmf(nreps - i, nreps, pu) call_price = call_T * np.exp(-rate * expiry) return call_price elif(option.payoffType is PayoffType.put): for i in range(node_number): spot_T = spot * (u ** (nreps - i)) * (d ** (i)) put_T += option.payoff(spot_T, strike) * binom.pmf(nreps - i, nreps, pd) put_price = put_T * np.exp(-rate * expiry) return put_price else: raise ValueError("Wrong Payoff Type")
def test_remaining_gain_sample_gtps(): n = len(ground_truth_pairs) gtps = sorted(gtp_scores.remaining_gain_sample_gtps(max_n=n)) assert len(gtps) == n # if we draw everything the results should always be everything assert gtps == sorted(gtp_scores.remaining_gain_sample_gtps(max_n=n)) # if we don't draw everything it's quite unlikely we get the same result gtps = gtp_scores.remaining_gain_sample_gtps(max_n=5) assert len(gtps) == 5 assert gtps != gtp_scores.remaining_gain_sample_gtps(max_n=5) # make sure we never get items that are fully covered already gtp_scores.gtp_max_precisions[ground_truth_pairs[0]] = 1 c = Counter() k = 100 n = 128 for i in range(k): c.update(gtp_scores.remaining_gain_sample_gtps(max_n=n)) assert ground_truth_pairs[0] not in c assert sum(c.values()) == k * n # count how many aren't in gtps c_not_in = 0 for gtp in ground_truth_pairs[1:]: if gtp not in c: c_not_in += 1 assert c_not_in < 2, \ "it's very unlikely that 2 gtps weren't in our %d samples, " \ "but %d are not" % (k, c_not_in) # near end simulation gtpe_scores = gtp_scores.copy_reset() # set all scores to 1 --> remaining gains to 0 gtpe_scores.gtp_max_precisions = gtpe_scores.get_remaining_gains() high_prob, low_prob = random.sample(gtpe_scores.ground_truth_pairs, 2) # high and low prob refer to the remaining gains and the expected probs to # be selected by remaining gain samples... gtpe_scores.gtp_max_precisions[high_prob] = 0.1 gtpe_scores.gtp_max_precisions[low_prob] = 0.9 assert gtpe_scores.remaining_gain == 1 c = Counter() for i in range(100): c.update(gtpe_scores.remaining_gain_sample_gtps(max_n=1)) assert len(c) == 2 assert sum(c.values()) == 100 assert (binom.pmf(c[high_prob], 100, .9) > 0.001 and binom.pmf(c[low_prob], 100, .1) > 0.001), \ 'expected that high_prob item is drawn with a 9:1 chance, but got:\n' \ 'high: %d, low: %d' % (c[high_prob], c[low_prob])
def triplet_prob(Nw,t,ptriplet,psingle): weights = np.zeros(Nw+1) # number of non-singles must be greater than or equal to t for ns in xrange(t,Nw+1): weights[ns] = 1.0 - binom.cdf(t-1,ns,ptriplet) return sum(weights * binom.pmf(np.linspace(0,Nw,Nw+1),Nw,(1.0-psingle)))
def smooth(arr, n): avgs = empty(len(arr)) pm = binom.pmf(xrange(n * 2), n * 2, 0.5) for i in xrange(len(avgs)): subarr = zero_padded(arr, i - n, i + n) avgs[i] = sum(map(operator.mul, pm, subarr)) return avgs
def E_V(x, u, t, V): wl = list(range(0, n+1)) wpl = np.array(binom.pmf(wl, n, p)) xl = [f(x, u, w) for w in wl] vl = np.array([V[t + 1, xn] for xn in xl]) ev = sum(wpl * vl) return ev
def update_figure(sample_data): # first sample from current posterior beta distribution sample_data = json.loads(sample_data) p = sample_data['sample'] x_binom_pdf = np.arange(0, 10) y_binom_pdf = binom.pmf(x_binom_pdf, n, p) trace = go.Scatter( x=x_binom_pdf, y=y_binom_pdf ) layout_posterior = go.Layout( xaxis={'title': 'X'}, yaxis={'title': 'Prob X', 'range': [0, 1]}, margin={'l': 40, 'b': 40, 't': 10, 'r': 10}, legend={'x': 0, 'y': 1} ) fig2 = dict(data=[trace], layout=layout_posterior) return fig2
def get_risk1(adata, pp1, pp2, i, li, turk_uncer, r, crowd_type = 'single', pp3 = None): """ Risk after doing action 1 (query the crowd) with item i and get label li """ unlab_risk = get_unl_risk(adata, pp1, [i], li, r = r, pp3 = pp3) #for item i if crowd_type == 'all5': # generate 5 crowd labels from the crowd accuracy model: # assume li is the true label # eval the prob that 0,1,2,..5 crowd mems say li pr = [[0,0],[0,0]] for i in range(2): for j in range(2): pr[i][j] = adata.count[i][j] * 1.0 / (adata.count[i][j] + adata.count[1-i][j]) i_risk = 0 for x in range(6): p = binom.pmf(x, 5, pr[li][li]) new_labels = [li]*x p_true = prob_from_crowd_label2(i, adata, pp = None, labels = new_labels) this_i_risk = p_true[1-li] * r[1-li][li] i_risk += p * this_i_risk else: new_labels = [li] p_true = prob_from_crowd_label2(i, adata, pp = None, labels = new_labels) i_risk = p_true[1-li] * r[1-li][li] #res += r[0][1] * pp[i][1] * (li == 0) \ # + r[1][0] * pp[i][0] * (li == 1) # for labelled items #pp = pp2 crowd_risk = get_crowd_risk(adata, pp2, turk_uncer, r = r) return unlab_risk + i_risk + crowd_risk
def posteriorHiddenMemberProbs(data, params_old): """ :type data: Data :param data: :type params_old: Paramters :param params_old: :return: """ n = data.n m = data.m memberProbs = softmaxForData(data.Xm, params_old.Alpha, params_old.a, True) choosingProbs = sigmoidForData(data.Xr, params_old.Beta, params_old.b) if isinstance(m, int): if m == 1: # binary response observedLikelihoods = np.abs((1. - data.y).reshape(n, 1) - choosingProbs) else: observedLikelihoods = binom.pmf(data.y.reshape(n, 1), np.repeat(m, n).reshape(n, 1), choosingProbs) else: raise("Function is not yet implemented for non-integer 'm'.") jointProbs = np.multiply(memberProbs, observedLikelihoods) scales = jointProbs.max(axis=1).reshape(n, 1) scaledJointProbs = jointProbs / scales return scaledJointProbs / scaledJointProbs.sum(axis=1).reshape(n, 1)
def dbinom(x, size=1, prob=0.5, log=False): """ ============================================================================ dbinom() ============================================================================ Density Function for the binomial distribution. Returns the probability of getting "x" successes out of "size" number of trials, given a probability of "prob" for each success. USAGE: dbinom(x, size, prob=0.5, log=False) pbinom(q, size, prob=0.5, lowertail=True, log=False) qbinom(p, size, prob=0.5, lowertail=True, log=False) rbinom(n=1, size=1, prob=0.5) :param x: int. or array of ints. The number of successes :param size: int. Number of trials :param prob: float. Probability of a success :param log: bool. take the log? :return: ============================================================================ """ if log: # note, scipy flips meaning of n and size return binom.logpmf(x, n=size, p=prob, loc=0) else: # note, scipy flips meaning of n and size return binom.pmf(x, n=size, p=prob)
def observedLogLikelihood(data, params): """ :type data: Data :param data: :type params: Paramters :param params: :return: """ n = data.n m = data.m memberProbs = softmaxForData(data.Xm, params.Alpha, params.a, True) choosingProbs = sigmoidForData(data.Xr, params.Beta, params.b) if isinstance(m, int): if m == 1: observedLikelihoods = np.abs((1. - data.y).reshape(n, 1) - choosingProbs) else: observedLikelihoods = binom.pmf(data.y.reshape(n, 1), np.repeat(m, n).reshape(n, 1), choosingProbs) else: raise("Function is not yet implemented for non-integer 'm'.") jointProbs = np.multiply(memberProbs, observedLikelihoods) return np.log(jointProbs.sum(axis=1)).sum()
def binomial_distribution_vectors(n, p, v_size=None): pmf = np.array([binom.pmf(i, n, p) for i in range(n+1)]) cdf = np.array([binom.cdf(i, n, p) for i in range(n+1)]) if (v_size != None) and (v_size > n+1): # padding to the right to reach v_size length pmf = np.append(pmf, np.zeros(v_size-n)) cdf = np.append(cdf, np.ones(v_size-n)) return pmf,cdf
def get_value(x, distr): if distr['distr_name'] == 'binomial': if lookup_pmf.has_key(x): return lookup_pmf[x] else: v = binom.pmf(x, distr['n'], distr['p']) lookup_pmf[x] = v return v
def likelihood(self,hypo,data): idx,k = data N,f = hypo N = N * ((1-f) ** (idx-1)) #1-based if N < k: return 0 val = binom.pmf(k,N,f) return val
def pdf_integral(p1, data): # calculate pdens for range of p1 xj, nj, c, p2, var = data dens = pdf(p1, data=data[2:]) return dens * binom.pmf(xj, nj, p=p1)
def json_prob_anc(self): """return the probability this individual is an X ancestor""" breakpoints = np.arange(1e3) # how many breakpoints there are prob = 1 - np.sum(poisson.pmf(breakpoints, (self.xnrec-1)*XGENLEN) * binom.pmf(0, n=breakpoints+1, p=1/2.0**(self.xnrec-1))) if np.isnan(prob): prob = 1 all_slots = dict([(s, getattr(self, s)) for s in self.__slots__]) return dict(prob_anc=prob, **all_slots)
def __init__ (self, dice_type, num_dice, use_focus=False, use_target_lock=False): self.num_dice = num_dice self.results = [ x for x in range(num_dice+1)] self.probability_of_success = 0 if dice_type == DiceType.RED: self.probability_of_success = get_hit_prob(use_focus, use_target_lock) else: self.probability_of_success = get_evade_prob(use_focus) self.pmf = binom.pmf( self.results, num_dice, self.probability_of_success)
def compare_one(col, cons_aa, aln_size, weights, aa_freqs, pseudo_size): "Column probability using the ball-in-urn model." col_counts = count_col(col, weights, aa_freqs, pseudo_size) col_tot = col_counts['S'] + col_counts['T'] + col_counts['Y'] p_j = (aa_freqs['S'] + aa_freqs['T'] + aa_freqs['Y']) # pvalue = binom.pmf(range(cons_count, aln_size + 1), # aln_size, p_j).sum() pvalue = binom.pmf(range(int(math.ceil(col_tot)), aln_size + 2), aln_size + 1, p_j).sum() return pvalue
def binomialDistanceProb(delta, alpha, beta, prob): if alpha - beta == 0: if delta == 0: return 1 else: # This causes a gradient of 0 among "impossible" distance # values - making gradient ascent impossible. For cases where # gradient ascent is needed, use gradientBinomialDistanceProb return 0 return binom.pmf(delta, alpha - beta, prob)
def gradientBinomialDistanceProb(self, delta, alpha, beta, prob): if alpha - beta == 0: if delta == 0: return 1 else: return self.minimumDistanceProb**(1+delta) return max(min( binom.pmf(delta, alpha - beta, prob), self.maximumDistanceProb), self.minimumDistanceProb)
def compare_one(col, cons_aa, aln_size, weights, aa_freqs, pseudo_size): "Column probability using the ball-in-urn model." # cons_count = col.count(cons_aa) cons_count = count_col(col, weights)[cons_aa] p_j = aa_freqs[cons_aa] cons_count_i = int(ceil(cons_count)) size_i = int(ceil(aln_size)) # pvalue = float(cons_count_i)/len(col) pvalue = binom.pmf(range(cons_count_i, size_i + 1), size_i, p_j).sum() return pvalue
def computeLPPD_components(gam,pnull,y,N,x): '''Returns log-posterior predictive density (LPPD) for extracted gam,pnull from binomial model''' from scipy.stats import binom ll = 0 lppd = [] for i in xrange(5): ll = binom.pmf(y[i], N[i], logist(x[i]*gam + (1-gam)*log_odds(pnull))) lppd.append(np.log(np.mean(ll))) return np.array(lppd)
def mk_matrix1(N, ep, p): #同時改訂 P = np.empty((N+1, N+1)) for i in range(N): if i/N < p: pro = ep/2 elif i/N == p: pro = 1/2 else: pro = 1-ep/2 P[i] = binom.pmf(range(N+1), N, pro) return P
def likelihood(p_in): """ A function to return the likelihood for a given value of p. """ if (testPrior): # Allows us to test the prior, by ignoring the data return 1 elif (not testPrior): # Returns a likelihood score for actual data if (p_in >= 0 and p_in <= 1): return binom.pmf(data,n,p_in) else: return 0 # Returns value of 0 (rather than non) if p outside bounds.
def binomial_variance(n, p): #TODO: should not be here global alt_var_cnt n1 = np.floor(n*p) diff = binom.pmf(np.arange(0, n1), n, p) s = np.sum(diff * np.power(np.arange(0, n1) - n*p, 2)) old_var = n*p*(1-p) ret = (old_var - s) / (1- np.sum(diff)) alt_var_cnt += 1 return ret
def plot_expected_prices( und_vals: List[List[float]], p: float, ticker: str, expiration: str, external_axes: Optional[List[plt.Axes]] = None, ) -> None: """Plot expected prices of the underlying asset at expiration Parameters ---------- und_vals : List[List[float]] The expected underlying values at the expiration date p : float The probability of the stock price moving upward each round ticker : str The ticker of the option's underlying asset expiration : str The expiration for the option external_axes : Optional[List[plt.Axes]], optional External axes (1 axis is expected in the list), by default None """ up_moves = list(range(len(und_vals[-1]))) up_moves.reverse() probs = [100 * binom.pmf(r, len(up_moves), p) for r in up_moves] if external_axes is None: _, ax = plt.subplots(figsize=plot_autoscale(), dpi=cfp.PLOT_DPI) else: if len(external_axes) != 1: logger.error("Expected list of one axis item.") console.print("[red]Expected list of one axis item./n[/red]") return (ax,) = external_axes ax.set_title(f"Probabilities for ending prices of {ticker} on {expiration}") ax.xaxis.set_major_formatter("${x:1.2f}") ax.yaxis.set_major_formatter(mtick.PercentFormatter()) ax.plot(und_vals[-1], probs) theme.style_primary_axis(ax) if external_axes is None: theme.visualize_output()
def euro_binomial_pricer(S, K, r, v, q, T, n, payoff, verbose=True): nodes = n + 1 h = T / n u = np.exp((r - q) * h + v * np.sqrt(h)) d = np.exp((r - q) * h - v * np.sqrt(h)) pstar = (np.exp((r - q) * h) - d) / (u - d) price = 0.0 for i in range(nodes): prob = binom.pmf(i, n, pstar) spotT = S * (u**i) * (d**(n - i)) po = payoff(spotT, K) price += po * prob if verbose: print(f"({spotT:0.4f}, {po:0.4f}, {prob:0.4f})") price *= np.exp(-r * T) return price
def binomialPricer(option, S, r, v, q, n): nodes = n + 1 T = option.expiry K = option.strike h = T / n u = np.exp((r - q) * h + v * np.sqrt(h)) d = np.exp((r - q) * h - v * np.sqrt(h)) pstar = (np.exp((r - q) * h) - d) / (u - d) price = 0.0 for i in range(nodes): prob = binom.pmf(i, n, pstar) spotT = S * (u**i) * (d**(n - i)) po = option.payoff(spotT) price += po * prob price *= np.exp(-r * T) return price
def binom_cal(): n = int(e.get()) p = float(e1.get()) x1 = int(e2.get()) x2 = int(e3.get()) global x x = np.arange(x1, x2) global r r = binom.pmf(x, n, p) print(r) total = 0 for add in r: #total = 0 total += add print(total) global e4 e4 = Entry(root) e4.grid(row=5, column=1) e4.insert(10, str(total))
def Binomial(): binomValues = list(map(float, txt.get().split(','))) n = np.float_(binomValues[0]) p = np.float_(binomValues[1]) x = np.float_(binomValues[2]) x = np.arange(x) #it creates list [0,1,2,3] print(x) result = binom.pmf(x, n, p) #calculate binomial function print( result ) #printing list of result at each value of x ## result at y axis ##probability always betwenn 0 to 1 so on y AXIS value tkes between 0 and 1 plt.plot( x, result, 'o-' ) # at index zero of result of x=0 pmf calculated pmf= (n!/(x!*(n-x)!)) p^x*(1-p)^(1-p) plt.title("Binomial graph graph") plt.xlabel("Number of success") # x-axis lable plt.ylabel("probability of sucess") # y axis lable plt.show() #graph show
def next_round_prob_bravo(self, margin, round_size_prev, round_size, kmin_first, kmin, prob_table_prev): """ Parameters ---------- :param margin: margin of a given race (float in [0, 1]) :param round_size_prev: the size of the previous round :param round_size: the size of the current round :param kmin_first: the kmin for the first round :param kmin: the value of previous kmin :param prob_table_prev: the probability distribution at the begining of the current round :return: prob_table: the probability distribution at the end of the current round is returned """ prob_table = [0] * (round_size + 1) for i in range(kmin + 1): for j in range(min(round_size + 1, round_size - round_size_prev + kmin + 1)): prob_table[j] = prob_table[j] + binom.pmf(j-i, round_size - round_size_prev, (1+margin)/2) * prob_table_prev[i] return prob_table
def _jc_posterior_ng(COUNT_MATRIX, DIST_SAMPLES): MATRIX_SUM = COUNT_MATRIX.sum() MATRIX_DIAGONAL_SUM = COUNT_MATRIX.diagonal().sum() P = np.exp(-DIST_SAMPLES / 100) likelihood = binom.pmf(MATRIX_DIAGONAL_SUM, MATRIX_SUM, P) # This code is not commented in Octave # if (any(isnan(likelihood))) # # In case the binomial is tricky to compute, approx with normal distr! # likelihood = normpdf(k, tot .* p, tot .* p .* (1 .- p)); # endif likelihood[0] /= 2 likelihood[-1] /= 2 POSTERIOR_VEC = likelihood / (likelihood.sum() * (DIST_SAMPLES[1] - DIST_SAMPLES[0])) return POSTERIOR_VEC
def binomfortour(k=11): toursize_max = 60 toursize = np.arange(1, toursize_max,1) #print(toursize) popsize = 800 probabilities = toursize/popsize #print(probabilities) test = binom.pmf(k=k, n=popsize, p=probabilities) #P(X = k) test1 = binom.cdf(k=k, n=popsize, p=probabilities) #P(X <= k) test2 = 1 - binom.cdf(k=k-1, n=popsize, p=probabilities) #P(X >= k) #print(np.round(test, decimals=2)) #print(np.round(test1, decimals=2)) #print(np.round(test2, decimals=2)) plt.bar(x=toursize, height=test1, width=0.6) xticks = np.arange(5, toursize_max, 5) plt.xticks(xticks, xticks) plt.title("P(X >= "+str(k)+")") plt.show()
def binomial_pmf_theoretical(params): """ Computes the PMF for a binomial distribution using the explicit formula Args: params: An object containing two attributes: n and p. These are the distribution parameters Returns: A pandas data frame with 2 columns called 'k' and 'prob_thr'. The first column contains integers from 0 to n representing the number of successful trials. The second column contains the probabilities Pr[X=k]. """ xvals = range( 0, params.n + 1 ) # X is the random variable in Pr[X=k]. It represents number of successes probs = [binom.pmf(k=v, n=params.n, p=params.p) for v in xvals] # Generate p(k) for each possible value of k # Create a pandas dataframe with the results and return res_df = pd.DataFrame(data=list(zip(xvals, probs)), columns=["k", "prob_thr"]) return res_df
def calculate_cVaR(percentile, n, q): """ Calculate the conditional value at risk(cVaR) given confidence level and discrete distribution @percentile::float: confidence level, 0 <= percentile <= 1 @n::int; maximum number of trials @q::int; the probability of getting heads Return::int: the cVaR """ cVaR = 0 mul = -1 / (1 - percentile) # the multiplier in the front mu = n * q currVaR = 0 - n * q # right shift to standard binomial distribution while currVaR <= -calculate_VaR(percentile, n, q): x = currVaR + mu # shift back cVaR += mul * currVaR * binom.pmf(x, n, q) currVaR += 1 return cVaR
def european_binomial(option, rate, vol, div, steps): strike = option.strike expiry = option.expiry spot = option.spot call_t = 0.0 spot_t = 0.0 option_t = 0.0 h = expiry / steps u = np.exp((rate - div) * h + vol * np.sqrt(h)) d = np.exp((rate - div) * h - vol * np.sqrt(h)) num_nodes = steps + 1 pstar = (np.exp(rate * h) - d) / (u - d) for i in range(num_nodes): spot_t = spot * (u**(steps - i)) * (d**(i)) option_t += option.payoff(spot_t) * binom.pmf(steps - i, steps, pstar) option_t *= np.exp(-rate * option.expiry) return option_t
def _create_mdp(self, path, prob, size): def _fill_reward(x): if x == 0: return 0.1 elif x == size - 1: return 1 else: return 0 state_generator = StateGenerator('name', 'reward') action_generator = ActionGenerator('name') states = [ state_generator.generate_state(name="s" + str(x), reward=_fill_reward(x)) for x in range(size) ] actions = [ action_generator.generate_action(name=name) for name in ['LEFT', 'RIGHT'] ] # Initializing the states of the mdp with binomial distribution init_states = dict( zip(states, [0] + [binom.pmf(i, size - 3, prob) for i in range(size - 2)] + [0])) mdp = MDPModel('MdpEnvLinVariable') \ .add_states(states) \ .add_actions(actions) \ .add_init_states(init_states) \ .add_final_states([states[x] for x in [0, size - 1]], 100) for i in range(size - 2): mdp \ .add_transition(states[i + 1], actions[0], {states[i]: 1}) \ .add_transition(states[i + 1], actions[1], {states[i + 2]: 1}) # Visualize the MDP mdp.finalize() # mdp.visualize(file="{0}/{1}".format(path, self.__class__.__name__)) return mdp
def _classify_position(self, tx_indels: int, mock_indels: int, edit_prior: Pr, binom_p: Pr) -> bool: """ Classify for each position if it Edit or not. :param tx_indels: :param mock_indels: :param edit_prior: :param binom_p: :return: Edit or not bool """ total_reads = self._n_reads_tx + self._n_reads_mock no_edit_prior = 1 - edit_prior # if no indels in treatment, return True (same results as if run the classifier) if tx_indels == 0: return False # In extreme cases both posteriors are smaller than python minimum number (1e-325). # The heuristic to solve this rare event, is to divide by 10 both the treatment and the mock indels. for idx in range(5): total_indels = tx_indels + mock_indels # Likelihood function computation no_edit_likelihood = hypergeom.pmf(tx_indels, total_reads, total_indels, self._n_reads_tx) edit_likelihood = binom.pmf(k=tx_indels, n=total_indels, p=binom_p) # Posterior probability computation no_edit_post = no_edit_prior * no_edit_likelihood edit_post = edit_prior * edit_likelihood # Regular case - Both different from zero if (edit_post != 0) or (no_edit_post != 0): edit = edit_post > no_edit_post return edit else: tx_indels = tx_indels // 10 mock_indels = mock_indels // 10 raise ClassificationFailed()
def make_3D(t, N, F, Tf, Ts, R, a, eta, Q, T, St): dt = t[1] - t[0] r = np.arange(t[0], t[-1] + dt, dt / 100) dr = r[1] - r[0] n = np.arange(np.floor(N - 4 * np.sqrt(N)), np.ceil(N + 4 * np.sqrt(N))) pois = poisson.pmf(n, N) nu = np.arange(30) model = np.sum(((1 - R) * Promt(r, F, Tf, Ts, T, St) + R * (1 - eta) * Recomb(r, F, Tf, Ts, T, St, a, eta)).reshape( len(t), 100, len(T)), axis=1) if np.any(model < 0): return np.amin(model) * np.ones((len(nu), 100, len(Q))) I = np.arange(len(nu) * len(Q) * len(t) * len(n)) B = binom.pmf( nu[I // (len(Q) * len(t) * len(n))], (n[I % len(n)]).astype(int), dS[(I // (len(n) * len(t))) % len(Q)] * Q[(I // (len(n) * len(t))) % len(Q)] * model[(I // len(n)) % len(t), (I // (len(n) * len(t))) % len(Q)]).reshape( len(nu), len(Q), len(t), len(n)) # P=np.matmul(B, pois) if np.any(np.isnan(B)): print('B is nan') sys.exit() if np.any(np.isinf(B)): print('B is inf') sys.exit() P0 = np.vstack((np.ones(len(n)), np.cumprod(np.prod(B[0], axis=0), axis=0)[:-1])) P1 = (P0 * (1 - np.prod(B[0], axis=0))) P = np.zeros((100, len(nu), len(Q))) for i in range(len(Q)): P2 = P0 * (1 - np.prod(B[0, np.delete(np.arange(len(Q)), i)], axis=0)) P[0, 0, i] = np.sum(np.sum(B[0, i] * P2 * pois, axis=1), axis=0) P[0, 1:] = np.sum(np.sum(B[1:] * P0 * pois, axis=3), axis=2) for i in range(1, 100): P[i] = np.sum(np.sum(B[:, :, i:, :] * P1[:len(t) - i, :] * pois, axis=3), axis=2) return np.transpose(P, (1, 0, 2))
def kmr_markov_matrix(p, N, epsilon=0, simultaneous=False): """ Generate the transition probability matrix for the KMR dynamics with two acitons. Parameters ---------- p : scalar(int) Probability on mixed strategy nash equilibrium. N : scalar(int) Number of players. epsilon : scalar(int), optional(default=0) perturbations. simultaneous : True or False sequential or simultaneous Returns --------- P : ndarray(int, ndim=2) The transition probability matrix for the KMR dynamics. """ P = np.zeros((N + 1, N + 1), dtype=float) if simultaneous is False: P[0][1] = (epsilon) * (1 / 2) P[N][N - 1] = (epsilon) * (1 / 2) P[0][0], P[N][N] = 1 - P[0][1], 1 - P[N][N - 1] for i in range(1, N): P[i][i - 1] = (i / N) * (epsilon * (1 / 2) + (1 - epsilon) * (((i - 1) / (N - 1) < p) + ((i - 1) / (N - 1) == p) * (1 / 2))) P[i][i + 1] = ((N - i) / N) * (epsilon * (1 / 2) + (1 - epsilon) * ((i / (N - 1) > p) + (i / (N - 1) == p) * (1 / 2))) P[i][i] = 1 - P[i][i - 1] - P[i][i + 1] return P else: for i in range(0, N + 1): P[i] = binom.pmf(range(N + 1), N, (i / N < p) * epsilon / 2 + (i / N == p) / 2 + (i / N > p) * (1 - epsilon / 2)) return P
def compare_aln(fg_aln, bg_aln): """Compare alignments using the ball-in-urn model. Like CHAIN does. """ # BG seqs are weighted, FG seqs are not bg_weights = alnutils.sequence_weights(bg_aln, 'none') bg_size = sum(bg_weights) bg_cons = consensus.consensus(bg_aln, weights=bg_weights) # Height of the foreground alignment column fg_size = len(fg_aln) fg_cons = consensus.consensus(fg_aln) fg_cols = zip(*fg_aln) bg_cols = zip(*bg_aln) fg_weights = [1] * fg_size pseudocounts = combined_frequencies(fg_aln, fg_weights, bg_aln, bg_weights) hits = [] for faa, baa, fg_col, bg_col in zip(fg_cons, bg_cons, fg_cols, bg_cols): if faa == '-' or baa == '-': # Ignore indel columns -- there are better ways to look at these pvalue = 1.0 else: # Cumulative binomial test # Number of consensus-type residues in the foreground column fg_counts = count_col(fg_col, fg_weights, pseudocounts) fg_tot = fg_counts['S'] + fg_counts['T'] + fg_counts['Y'] # Consensus residue frequency in the combined alignment column bg_counts = count_col(bg_col, bg_weights, pseudocounts) p_j = (bg_counts['S'] + bg_counts['T'] + bg_counts['Y'] + fg_tot ) / (bg_size + fg_size + 2.0) # pseudocount size = 1.0 # Probability of fg col conservation vs. the combined/main set # (P_j_LB in the publication) # NB: Some tweaks for pseudocounts pvalue = binom.pmf(range(int(math.ceil(fg_tot)), fg_size + 2), fg_size + 1, p_j).sum() if pvalue == 1.0: logging.info("Meaningless p-value: p_j=%s, fg=%s vs. bg=%s", p_j, fg_tot, bg_counts) hits.append((faa, baa, pvalue)) return hits
def matrix_selection_more_contributors(n, N, s=0): rocc = reduced_occupancy(N) mtx = np.zeros((n + 1, n + 1)) # cache is (io, no, ip, nc) cache = np.full((2*n+1, n+1, 2*n+1, n+1), np.nan) for ip in range(0, n + 1): for io in range(0, n + 1): for nc in range(0, n+1): for ic in range(0, nc+1): p = binom.pmf(ic, nc, ip/n) mtx[ip, io] += Qs(io, n, ic, nc, N, s, cache) * p # for nc in range(0, n+1): # for ic in range(0, nc+1): # # p = binom.pmf(ic, nc, np.arange(0, n+1)/n) # p = hypergeom.pmf(ic, n, np.arange(0, n+1), nc) # for io in range(0, n + 1): # mtx[:, io] += Qs(io, n, ic, nc, N, s, cache) * p return mtx, cache
def UtilIG_bayes_risk(self): """ 効用としてベイズリスクを計算する関数 推定1回目はランダムに実験を選ぶ """ if self.i != 0: m = np.argmax(self.w) L_w = np.ones(self.n_particles()) dU = np.zeros([self.n_exp(), 1]) for i in range(self.n_exp()): L_w[m] = binom.pmf(self.num, n=self.d, p=self.ptable_C[i]) #各パーティクルでの実験でms=0にいた確率 w_new = self.w * L_w.reshape([len(L_w), 1]) #重みの更新 x_infer = self.Mean(w_new, self.x) dU[i] = np.trace(self.Q * np.dot( (self.x - x_infer[0]).T, (self.x - x_infer[0]))) #実験C[i]でのベイズリスク self.U = dU * self.U self.U = self.U / np.sum(self.U) self.C_best_i = np.argmin(self.U) self.C_best = self.C[self.C_best_i]
def EuropeanBinomialPricer(pricing_engine, option, data): expiry = option.expiry strike = option.strike (spot, rate, volatility, dividend) = data.get_data() steps = pricing_engine.steps nodes = steps + 1 dt = expiry / steps u = np.exp(((rate - dividend) * dt) + volatility * np.sqrt(dt)) d = np.exp(((rate - dividend) * dt) - volatility * np.sqrt(dt)) pu = (np.exp((rate - dividend) * dt) - d) / (u - d) pd = 1 - pu disc = np.exp(-rate * expiry) spotT = 0.0 payoffT = 0.0 for i in range(nodes): spotT = spot * (u ** (steps - i)) * (d ** (i)) payoffT += option.payoff(spotT) * binom.pmf(steps - i, steps, pu) price = disc * payoffT return price
def get_binominal_probabilities(n): params = {} params['4'] = [0.2, 0.4, 0.6, 0.8] params['5'] = [0.1, 0.3, 0.5, 0.7, 0.9] params['6'] = [0.1, 0.26, 0.42, 0.58, 0.74, 0.9] params['8'] = [ 0.1, 0.21428571, 0.32857143, 0.44285714, 0.55714286, 0.67142857, 0.78571429, 0.9 ] probs = [] for true_class in range(0, n): probs.append( binom.pmf(np.arange(0, n), n - 1, params[str(n)][true_class])) return np.array(probs)
def UtilIG_bayes_risk(self): """ 効用としてベイズリスクを計算する関数 重みの仮更新がこれで良いのか議論の余地あり """ m = np.argmax(self.w) p_exp = self.Expsim(self.x[m], self.C_best) #真値におけるms0の確立 num = binomial(self.d, p_exp) #実験をd回行いms=0であった回数 L_w = np.ones(self.n_particles()) dU = np.zeros([self.n_exp(), 1]) for i in range(self.n_exp()): L_w[m] = binom.pmf(num, n=self.d, p=self.ptable_C[i]) #各パーティクルでの実験でms=0にいた確率 w_new = self.w * L_w.reshape([len(L_w), 1]) #重みの更新 x_infer = self.Mean(w_new, self.x) dU[i] = np.trace(self.Q * np.dot((self.x - x_infer[0]).T, (self.x - x_infer[0]))) self.U = dU * self.U self.U = self.U / np.sum(self.U) self.C_best_i = np.argmin(self.U) self.C_best = self.C[self.C_best_i]
def Update(self): #ベイズ推定を行う関数 引数(self,Ci) """ パーティクルの重みを更新する関数 """ #ラビ振動とラムゼー干渉の実験を行う exp_flag_best=self.exp_flag self.mode=1 self.p_exp=[] self.exp_flag="rabi" self.p_exp.append(self.Expsim(self.x0,self.C_best[0]))#真値におけるms0の確立 self.exp_flag="ramsey" self.p_exp.append(self.Expsim(self.x0,self.C_best[1]))#真値におけるms0の確立 self.num=binomial(self.d, self.p_exp)#実験をd回行いms=0であった回数 self.exp_flag=exp_flag_best if exp_flag_best=="rabi": _num=self.num[0] elif exp_flag_best=="ramsey": _num=self.num[1] temp=binom.pmf(_num,n=self.d,p=self.ptable_x)#各パーティクルでの実験でms=0にいた確率 self.w=self.w*temp.reshape([len(temp),1]) #重みの更新 self.w=self.w/np.sum(self.w) #重みの規格化
def get_series_prob(series_games, series_wins, series_losses, team_winProb): team_probs = [] if series_wins == series_games / 2 + 1: team_probs.append(1) total_games = series_wins + series_losses if series_losses == series_games / 2 + 1: team_probs.append(0) if (series_wins != series_games / 2 + 1 and series_losses != series_games / 2 + 1): for end_game in range(series_games / 2 + 1, series_games + 1 - series_losses): team_in_N = BinomDist.pmf(n=end_game - 1 - series_wins, k=(series_games / 2 - series_wins), p=team_winProb) * team_winProb team_probs.append(team_in_N) return float(sum(team_probs))
def compare_cols(fg_col, fg_cons, fg_size, fg_weights, bg_col, bg_cons, bg_size, bg_weights, aa_freqs, pseudo_size): "Compare alignments using the ball-in-urn model (cumulative binomial test)" # Number of consensus-type residues in the foreground column fg_counts = count_col(fg_col, fg_weights, aa_freqs, pseudo_size) fg_tot = fg_counts['S'] + fg_counts['T'] + fg_counts['Y'] # Consensus residue frequency in the combined alignment column bg_counts = count_col(bg_col, bg_weights, aa_freqs, pseudo_size) p_j = (bg_counts['S'] + bg_counts['T'] + bg_counts['Y'] + fg_tot) / ( bg_size + fg_size + 2.0) # pseudocount size = 1.0 # Probability of fg col conservation vs. the combined/main set # (P_j_LB in the publication) # NB: Some tweaks for pseudocounts pvalue = binom.pmf(range(int(math.ceil(fg_tot)), fg_size + 2), fg_size + 1, p_j).sum() if pvalue == 1.0: logging.info("Meaningless p-value: p_j=%s, fg=%s vs. bg=%s", p_j, fg_tot, bg_counts) return pvalue
def show_my_result(name_list, correct_list, num_questions=30, ax1=None): n_correct = np.arange(num_questions + 1) if ax1 is None: fig, ax1 = plt.subplots(1, 1, figsize=(10, 5), dpi=250) binom_pmf = binom.pmf(n_correct, num_questions, 0.5) binom_cdf = np.cumsum(binom_pmf) ax1.bar(n_correct, binom_pmf / np.max(binom_pmf), color='k', alpha=0.5) ax1.plot(n_correct, binom_cdf, 'k-', label='Cumulative') prop_cycle = plt.rcParams['axes.prop_cycle'] color_cycle = cycle(prop_cycle.by_key()['color']) y_pos_list = np.linspace(0.2, 0.8, len(name_list)) for y_pos, name, correct, color in zip(y_pos_list, name_list, correct_list, color_cycle): ax1.axvline(correct, label='{}\nTop {:2.1f}%'.format(name, 100 * binom_cdf[correct]), color=color) ax1.text(correct, y_pos, name) ax1.legend() return ax1
def model(args): """ %prog model erate Model kmer distribution given error rate. See derivation in FIONA paper: <http://bioinformatics.oxfordjournals.org/content/30/17/i356.full> """ from scipy.stats import binom, poisson p = OptionParser(model.__doc__) p.add_option("-k", default=23, type="int", help="Kmer size") p.add_option("--cov", default=50, type="int", help="Expected coverage") opts, args = p.parse_args(args) if len(args) != 1: sys.exit(not p.print_help()) (erate, ) = args erate = float(erate) cov = opts.cov k = opts.k xy = [] # Range include c although it is unclear what it means to have c=0 for c in range(0, cov * 2 + 1): Prob_Yk = 0 for i in range(k + 1): # Probability of having exactly i errors pi_i = binom.pmf(i, k, erate) # Expected coverage of kmer with exactly i errors mu_i = cov * (erate / 3)**i * (1 - erate)**(k - i) # Probability of seeing coverage of c Prob_Yk_i = poisson.pmf(c, mu_i) # Sum i over 0, 1, ... up to k errors Prob_Yk += pi_i * Prob_Yk_i xy.append((c, Prob_Yk)) x, y = zip(*xy) asciiplot(x, y, title="Model")
def single_gen_matrix(Ne= 1092, ploidy= 2,precision= 1092,s=0): ''' define transition probabilities for alleles at any given frequency from 1 to Ne. ''' pop_size= Ne * ploidy space= np.linspace(0,pop_size,precision,dtype=int) t= np.tile(np.array(space),(precision,1)) probs= [x / (pop_size) for x in space] ## sadjust= [x * (1+s) for x in probs] scomp= [(1-x) for x in probs] new_probs= [sadjust[x] / (scomp[x]+sadjust[x]) for x in range(precision)] new_probs= np.nan_to_num(new_probs) probs= new_probs freq_matrix= binom.pmf(t.T,pop_size,probs) return freq_matrix
def sample_c_i(i): global c, clusters #print_cluster(clusters) xi = x[i] ci = c[i] phi_i_j = {} clusters_cp = deepcopy(clusters) clusters_cp[ci].member.remove(xi) for j in clusters_cp: n_j_i = clusters_cp[j].size() if n_j_i > 0: theta_j = beta.rvs(clusters_cp[j].a1,clusters_cp[j].b1) phi_i_j[j] = binom.pmf(xi,20,theta_j) * n_j_i / (alpha + N - 1) j_new = max(clusters) + 1 phi_i_j[j_new] = p_new[xi] # generate j1 c_phi = phi_i_j.keys() p_phi = phi_i_j.values() / sum(phi_i_j.values()) #j1 = stats.rv_discrete(name='custm',values=(c_phi,p_phi)) j1 = np.random.choice(c_phi,p = p_phi) #j1 = max(phi_i_j, key = phi_i_j.get) #print "debug:x",i,"ci",ci, "j1",j1 if j1 == j_new: c[i] = j1 clusters[j1] = Cluster(j1) clusters[j1].member.append(xi) clusters[ci].member.remove(xi) if clusters[ci].size() == 0: del clusters[ci] else: c[i] = j1 clusters[j1].member.append(xi) #print xi, ci, clusters[ci].member #print "ci",ci,"member",clusters[ci].member,"xi",xi clusters[ci].member.remove(xi) if clusters[ci].size() == 0: del clusters[ci]
def _left_extreme(n_instances, n_total, prob): """Used for a binomial problem. Calculates the exact likelihood of finding observations as and less extreme than our observed value Parameters ---------- n_instances: int The number of observed successes in our binomial problem n_total: int The total number of observations prob: float, 0<=p<=1 The expected probability of success Returns ------- p: float The exact likelihood that we would find observations less extreme than our observed number of success """ counter = np.arange(n_instances + 1) p = np.sum(binom.pmf(counter, n_total, prob)) return p
def part_a2(fname=fname): fname = fname + '_a2' m = 5 theta_set = np.linspace(1 / float(m), 1 - 1 / float(m), m) print(theta_set) n = 34 x = np.arange(n + 1) fig = plt.figure() hh = [] for i, theta in enumerate(theta_set): h, = plt.plot(x, binom.pmf(x, n, theta), lw=1.5, label=r'$\theta=' + '{:.2f}'.format(theta) + '$') hh.append(h) plt.xlabel(r'$y$', fontsize=labelFontSize) plt.ylabel(r'$p(y \mid \theta)$', fontsize=labelFontSize) plt.title('3.4a' + r' $p(y \mid \theta)$', fontsize=titleFontSize) plt.xticks(fontsize=tickFontSize) plt.yticks(fontsize=tickFontSize) plt.legend() plt.savefig(fname + '.' + imgFmt, format=imgFmt)