ó
áp7]c           @   s¸  d  d l  Z d  d l  m Z m Z m Z m Z m Z m Z m Z m	 Z	 d  d l
 m Z d d „ Z e d k r´e j d d d	 d d
 d d d d d d d g ƒ Z e j d d g ƒ Z e e e d d ƒZ e j d d d d d d d d d d d d g ƒ Z e j j e e d ƒ e j d  d! d" d! g d# ƒ Z e j d$ d% d& d% d$ g ƒ Z e e e d d ƒZ e j d' d( d) d* d+ d, d- d. d/ d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 d: g ƒ Z n  d S(;   iÿÿÿÿN(   t   dott   eyet   diag_indicest   zerost   onest   diagt   asarrayt   r_(   t   solvet   aqc         K   sæ  t  |  ƒ }  |  j d k r4 |  d d … d f }  n  t  | ƒ } | j d k rh | d d … d f } n  t |  ƒ } t | ƒ } | d k r• d } nX | d k rª d } nC | d k rÝ | j d ƒ } | sí t d	 ƒ ‚ qí n t d
 | ƒ ‚ | | } | | k r| | } n d } t j t j | ƒ t	 | d f ƒ ƒ }	 t
 d |  j ƒ  |  ƒ }
 t | ƒ } t | ƒ \ } } | | d d !| d d !f c d 7<d | | d  d | d  f <d | | d  | d  d f <t t |
 | ƒ |
 ƒ } t | | | | f ƒ } | | d | … d | … f <|	 | d | … | d … f <|	 j | | d … d | … f <t | | d f ƒ } | | | )t | | ƒ } | |  } | d k rÜ| | d |  | d } | |  | } t | | f } n  | j ƒ  S(   s
  
    Modified Denton's method to convert low-frequency to high-frequency data.

    Uses proportionate first-differences as the penalty function.  See notes.

    Parameters
    ----------
    indicator
        A low-frequency indicator series.  It is assumed that there are no
        pre-sample indicators.  Ie., the first indicators line up with
        the first benchmark.
    benchmark : array-like
        The higher frequency benchmark.  A 1d or 2d data series in columns.
        If 2d, then M series are assumed.
    freq : str {"aq","qm", "other"}
        "aq" - Benchmarking an annual series to quarterly.
        "mq" - Benchmarking a quarterly series to monthly.
        "other" - Custom stride.  A kwarg, k, must be supplied.
    kwargs :
        k : int
            The number of high-frequency observations that sum to make an
            aggregate low-frequency observation. `k` is used with
            `freq` == "other".
    Returns
    -------
    benchmarked series : array

    Examples
    --------
    >>> indicator = [50,100,150,100] * 5
    >>> benchmark = [500,400,300,400,500]
    >>> benchmarked = dentonm(indicator, benchmark, freq="aq")

    Notes
    -----
    Denton's method minimizes the distance given by the penalty function, in
    a least squares sense, between the unknown benchmarked series and the
    indicator series subject to the condition that the sum of the benchmarked
    series is equal to the benchmark. The modification allows that the first
    value not be pre-determined as is the case with Denton's original method.
    If the there is no benchmark provided for the last few indicator
    observations, then extrapolation is performed using the last
    benchmark-indicator ratio of the previous period.

    Minimizes sum((X[t]/I[t] - X[t-1]/I[t-1])**2)

    s.t.

    sum(X) = A, for each period.  Where X is the benchmarked series, I is
    the indicator, and A is the benchmark.

    References
    ----------
    Bloem, A.M, Dippelsman, R.J. and Maehle, N.O.  2001 Quarterly National
        Accounts Manual--Concepts, Data Sources, and Compilation. IMF.
        http://www.imf.org/external/pubs/ft/qna/2000/Textbook/index.htm
    Cholette, P. 1988. "Benchmarking systems of socio-economic time series."
        Statistics Canada, Time Series Research and Analysis Division,
        Working Paper No TSRA-88-017E.
    Denton, F.T. 1971. "Adjustment of monthly or quarterly series to annual
        totals: an approach based on quadratic minimization." Journal of the
        American Statistical Association. 99-102.
    i   NR	   i   t   qmi   t   othert   ks$   k must be supplied with freq="other"s   freq %s not understoodi    g      ð?iÿÿÿÿ(   R   t   ndimt   Nonet   lent   gett
   ValueErrort   npt   kronR   R   R   t   squeezeR   R    R   t   TR   R   (   t	   indicatort	   benchmarkt   freqt   kwargst   Nt   mR   t   nt   qt   Bt   Zinvt   HTHt	   diag_idx0t	   diag_idx1t   Wt   It   At   Xt   bit   extrapolated(    (    s<   lib/python2.7/site-packages/statsmodels/tsa/interp/denton.pyt   dentonmX   sT    K		
'	$t   __main__gÍÌÌÌÌŒX@g333333Y@gÍÌÌÌÌŒY@g     ÀX@gffffffY@gÍÌÌÌÌ¬Y@g     `Y@g      Y@g     ÀY@g     àY@g     @¯@gfffffA°@R   gfffffNŽ@g333333@gfffffÒ@g33333«@gš™™™™y@gš™™™™K@g33333‘@g     l@gfffffB@g     ª@gÍÌÌÌÌ¾@i   i2   id   i–   i   iô  i  i,  g9µ3LmP@gúÕ ˜ó_@g³˜Ø|\zg@gØ¶(³A^@g4¸­-HL@g×†Šq~Z@g æËpb@g’`}V@gT«¯®
FD@gxe¨ŠœR@gÎüj[@g±Ÿ`*S@g÷?ÀZµaE@gE½Œb‰W@g7ŽX‹O-c@gšwœ¢#k[@gäù¨7%M@gãkÏ,	¨^@g8ó«9@Íg@gQ÷Hm`@(   t   numpyR   R    R   R   R   R   R   R   R   t   numpy.linalgR   R)   t   __name__t   arrayR   R   t   x_imft	   imf_statat   testingt   assert_almost_equalt   zQt   Yt   x_dentont   x_stata(    (    (    s<   lib/python2.7/site-packages/statsmodels/tsa/interp/denton.pyt   <module>   s&   :S–0