Exemplo n.º 1
0
def score_precision_beta(
    endog,
    exog_mean,
    exog_precision,
    bounded_reg_link,
    param_mean,
    param_precision,
):
    """
    Computes the score vector with respect to the precision regression 
    parameters. 
    
    For more details we refer to:
    Ferrari, S. L. P., Cribari-Neto, F. (2004). Beta regression
    for modeling rates and proportions. J. Appl. Statist. 31, 799–815
    
    Simas, A. B., Barreto-Souza, W., Rocha, A. V. (2010). Improved
    estimators for a general class of beta regression models. Computational
    Statistics and Data Analysis, 54, 348–366
    
    :param endog (array_like): 1d array of endogenous response variable.
    
    :param exog_mean (array_like): A nobs x k array where nobs is the number 
    of observations and k is the number of mean regressors. An intercept is 
    not included by default and should be added by the user.
    
    :param exog_precision (array_like): A nobs x q array where nobs is the 
    number of observations and q is the number of precision regressors. 
    An intercept is not included by default and should be added by the user.
    
    :param bounded_reg_link: An instance of BoundedRegLink. Recall that
    the default mean link is 'logit' and that the default precision link 
    is None.

    :param param_mean: 1d array of mean regression parameters.   
    
    :param param_precision: 1d array of precision regression parameters.
    """
    estimated_mean = estimate_mean(exog_mean, param_mean, bounded_reg_link)

    estimated_precision = estimate_precision(exog_precision, param_precision,
                                             bounded_reg_link)

    if exog_precision is None:
        exog_precision = param_precision * np.ones_like(estimated_mean)

    score_precision = np.matmul(
        exog_precision.T,
        (estimated_mean *
         (np.log(endog) - np.log(1 - endog) -
          digamma(estimated_mean * estimated_precision) + digamma(
              (1 - estimated_mean) * estimated_precision)) +
         digamma(estimated_precision) - digamma(
             (1 - estimated_mean) * estimated_precision) + np.log(1 - endog)) *
        bounded_reg_link.dphideta(estimated_precision),
    )

    return correct_dimension(score_precision)
Exemplo n.º 2
0
def grad_q_precision_beta(
    endog,
    exog_mean,
    exog_precision,
    bounded_reg_link,
    param_mean,
    param_precision,
    previous_precision,
):
    """
    Computes the gradient of the Q function with respect to the precision
    regression parameters. 
    
    For more details we refer to:
    Barreto-Souza & Simas (2017) Improving estimation for beta regression 
    models via em-algorithm and related diagnostic tools, Volume 87, Pages
    2847-2867
    
    :param endog (array_like): 1d array of endogenous response variable.
    
    :param exog_mean (array_like): A nobs x k array where nobs is the number 
    of observations and k is the number of mean regressors. An intercept is 
    not included by default and should be added by the user.
    
    :param exog_precision (array_like): A nobs x q array where nobs is the 
    number of observations and q is the number of precision regressors. 
    An intercept is not included by default and should be added by the user.
    
    :param bounded_reg_link: An instance of BoundedRegLink. Recall that
    the default mean link is 'logit' and that the default precision link 
    is None.

    :param param_mean: 1d array of mean regression parameters.   
    
    :param param_precision: 1d array of precision regression parameters.
    
    :param previous_precision: 1d array of the regression parameters 
    related to the precision in the previous EM-step.
    """
    estimated_mean = estimate_mean(exog_mean, param_mean, bounded_reg_link)
    estimated_precision = estimate_precision(exog_precision, param_precision,
                                             bounded_reg_link)
    if exog_precision is None:
        exog_precision = param_precision * np.ones_like(estimated_mean)

    grad_precision = np.matmul(
        exog_precision.T,
        (estimated_mean * np.log(endog / (1 - endog)) +
         digamma(previous_precision) + np.log(1 - endog) -
         estimated_mean * digamma(estimated_mean * estimated_precision) -
         (1 - estimated_mean) * digamma(
             (1 - estimated_mean) * estimated_precision)) *
        bounded_reg_link.dphideta(estimated_precision),
    )

    return correct_dimension(grad_precision)
