<filename>experiments_track/ab_bayes/src/implem/coefficients.py
import numpy as np
from scipy.optimize import minimize
from scipy.stats import betabinom


class CoefTransformers:

    @staticmethod
    def log_likelihood_loss_func(parameters, prior_df):
        alpha_test, beta_test = parameters
        k, n = prior_df["readmission_sum"].values, prior_df["patients_cnt"].values
        return sum(abs(betabinom.logpmf(k, n, alpha_test, beta_test)))

    @staticmethod
    def calc_maximum_likelihood(func_name, prior_df):
        print(f"prior df size: {len(prior_df)} \n "
              f"number of succses {prior_df['readmission_sum'].sum()} number of attempts {prior_df['patients_cnt'].sum()}")
        if func_name == "ll":
            func = CoefTransformers.log_likelihood_loss_func
        mle_model = minimize(
            fun=func,
            x0=np.array([1, 10]),
            method='L-BFGS-B',
            bounds=[(0.0001, np.inf), (1, np.inf)],
            args=prior_df,
            options={
                'disp': True,
                "iprint": 5
            }
        )
        return mle_model


<filename>experiments_track/ab_bayes/src/implem/coefficients.py
import numpy as np
from scipy.optimize import minimize
from scipy.stats import betabinom


class CoefTransformers:

    @staticmethod
    def log_likelihood_loss_func(parameters, prior_df):
        alpha_test, beta_test = parameters
        k, n = prior_df["readmission_sum"].values, prior_df["patients_cnt"].values
        return sum(abs(betabinom.logpmf(k, n, alpha_test, beta_test)))

    @staticmethod
    def calc_maximum_likelihood(func_name, prior_df):
        print(f"prior df size: {len(prior_df)} \n "
              f"number of succses {prior_df['readmission_sum'].sum()} number of attempts {prior_df['patients_cnt'].sum()}")
        if func_name == "ll":
            func = CoefTransformers.log_likelihood_loss_func
        mle_model = minimize(
            fun=func,
            x0=np.array([1, 10]),
            method='L-BFGS-B',
            bounds=[(0.0001, np.inf), (1, np.inf)],
            args=prior_df,
            options={
                'disp': True,
                "iprint": 5
            }
        )
        return mle_model

