Skip to content

Instantly share code, notes, and snippets.

@sshh12
Last active June 3, 2024 06:38
Show Gist options
  • Save sshh12/b762ed5b699de35b72f61fe9ad6c6303 to your computer and use it in GitHub Desktop.
Save sshh12/b762ed5b699de35b72f61fe9ad6c6303 to your computer and use it in GitHub Desktop.
Beta Transformed Linear Opinion Pool (Python)
blp_N = len(sources)
blp_X = np.array(blp_X) # (examples, sources)
blp_y = np.array(blp_y) # (examples as 1/0,)
def log_llh_blp(params, x, y):
a, b, w = params[0], params[1], params[2:]
hswp = st.beta.cdf(np.dot(x, w), a, b)
llh = scipy.special.xlogy(y, hswp) + scipy.special.xlogy(1.0 - y, 1.0 - hswp)
return -np.mean(llh)
const_w_sum_to_one = scipy.optimize.LinearConstraint([[0, 0] + [1] * blp_N], 1.0, 1.0)
const_all_positive = scipy.optimize.LinearConstraint(np.eye(2 + blp_N), 0.0, 1e4)
res = scipy.optimize.minimize(
fun=log_llh_blp,
x0=np.array([2.0, 2.0] + [1.0 / blp_N] * blp_N),
args=(blp_X, blp_y),
constraints=[const_w_sum_to_one, const_all_positive],
method="slsqp",
options={"maxiter": 1000, "ftol": 1e-08},
)
blp_a = res.x[0]
blp_b = res.x[1]
blp_w = dict(zip(sources, res.x[2:]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment