def _updateRecallSingle(prior, result, tnow, rebalance=True, tback=None, q0=None): (alpha, beta, t) = prior z = result > 0.5 q1 = result if z else 1 - result # alternatively, max(result, 1-result) if q0 is None: q0 = 1 - q1 dt = tnow / t if z == False: c, d = (q0 - q1, 1 - q0) else: c, d = (q1 - q0, q0) den = c * betafn(alpha + dt, beta) + d * (betafn(alpha, beta) if d else 0) def moment(N, et): num = c * betafn(alpha + dt + N * dt * et, beta) if d != 0: num += d * betafn(alpha + N * dt * et, beta) return num / den if rebalance: from scipy.optimize import root_scalar rootfn = lambda et: moment(1, et) - 0.5 sol = root_scalar(rootfn, bracket=_findBracket(rootfn, 1 / dt)) et = sol.root tback = et * tnow elif tback: et = tback / tnow else: tback = t et = tback / tnow mean = moment( 1, et) # could be just a bit away from 0.5 after rebal, so reevaluate secondMoment = moment(2, et) var = secondMoment - mean * mean newAlpha, newBeta = _meanVarToBeta(mean, var) assert newAlpha > 0 assert newBeta > 0 return (newAlpha, newBeta, tback)
def demo_idist_jacobi(): alph = -0.8 bet = np.sqrt(101) #n = 8; M = 1001 n = 4 M = 5 x = np.cos(np.linspace(0, np.pi, M + 2))[:, np.newaxis] x = x[1:-1] F = idist_jacobi(x, n, alph, bet) recursion_coeffs = jacobi_recurrence(n + 1, alph, bet, True) #recursion_coeffs[:,1]=np.sqrt(recursion_coeffs[:,1]) polys = evaluate_orthonormal_polynomial_1d(x[:, 0], n, recursion_coeffs) wt_function = (1 - x)**alph * (1 + x)**bet / (2**(alph + bet + 1) * betafn(bet + 1, alph + 1)) f = polys[:, -1:]**2 * wt_function fig, ax = plt.subplots(1, 1) plt.plot(x, f) ax.set_xlabel('$x$') ax.set_ylabel('$p_n^2(x) \mathrm{d}\mu(x)$') ax.set_xlim(-1, 1) ax.set_ylim(0, 4) fig, ax = plt.subplots(1, 1) plt.plot(x, F) ax.set_xlabel('$x$') ax.set_ylabel('$F_n(x)$') ax.set_xlim(-1, 1) plt.show()
def true_calib_error(self): ans = (betafn(self.alpha + 2, self.beta) - 2. * betafn(self.alpha + self.d + 1, self.beta) + betafn(self.alpha + 2 * self.d, self.beta)) / betafn( self.alpha, self.beta) return np.sqrt(ans)
def pdf_fx(self, x): return pow(x, self.alpha - 1.) * pow(1. - x, self.beta - 1.) / betafn( self.alpha, self.beta)
def moment(N, et): num = c * betafn(alpha + dt + N * dt * et, beta) if d != 0: num += d * betafn(alpha + N * dt * et, beta) return num / den