
    M/Ph?*                        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        g d          Z ej        dd	g          Z eeed
          Z ej        g d          Zej                            eed            ej        g ddz            Z ej        g d          Z eeed
          Z ej        g d          ZdS )    N)doteyediag_indiceszerosonesdiagasarrayr_)solveaqc                    t          |           } | j        dk    r| dddf         } t          |          }|j        dk    r|dddf         }t          |           }t          |          }|dk    rd}nH|dk    rd}n?|dk    r'|                    d          }|st	          d	          nt	          d
|z            ||z  }||k    r||z
  }nd}t          j        t          j        |          t          |df                    }	t          d| 
                                d|         z            }
t          |          }t          |          \  }}||dd         |dd         fxx         dz  cc<   d||dd         dz   |dd         f<   d||dd         |dd         dz   f<   t          t          |
|          |
          }t          ||z   ||z   f          }||d|d|f<   |	|d||df<   |	j        ||dd|f<   t          ||z   df          }||| d<   t          ||          }|d|          }|dk    r3||dz
           | |dz
           z  }|| |d         z  }t           ||f         }|
                                S )a
  
    Modified Denton's method to convert low-frequency to high-frequency data.

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

    Parameters
    ----------
    indicator : array_like
        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"}
        The frequency to use in the conversion.

        * "aq" - Benchmarking an annual series to quarterly.
        * "mq" - Benchmarking a quarterly series to monthly.
        * "other" - Custom stride.  A kwarg, k, must be supplied.
    **kwargs
        Additional keyword argument. For example:

        * k, an int, the number of high-frequency observations that sum to make
          an aggregate low-frequency observation. `k` is used with
          `freq` == "other".

    Returns
    -------
    transformed : ndarray
        The transformed series.

    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.
       Nr      qm   otherkz$k must be supplied with freq="other"zfreq %s not understoodr   g      ?)r	   ndimlenget
ValueErrornpkronr   r   r   squeezer   r   r   Tr   r
   )	indicator	benchmarkfreqkwargsNmr   nqBZinvHTH	diag_idx0	diag_idx1WIAXbiextrapolateds                       ]/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/tsa/interp/denton.pydentonmr1   V   s   ` 	""I~aaaf%		""I~aaaf%	 	IAIA t||		JJsOO 	GEFFF	G 1D8999	!A1uuE 	q		41;;''A 9$$&&rr**++D a&&C'??Iy	!B$1R4()))Q.))),.C	#2#q)CRC.(),.C	#2#	#2#q(()CSMM$A 	qsAaCjAAbqb!eHAbqbeHsAabb!eHqs1gAAqbccFa

A	#A2#A 	1uuqsVIacN"IabbM)q~99;;    __main__)ǧX@333333Y@ǧY@r4   g     X@gffffffY@g̬Y@     `Y@g      Y@g     Y@g     Y@r5   g     @@gfffffA@)r   )gfffffN@g333333@gfffffҏ@g33333@gy@gK@g33333@     l@gfffffB@g     @g̾@r6   r   )2   d      r8      )    i,  r<   r;   )g93LmP@g _@g|\zg@gض(A^@g4-HL@g׆q~Z@g pb@g`}V@gT
FD@gxeR@gj[@g`*S@g?ZaE@gEbW@g7XO-c@gw#k[@g7%M@gk,	^@g89@g@gQHm`@)r   )numpyr   r   r   r   r   r   r   r	   r
   numpy.linalgr   r1   __name__arrayr   r   x_imf	imf_statatestingassert_almost_equalzQYx_dentonx_stata r2   r0   <module>rJ      s                               dX X X Xv z
  D D D E EI %((IGIyt444E P P P Q QIJ""9eQ777 
"""Q&	'	'B&&&''Awr14(((Hbh G G G H HG- -r2   