def bayesfactor(locallambda, peakscore): try: # bayesfactor = 2 * (math.log((gammaincc(peakscore-1, locallambda)*gamma(peakscore-1)), math.e) - (peakscore-1)*math.log(locallambda, math.e) + locallambda) # # a = (math.log(gammaincc(peakscore-1, locallambda), math.e) ) # b = math.lgamma(peakscore-1) # c=(peakscore-1)*math.log(locallambda, math.e) # print (locallambda,peakscore,a,b,c) bayesfactor2 = 2 * ( math.log(gammaincc(peakscore - 1, locallambda), math.e) + math.lgamma(peakscore - 1) - (peakscore - 1) * math.log(locallambda, math.e) + locallambda) return bayesfactor2 except Exception as e: print('got exception in Jazzlib.sta.bayesfactor: %r,' % (e, )) print(locallambda, peakscore) except KeyboardInterrupt: sys.stderr.write("User interrupt\n") sys.exit(0)
def changeTAndXt(filename): x2 = temperatureToTime(filename) y4 = caculateXt(filename) x5 = [] y5 = [] totalTime = x2[0] for i in range(1, len(x2) - 1): # x2中最后一个元素为0,不能做分母,排除 tr = x2[i] / totalTime x5.append(math.log(tr)) for i in range(1, len(y4) - 1): # y4中第一个元素为1,最后一个元素为0,不能放在log里面,排除 y5.append(math.log(-(math.log(1 - y4[i])))) return x5, y5
def lh_exp(tau, *args): t = args[0] lh = prob_exp(t, tau) if (lh > 0): return -2.0 * math.log(lh) else: return 1e99
def lh_poisson(mu, *args): n = args[0] lh = prob_poisson(n, mu) if lh > 0: return -2.0 * math.log(lh) else: return 1e99
def optionBS(spot,strike, vol, T, Rf): ''' call option using Black Scholes ''' denom = vol * math.sqrt(T) d1 = (math.log(spot/strike) + (Rf + 0.5*vol**2)*T) / denom d2 = d1 - denom call = spot * stats.norm.cdf(d1) - strike * math.exp(-Rf * T) * stats.norm.cdf(d2) put = strike * math.exp(-Rf * T) * stats.norm.cdf(-d2) - spot * stats.norm.cdf(-d1) return call, put
def get_prob_ratio_from_policies(sample_trajectories, base_p, execu_p): ratios = [] for traj in sample_trajectories: ratio = 1 for i in range(len(traj) - 1): step = traj[i] next_step = traj[i + 1] print(str(next_step)) try: grid = str(step[1]) + '|' + str(step[2]) action = str(next_step[1]) + '|' + str(next_step[2]) ratio += math.log(base_p[grid][action] / 1.0 / execu_p[grid][action]) except: ratio += 0 ratios.append(ratio) return np.array(ratios)
def bayesfactor(locallambda, peakscore): try: # bayesfactor = 2 * (math.log((gammaincc(peakscore-1, locallambda)*gamma(peakscore-1)), math.e) - (peakscore-1)*math.log(locallambda, math.e) + locallambda) # # a = (math.log(gammaincc(peakscore-1, locallambda), math.e) ) # b = math.lgamma(peakscore-1) # c=(peakscore-1)*math.log(locallambda, math.e) # print (locallambda,peakscore,a,b,c) bayesfactor2 = 2 * (math.log(gammaincc(peakscore-1, locallambda), math.e)+math.lgamma(peakscore-1) - (peakscore-1)*math.log(locallambda, math.e) + locallambda) return bayesfactor2 except Exception, e: print ('got exception in Jazzlib.sta.bayesfactor: %r,' % (e,)) print (locallambda, peakscore)
def protectedLog(a): if (a <= 0): return 1 else: return math.log(a)