def maximum_likelihood(Ms, Mu, Mus, Mss, fit_offset=False, fit_offset2=False):
n_obs, n_var = Ms.shape<EOL>offset, offset_ss = np.zeros(n_var, dtype="<STR_LIT>"), np.zeros(n_var, dtype="<STR_LIT>")<EOL>gamma = np.ones(n_var, dtype="<STR_LIT>")<EOL>def sse(A, data, b):<EOL><INDENT>sigma = (A.dot(data) - b).std(<NUM_LIT:1>)<EOL>return np.log(sigma).sum()  <EOL><DEDENT>if fit_offset and fit_offset2:<EOL><INDENT>for i in range(n_var):<EOL><INDENT>data = np.vstack((Mu[:, i], Ms[:, i], Mus[:, i], Mss[:, i]))<EOL>offset[i], offset_ss[i], gamma[i] =minimize(lambda m: sse(np.array([[<NUM_LIT:1>, -m[<NUM_LIT:2>], <NUM_LIT:0>, <NUM_LIT:0>], [<NUM_LIT:1>, m[<NUM_LIT:2>], <NUM_LIT:2>, -<NUM_LIT:2> * m[<NUM_LIT:2>]]]),<EOL>data, b=np.array(m[<NUM_LIT:0>], m[<NUM_LIT:1>])), x0=(<NUM_LIT>, <NUM_LIT>, <NUM_LIT:1>), method="<STR_LIT>").x<EOL><DEDENT><DEDENT>elif fit_offset:<EOL><INDENT>for i in range(n_var):<EOL><INDENT>data = np.vstack((Mu[:, i], Ms[:, i], Mus[:, i], Mss[:, i]))<EOL>offset[i], gamma[i] =minimize(lambda m: sse(np.array([[<NUM_LIT:1>, -m[<NUM_LIT:1>], <NUM_LIT:0>, <NUM_LIT:0>], [<NUM_LIT:1>, m[<NUM_LIT:1>], <NUM_LIT:2>, -<NUM_LIT:2> * m[<NUM_LIT:1>]]]),<EOL>data, b=np.array(m[<NUM_LIT:0>], <NUM_LIT:0>)), x0=(<NUM_LIT>, <NUM_LIT:1>), method="<STR_LIT>").x<EOL><DEDENT><DEDENT>elif fit_offset2:<EOL><INDENT>for i in range(n_var):<EOL><INDENT>data = np.vstack((Mu[:, i], Ms[:, i], Mus[:, i], Mss[:, i]))<EOL>offset_ss[i], gamma[i] =minimize(lambda m: sse(np.array([[<NUM_LIT:1>, -m[<NUM_LIT:1>], <NUM_LIT:0>, <NUM_LIT:0>], [<NUM_LIT:1>, m[<NUM_LIT:1>], <NUM_LIT:2>, -<NUM_LIT:2> * m[<NUM_LIT:1>]]]),<EOL>data, b=np.array(<NUM_LIT:0>, m[<NUM_LIT:0>])), x0=(<NUM_LIT>, <NUM_LIT:1>), method="<STR_LIT>").x<EOL><DEDENT><DEDENT>else:<EOL><INDENT>for i in range(n_var):<EOL><INDENT>data = np.vstack((Mu[:, i], Ms[:, i], Mus[:, i], Mss[:, i]))<EOL>gamma[i] =minimize(lambda m: sse(np.array([[<NUM_LIT:1>, -m, <NUM_LIT:0>, <NUM_LIT:0>], [<NUM_LIT:1>, m, <NUM_LIT:2>, -<NUM_LIT:2> * m]]), data, b=<NUM_LIT:0>),<EOL>x0=gamma[i], method="<STR_LIT>").x<EOL><DEDENT><DEDENT>return offset, offset_ss, gamma<EOL>
Maximizing the log likelihood using weights according to empirical bayes