from numeric_base import D,ROUND_CEILING,ROUND_FLOOR,getcontext,lru_cache,np,PR,guard,fu,fd,upfloat,dnfloat from numeric_center import dh,dir_log1p_ratio,ex,ln getcontext().prec = 70 ZERO, ONE, HALF = D(0), D(1), D(".5") M, PEAK = D(".05"), D(".088889") CUT = 25000 PP = [int(p) for p in PR if p < CUT] LOGP = {p:ln(D(p)) for p in PP} LOWLOG = np.asarray([dnfloat(LOGP[p]) for p in PP]) FUP = np.asarray([upfloat(D(p-1)/(p*(p-2))) if p>2 else 1 for p in PP]) def firstint(x): return int((x-guard(x)).to_integral_value(rounding=ROUND_CEILING)) def num(x): return x if isinstance(x,D) else D(str(x)) def transport(logy): d=D("1.03")*((logy+D(".02"))*logy/(25*ex(logy/2))+(logy+D(".02"))/ex(logy)) assert dD(2658).ln() return d+guard(d) def cdf(v): return 6*v*v-8*v**3+3*v**4 def cdf_envelope(x,scale,lower=None): tail=scale if lower is None else lower a,b,c=1/(3*scale),tail/3,tail if x<=a:return cdf(x*scale)/scale part=cdf(ONE/3)/scale+D(16)/9*(min(b,x)-a) if x<=b:return part return part+tail*(cdf(min(c,x)/tail)-cdf(ONE/3)) @lru_cache(maxsize=5000) def ordinary(l0,l1,be,et,t,nv=72,nu=320,fixed_z=None,buthe=False): # Upper for L/(MN) times the ordinary softened triple mass, with q-conditioning premium. y=et*l0+t;de=transport(y) if buthe: # For p on this switched support, p10 de=D("1.01")*((D("1.95")+D("3.9")/y+D("19.5")/(y*y))*ex(-y/2)+y*ex(-y)) assert de1 scale=ex(2*de);tail=ONE if buthe else scale e=et+min((t-de)/l0,(t-de)/l1); ef=dnfloat(e) z0=ex(be*l0) if fixed_z is None else num(fixed_z) assert z0>41 and z00 numerator=dir_log1p_ratio(h[hit],ef);J=fu(numerator/s[hit]) row=fu(np.sum(fu(J*weights[hit]))*(1+len(xs)*4e-16)) total+=mass*D.from_float(float(row)) return total+Jtail*cdf_envelope(tail,scale,tail)+guard(total)