import%20marimo%0A%0A__generated_with%20%3D%20%220.24.0%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Bilinear%20Logistic%20Regression%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20warnings%0A%0A%20%20%20%20warnings.filterwarnings(%22ignore%22)%0A%0A%20%20%20%20import%20cvxpy%20as%20cp%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20from%20sklearn.datasets%20import%20make_classification%0A%0A%20%20%20%20from%20dbcp%20import%20BiconvexProblem%0A%0A%20%20%20%20np.random.seed(10015)%0A%20%20%20%20return%20BiconvexProblem%2C%20cp%2C%20make_classification%2C%20mo%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Introduction%0A%0A%20%20%20%20Suppose%20we%20are%20given%20a%20dataset%20%24(X_i%2C%20y_i)%24%2C%20%24i%20%3D%201%2C%20%5Cldots%2C%20m%24%2C%20where%20each%20sample%20consists%20of%20a%20feature%0A%20%20%20%20matrix%20%24X_i%20%5Cin%20%5Cmathbf%7BR%7D%5E%7Bn%20%5Ctimes%20k%7D%24%20and%20a%20binary%20label%20%24y_i%20%5Cin%20%5C%7B0%2C%201%5C%7D%24.%0A%20%20%20%20Our%20goal%20is%20to%20construct%20a%20bilinear%20classifier%0A%0A%20%20%20%20%5C%5B%0A%20%20%20%20%20%20%20%20%5Chat%7By%7D%20%3D%20%5Cleft%5C%7B%5Cbegin%7Barray%7D%7Bll%7D%0A%20%20%20%20%20%20%20%20%20%20%20%201%20%26%20%5Cmathop%7B%5Cbf%20tr%7D(U%5ET%20X%20V)%20%3E%200%5C%5C%0A%20%20%20%20%20%20%20%20%20%20%20%200%20%26%20%5Ctext%7Botherwise%7D%2C%0A%20%20%20%20%20%20%20%20%5Cend%7Barray%7D%5Cright.%0A%20%20%20%20%5C%5D%0A%0A%20%20%20%20where%20%24U%20%5Cin%20%5Cmathbf%7BR%7D%5E%7Bn%20%5Ctimes%20r%7D%24%20and%20%24V%20%5Cin%20%5Cmathbf%7BR%7D%5E%7Bk%20%5Ctimes%20r%7D%24%20are%20the%20bilinear%20logistic%0A%20%20%20%20regression%20coefficients%20with%20a%20predefined%20(maximum)%20rank%20%24r%24%2C%20and%20%24%5Cmathop%7B%5Cbf%20tr%7D(M)%24%20denotes%20the%20trace%0A%20%20%20%20of%20some%20square%20matrix%20%24M%24.%0A%0A%20%20%20%20To%20fit%20a%20bilinear%20logistic%20regression%20model%20to%20the%20dataset%2C%20we%20would%20like%20to%20solve%20the%20following%20bilinear%0A%20%20%20%20maximum%20likelihood%20estimation%20problem%3A%0A%0A%20%20%20%20%5C%5B%0A%20%20%20%20%20%20%20%20%5Cbegin%7Barray%7D%7Bll%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%5Ctext%7Bmaximize%7D%20%26%20%5Csum_%7Bi%20%3D%201%7D%5E%7Bm%7D%20y_i%20%5Cmathop%7B%5Cbf%20tr%7D(U%5ET%20X_i%20V)%20-%0A%20%20%20%20%5Clog(1%20%2B%20%5Cexp(%5Cmathop%7B%5Cbf%20tr%7D(U%5ET%20X_i%20V)))%0A%20%20%20%20%20%20%20%20%5Cend%7Barray%7D%0A%20%20%20%20%5C%5D%0A%0A%20%20%20%20with%20variables%20%24U%24%20and%20%24V%24.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Generate%20problem%20data%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(make_classification)%3A%0A%20%20%20%20m%20%3D%20300%0A%20%20%20%20n%20%3D%2020%0A%20%20%20%20k%20%3D%2010%0A%20%20%20%20r%20%3D%205%0A%20%20%20%20ninfo_frac%20%3D%200.9%0A%20%20%20%20Xs%2C%20ys%20%3D%20make_classification(%0A%20%20%20%20%20%20%20%20n_samples%3Dm%2C%20n_features%3Dn%20*%20k%2C%20n_informative%3Dint(n%20*%20k%20*%20ninfo_frac)%2C%20n_redundant%3Dint(n%20*%20k%20*%20(1%20-%20ninfo_frac))%0A%20%20%20%20)%0A%20%20%20%20Xs%20%3D%20Xs.reshape(m%2C%20n%2C%20k)%0A%20%20%20%20return%20Xs%2C%20k%2C%20n%2C%20r%2C%20ys%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Specify%20and%20solve%20the%20biconvex%20problem%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(BiconvexProblem%2C%20Xs%2C%20cp%2C%20k%2C%20n%2C%20r%2C%20ys)%3A%0A%20%20%20%20U%20%3D%20cp.Variable((n%2C%20r))%0A%20%20%20%20V%20%3D%20cp.Variable((k%2C%20r))%0A%0A%20%20%20%20obj%20%3D%20cp.Maximize(sum(_y%20*%20cp.trace(U.T%20%40%20_X%20%40%20V)%20-%20cp.logistic(cp.trace(U.T%20%40%20_X%20%40%20V))%20for%20_X%2C%20_y%20in%20zip(Xs%2C%20ys)))%0A%20%20%20%20prob%20%3D%20BiconvexProblem(obj%2C%20%5BU%5D%2C%20%5BV%5D)%0A%20%20%20%20prob.solve(cp.CLARABEL%2C%20lbd%3D0.1%2C%20abs_tol%3D1e-4)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
1615abe8f59c5bbec4ddd97af6b36af9