Exemplo n.º 3
0
def q_function_beta(
    endog,
    exog_mean,
    exog_precision,
    bounded_reg_link,
    param_mean,
    param_precision,
    previous_precision,
):
    """
    Obtain the Q function for the EM estimation of the beta regression.
    In the iterative procedure, we have to carry the previous value of 
    the precision parameter.
    
    For more details we refer to:
    Barreto-Souza & Simas (2017) Improving estimation for beta regression 
    models via em-algorithm and related diagnostic tools, Volume 87, Pages
    2847-2867
    
    We return the value with the minus sign since we want to maximize
    the Q function and we will utilize the minimize function to
    optimizate.
    
    :param endog (array_like): 1d array of endogenous response variable.
    
    :param exog_mean (array_like): A nobs x k array where nobs is the number 
    of observations and k is the number of mean regressors. An intercept is 
    not included by default and should be added by the user.

    :param exog_precision (array_like): A nobs x q array where nobs is the 
    number of observations and q is the number of precision regressors. 
    An intercept is not included by default and should be added by the user.

    :param bounded_reg_link: An instance of BoundedRegLink. Recall that
    the default mean link is 'logit' and that the default precision link 
    is None.

    :param param_mean: 1d array of mean regression parameters.
    
    :param param_precision: 1d array of precision regression parameters.
    
    :param previous_precision: 1d array of the regression parameters 
    related to the precision in the previous EM-step.
    """

    estimated_mean = estimate_mean(exog_mean, param_mean, bounded_reg_link)
    estimated_precision = estimate_precision(exog_precision, param_precision,
                                             bounded_reg_link)

    return -np.sum(estimated_precision *
                   (estimated_mean * np.log(endog / (1 - endog)) +
                    digamma(previous_precision) + np.log(1 - endog)) -
                   gammaln(estimated_mean * estimated_precision) -
                   gammaln((1 - estimated_mean) * estimated_precision) -
                   np.log(endog * (1 - endog)) - digamma(previous_precision) -
                   np.log(1 - endog) - previous_precision)
Exemplo n.º 4
0
def loglikelihood_function_beta(
    endog,
    exog_mean,
    exog_precision,
    bounded_reg_link,
    param_mean,
    param_precision,
):
    """
    Obtain the log-likelihood function for the beta regression model.
    
    For more details we refer to:
    Ferrari, S. L. P., Cribari-Neto, F. (2004). Beta regression
    for modeling rates and proportions. J. Appl. Statist. 31, 799–815
    
    Simas, A. B., Barreto-Souza, W., Rocha, A. V. (2010). Improved
    estimators for a general class of beta regression models. Computational
    Statistics and Data Analysis, 54, 348–366
    
    We return the value with the minus sign since we want to maximize
    the log-likelihood function and we will utilize the minimize function to
    optimizate.
    
    :param endog (array_like): 1d array of endogenous response variable.
    
    :param exog_mean (array_like): A nobs x k array where nobs is the number 
    of observations and k is the number of mean regressors. An intercept is 
    not included by default and should be added by the user.

    :param exog_precision (array_like): A nobs x q array where nobs is the 
    number of observations and q is the number of precision regressors. 
    An intercept is not included by default and should be added by the user.

    :param bounded_reg_link: An instance of BoundedRegLink. Recall that
    the default mean link is 'logit' and that the default precision link 
    is None.

    :param param_mean: 1d array of mean regression parameters.
    
    :param param_precision: 1d array of precision regression parameters.
    
    """

    estimated_mean = estimate_mean(exog_mean, param_mean, bounded_reg_link)
    estimated_precision = estimate_precision(exog_precision, param_precision,
                                             bounded_reg_link)

    loglik = -np.sum(
        gammaln(estimated_precision) -
        gammaln(estimated_mean * estimated_precision) -
        gammaln((1 - estimated_mean) * estimated_precision) +
        (estimated_mean * estimated_precision - 1) * np.log(endog) +
        ((1 - estimated_mean) * estimated_precision - 1) * np.log(1 - endog))
    return loglik