ó
áp7]c           @   sÃ   d  d l  Z d  d l m Z d  d l j j Z d  d l m	 Z	 d „  Z
 d d d d d d	 d
 e e d d d d „ Z e d „ Z d e f d „  ƒ  YZ d e j f d „  ƒ  YZ e j e e ƒ d S(   iÿÿÿÿN(   t   Results(   t   cache_readonlyc   	         sU   ‡  ‡ ‡ ‡ f d †  } ‡  ‡ ‡ ‡ f d †  } ‡  ‡ ‡ ‡ f d †  } | | | f S(   so  
    Negative penalized log-likelihood functions.

    Returns the negative penalized log-likelihood, its derivative, and
    its Hessian.  The penalty only includes the smooth (L2) term.

    All three functions have argument signature (x, model), where
    ``x`` is a point in the parameter space and ``model`` is an
    arbitrary statsmodels regression model.
    c            sV   | j  } ˆ ˆ d ˆ  t j |  d ƒ d } | j t j |  ˆ  } | | | S(   Ni   i   (   t   nobst   npt   sumt   logliket   r_(   t   paramst   modelR   t   pen_llft   llf(   t   L1_wtt   alphat   kt   loglike_kwds(    s;   lib/python2.7/site-packages/statsmodels/base/elastic_net.pyt	   nploglike)   s    	'c            sI   | j  } ˆ ˆ d ˆ  |  } | j t j |  ˆ  d | } | | S(   Ni   i    (   R   t   scoreR   R   (   R   R   R   t   pen_gradt   gr(   R   R   R   t
   score_kwds(    s;   lib/python2.7/site-packages/statsmodels/base/elastic_net.pyt   npscore/   s    	"c            sE   | j  } ˆ ˆ d ˆ  } | j t j |  ˆ  d | | } | S(   Ni   i    (   i    i    (   R   t   hessianR   R   (   R   R   R   t   pen_hesst   h(   R   R   t	   hess_kwdsR   (    s;   lib/python2.7/site-packages/statsmodels/base/elastic_net.pyt   nphess5   s    	&(    (	   R   R   R   R   R   R   R   R   R   (    (   R   R   R   R   R   R   s;   lib/python2.7/site-packages/statsmodels/base/elastic_net.pyt   _gen_npfuncs   s    t   coord_descentid   g        g      ð?gH¯¼šò×z>g:Œ0âŽyE>c   '      C   s  |  j  j d } |
 d k r" i  n |
 }
 | d k r: i  n | } | d k rR i  n | } t j | ƒ r} | t j | ƒ } n  | d k r› t j | ƒ } n | j ƒ  } d } t j t | ƒ d t	 ƒ} |  j
 ƒ  } t | d <| j d d ƒ } d | k rR| d d k	 rR| d k r3t j | j d ƒ ƒ } qR| t j | j d ƒ ƒ 7} n  g  t | ƒ D]! } t | | | |
 | | ƒ ^ q_} xnt | ƒ D]`} | j ƒ  } xt | ƒ D]} | | rÈq²n  | j ƒ  } d | | <t j |  j  | ƒ } | d k	 r| | 7} n  |  j |  j |  j  d d … | f d | | } | | \ } } } t | | | | | | | | | d	 | d
 |	 ƒ| | <| d k r²t j | | ƒ | k  r²t | | <d | | <q²q²Wt j t j | | ƒ ƒ } | | k  r“Pq“q“Wd | t j | ƒ | k  <| s/t |  | ƒ } t | ƒ St j | ƒ } t j | | f ƒ }  t g  |  j D] } | t |  | d ƒ f ^ q`ƒ } t | ƒ d k rù|  j |  j |  j  d d … | f |  }! |! j ƒ  }" |" j | | <|" j |  t j  | | ƒ <n= |  j |  j |  j  d d … d f |  }! |! j d d ƒ }" t! |" j t" j# ƒ rZ|" j$ j }# n	 |" j }# t% |" d ƒ r~|" j& }$ n d }$ |  j' |  j( }% }& t | ƒ |  _' |  j) |  j' |  _( |# |  | |  d |$ ƒ} t | _* | | _+ i | d d 6| _, |% |& |  _' |  _( | S(   sy	  
    Return an elastic net regularized fit to a regression model.

    Parameters
    ----------
    model : model object
        A statsmodels object implementing ``loglike``, ``score``, and
        ``hessian``.
    method :
        Only the coordinate descent algorithm is implemented.
    maxiter : integer
        The maximum number of iteration cycles (an iteration cycle
        involves running coordinate descent on all variables).
    alpha : scalar or array-like
        The penalty weight.  If a scalar, the same penalty weight
        applies to all variables in the model.  If a vector, it
        must have the same length as `params`, and contains a
        penalty weight for each coefficient.
    L1_wt : scalar
        The fraction of the penalty given to the L1 penalty term.
        Must be between 0 and 1 (inclusive).  If 0, the fit is
        a ridge fit, if 1 it is a lasso fit.
    start_params : array-like
        Starting values for `params`.
    cnvrg_tol : scalar
        If `params` changes by less than this amount (in sup-norm)
        in one iteration cycle, the algorithm terminates with
        convergence.
    zero_tol : scalar
        Any estimated coefficient smaller than this value is
        replaced with zero.
    refit : bool
        If True, the model is refit using only the variables that have
        non-zero coefficients in the regularized fit.  The refitted
        model is not regularized.
    check_step : bool
        If True, confirm that the first step is an improvement and search
        further if it is not.
    loglike_kwds : dict-like or None
        Keyword arguments for the log-likelihood function.
    score_kwds : dict-like or None
        Keyword arguments for the score function.
    hess_kwds : dict-like or None
        Keyword arguments for the Hessian function.

    Returns
    -------
    A results object.

    Notes
    -----
    The ``elastic net`` penalty is a combination of L1 and L2
    penalties.

    The function that is minimized is:

    -loglike/n + alpha*((1-L1_wt)*|params|_2^2/2 + L1_wt*|params|_1)

    where |*|_1 and |*|_2 are the L1 and L2 norms.

    The computational approach used here is to obtain a quadratic
    approximation to the smooth part of the target function:

    -loglike/n + alpha*(1-L1_wt)*|params|_2^2/2

    then repeatedly optimize the L1 penalized version of this function
    along coordinate axes.
    i   g-Cëâ6?t   dtypet   hasconstt   offsett   exposurei    Nt   tolt
   check_stepg        t   maxitert   scaleg      ð?t	   iteration(-   t   exogt   shapet   NoneR   t   isscalart   onest   zerost   copyt   lent   boolt   _get_init_kwdst   Falset   popt   logt   rangeR   t   dott	   __class__t   endogt   _opt_1dt   abst   Truet   maxt   RegularizedResultst   RegularizedResultsWrappert   flatnonzerot   dictt
   _init_keyst   getattrt   fitR   t   normalized_cov_paramst   ix_t
   issubclasst   wrapt   ResultsWrappert   _resultst   hasattrR#   t   df_modelt   df_residR   t   regularizedt   methodt   fit_history('   R   RK   R"   R   R   t   start_paramst	   cnvrg_tolt   zero_tolt   refitR!   R   R   R   t   k_exogR   t   btolt   params_zerot	   init_argst   model_offsetR   t   fgh_listt   itrt   params_savet   params0R   t
   model_1vart   funct   gradt   hesst   pchanget   resultst   iit   covt   model1t   rsltt   klassR#   t   pt   q(    (    s;   lib/python2.7/site-packages/statsmodels/base/elastic_net.pyt   fit_elasticnet>   s    I
1

+!%

4%+			c         C   s>  | } |  | | ƒ }	 | | | ƒ }
 | | | ƒ } |
 | | } | t  j | ƒ k rZ d S| d k rw | |
 | } n% | d k  r• | |
 | } n t  j S| sª | | S|  | | | ƒ | t  j | | ƒ } | |	 | t  j | ƒ d k rû | | Sd d l m } | |  d | f d | d | d f d	 | ƒ} | S(
   sz  
    One-dimensional helper for elastic net.

    Parameters
    ----------
    func : function
        A smooth function of a single variable to be optimized
        with L1 penaty.
    grad : function
        The gradient of `func`.
    hess : function
        The Hessian of `func`.
    model : statsmodels model
        The model being fit.
    start : real
        A starting value for the function argument
    L1_wt : non-negative real
        The weight for the L1 penalty function.
    tol : non-negative real
        A convergence threshold.
    check_step : bool
        If True, check that the first step is an improvement and
        use bisection if it is not.  If False, return after the
        first step regardless.

    Notes
    -----
    ``func``, ``grad``, and ``hess`` have argument signature (x,
    model), where ``x`` is a point in the parameter space and
    ``model`` is the model being fit.

    If the log-likelihood for the model is exactly quadratic, the
    global minimum is returned in one step.  Otherwise numerical
    bisection is used.

    Returns
    -------
    The argmin of the objective function.
    g        i    g»½×Ùß|Û=iÿÿÿÿ(   t   brentt   argst   bracki   R    (   R   R7   t   nant   scipy.optimizeRh   (   R[   R\   R]   R   t   startR   R    R!   t   xt   ft   bt   ct   dR   t   f1Rh   t   x_opt(    (    s;   lib/python2.7/site-packages/statsmodels/base/elastic_net.pyR6   
  s(    5(!/R:   c           B   s    e  Z d  „  Z e d „  ƒ Z RS(   c         C   s   t  t |  ƒ j | | ƒ d  S(   N(   t   superR:   t   __init__(   t   selfR   R   (    (    s;   lib/python2.7/site-packages/statsmodels/base/elastic_net.pyRv   b  s    c         C   s   |  j  j |  j ƒ S(   N(   R   t   predictR   (   Rw   (    (    s;   lib/python2.7/site-packages/statsmodels/base/elastic_net.pyt   fittedvaluese  s    (   t   __name__t
   __module__Rv   R   Ry   (    (    (    s;   lib/python2.7/site-packages/statsmodels/base/elastic_net.pyR:   `  s   	R;   c           B   s)   e  Z i d  d 6d d 6d d 6Z e Z RS(   t   columnsR   t   rowst   residRy   (   Rz   R{   t   _attrst   _wrap_attrs(    (    (    s;   lib/python2.7/site-packages/statsmodels/base/elastic_net.pyR;   j  s
   
(   t   numpyR   t   statsmodels.base.modelR    t   statsmodels.base.wrappert   baset   wrapperRD   t   statsmodels.tools.decoratorsR   R   R'   R/   R8   Rg   R6   R:   RE   R;   t   populate_wrapper(    (    (    s;   lib/python2.7/site-packages/statsmodels/base/elastic_net.pyt   <module>   s   	!	ÊU
	