Last active
June 3, 2024 06:38
-
-
Save sshh12/b762ed5b699de35b72f61fe9ad6c6303 to your computer and use it in GitHub Desktop.
Beta Transformed Linear Opinion Pool (Python)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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