ó
áp7]c           @   sR   d  Z  d d l Z d d l m Z d d l m Z m Z d e f d „  ƒ  YZ	 d S(   sL   
Created on Sun May 10 08:23:48 2015

Author: Josef Perktold
License: BSD-3
iÿÿÿÿNi   (   t   NonePenalty(   t   approx_fprime_cst   approx_fprimet   PenalizedMixinc           B   s‰   e  Z d  Z d „  Z d d „ Z d d „ Z d d „ Z d d d „ Z d d „ Z	 d d „ Z
 d d	 „ Z d d
 „ Z d d d „ Z RS(   s  Mixin class for Maximum Penalized Likelihood

    Parameters
    ----------
    args and kwds for the model super class
    penal : None or instance of Penalized function class
        If penal is None, then NonePenalty is used.
    pen_weight : float or None
        factor for weighting the penalization term.
        If None, then pen_weight is set to nobs.


    TODO: missing **kwds or explicit keywords

    TODO: do we adjust the inherited docstrings?
    We would need templating to add the penalization parameters

    c         O   sÐ   | j  d d  ƒ |  _ | j  d d  ƒ |  _ t t |  ƒ j | | Ž  |  j d  k rg t |  j ƒ |  _ n  |  j d  k rŽ t	 ƒ  |  _ d |  _ n  |  j
 j d d g ƒ t |  d g  ƒ |  _ |  j j d g ƒ d  S(   Nt   penalt
   pen_weighti    t   _null_drop_keys(   t   popt   NoneR   R   t   superR   t   __init__t   lent   endogR    t
   _init_keyst   extendt   getattrR   (   t   selft   argst   kwds(    (    s:   lib/python2.7/site-packages/statsmodels/base/_penalized.pyR
   "   s    c         K   sI   | d  k rE t |  d ƒ r< |  j | ƒ } |  j | ƒ } qE d } n  | S(   Nt	   scaletypei   (   R   t   hasattrt   predictt   estimate_scale(   R   t   paramst   scaleR   t   mu(    (    s:   lib/python2.7/site-packages/statsmodels/base/_penalized.pyt   _handle_scale9   s    	c         K   sz   | d k r |  j } n  t t |  ƒ j | |  } | d k rv |  j | |  } | d | | |  j j | ƒ 8} n  | S(   s3   
        Log-likelihodo of model at params
        i    i   N(   R   R   R	   R   t   loglikeR   R   t   func(   R   R   R   R   t   llfR   (    (    s:   lib/python2.7/site-packages/statsmodels/base/_penalized.pyR   E   s    %c         K   s‘   | d k r |  j } n  t t |  ƒ j | |  } t | j d ƒ } | d k r |  j | |  } | d | | | |  j j	 | ƒ 8} n  | S(   s@   
        Log-likelihood of model observations at params
        i    i   N(
   R   R   R	   R   t
   loglikeobst   floatt   shapeR   R   R   (   R   R   R   R   R   t   nobs_llfR   (    (    s:   lib/python2.7/site-packages/statsmodels/base/_penalized.pyR   S   s    )t   fdc            su   ˆ d k r ˆ j ‰ n  ‡  ‡ ‡ f d †  } | d k rF t | | ƒ S| d k re t | | d t ƒSt d ƒ ‚ d S(   s5   score based on finite difference derivative

        c            s   ˆ j  |  d ˆ ˆ  S(   NR   (   R   (   t   p(   R   R   R   (    s:   lib/python2.7/site-packages/statsmodels/base/_penalized.pyt   <lambda>j   t    t   csR"   t   centereds-   method not recognized, should be "fd" or "cs"N(   R   R   R   R   t   Truet
   ValueError(   R   R   R   t   methodR   R   (    (   R   R   R   s:   lib/python2.7/site-packages/statsmodels/base/_penalized.pyt   score_numdiffc   s    c         K   sz   | d k r |  j } n  t t |  ƒ j | |  } | d k rv |  j | |  } | d | | |  j j | ƒ 8} n  | S(   s-   
        Gradient of model at params
        i    i   N(   R   R   R	   R   t   scoreR   R   t   deriv(   R   R   R   R   t   scR   (    (    s:   lib/python2.7/site-packages/statsmodels/base/_penalized.pyR,   s   s    %c         K   s‘   | d k r |  j } n  t t |  ƒ j | |  } t | j d ƒ } | d k r |  j | |  } | d | | | |  j j	 | ƒ 8} n  | S(   s:   
        Gradient of model observations at params
        i    i   N(
   R   R   R	   R   t	   score_obsR   R    R   R   R-   (   R   R   R   R   R.   t   nobs_scR   (    (    s:   lib/python2.7/site-packages/statsmodels/base/_penalized.pyR/      s    )c            sJ   ˆ d k r ˆ j ‰ n  ‡  ‡ ‡ f d †  } d d l m } | | | ƒ S(   s7   hessian based on finite difference derivative

        c            s   ˆ j  |  d ˆ ˆ  S(   NR   (   R   (   R#   (   R   R   R   (    s:   lib/python2.7/site-packages/statsmodels/base/_penalized.pyR$   –   R%   iÿÿÿÿ(   t   approx_hessN(   R   R   t   statsmodels.tools.numdiffR1   (   R   R   R   R   R   R1   (    (   R   R   R   s:   lib/python2.7/site-packages/statsmodels/base/_penalized.pyt   hessian_numdiff   s
    c         K   s±   | d k r |  j } n  t t |  ƒ j | |  } | d k r­ |  j | |  } |  j j | ƒ } | j d k r” | d | t	 j
 | | ƒ 8} q­ | d | | | 8} n  | S(   s,   
        Hessian of model at params
        i    i   N(   R   R   R	   R   t   hessianR   R   t   deriv2t   ndimt   npt   diag(   R   R   R   R   t   hessR   t   h(    (    s:   lib/python2.7/site-packages/statsmodels/base/_penalized.pyR4   ›   s    "c         K   s  t  |  d ƒ r& | j i d d 6ƒ n  | d k r; d } n  | d k rP t } n  t t |  ƒ j d | |  } | t k r~ | S| t k r“ d } n  t j	 t j
 | j ƒ | k  ƒ d } t j	 t j
 | j ƒ | k ƒ d } | j ƒ  rÿ |  j | |  } | S| Sd S(   s‡  minimize negative penalized log-likelihood

        Parameters
        ----------
        method : None or str
            Method specifies the scipy optimizer as in nonlinear MLE models.
        trim : Boolean or float
            Default is False or None, which uses no trimming.
            If trim is True or a float, then small parameters are set to zero.
            If True, then a default threshold is used. If trim is a float, then
            it will be used as threshold.
            The default threshold is currently 1e-4, but it will change in
            future and become penalty function dependent.
        kwds : extra keyword arguments
            This keyword arguments are treated in the same way as in the
            fit method of the underlying model class.
            Specifically, additional optimizer keywords and cov_type related
            keywords can be added.

        t   familyi    t   max_start_irlst   bfgsR*   g-Cëâ6?N(   R   t   updateR   t   FalseR	   R   t   fitR(   R7   t   nonzerot   absR   t   anyt
   _fit_zeros(   R   R*   t   trimR   t   rest
   drop_indext
   keep_indext   res_aux(    (    s:   lib/python2.7/site-packages/statsmodels/base/_penalized.pyR@   ­   s"    			%%N(   t   __name__t
   __module__t   __doc__R
   R   R   R   R   R+   R,   R/   R3   R4   R@   (    (    (    s:   lib/python2.7/site-packages/statsmodels/base/_penalized.pyR      s   	(
   RL   t   numpyR7   t
   _penaltiesR    R2   R   R   t   objectR   (    (    (    s:   lib/python2.7/site-packages/statsmodels/base/_penalized.pyt   <module>   s   