
    >h-                         S r SSKrSSKrSSKJr  SSKJ	r	J
r
  SSKJr  SSKJr  SSKJr  SSKJr  / S	QrS
 rS r     SS jr " S S5      rg)z+
Seasonal Decomposition by Moving Averages
    N)nanmean)PandasWrapper
array_like)STL)convolution_filter)MSTL)freq_to_period)r   seasonal_decomposeseasonal_meanDecomposeResultr   c           
      B   [        S [        U 5       5       5      nU R                  S   S-
  [        S [        U SSS2   5       5       5      -
  n[        X!-   U5      n[	        X#U-
  5      n[
        R                  R                  [
        R                  [
        R                  " X$5      [
        R                  " XB-
  5      4   XU SS9S   u  pg[
        R                  " SU5      [
        R                  U   -  [
        R                  U   -   R                  nU R                  S:X  a  UR                  5       nXSU& [
        R                  R                  [
        R                  [
        R                  " XS5      [
        R                  " X5-
  5      4   XU SS9S   u  pg[
        R                  " US-   U R                  S   5      [
        R                  U   -  [
        R                  U   -   R                  nU R                  S:X  a  UR                  5       nXUS-   S& U $ )z
Replace nan values on trend's end-points with least-squares extrapolated
values with regression considering npoints closest defined points.
c              3      #    U  H:  u  p[         R                  " [         R                  " U5      5      (       a  M6  Uv   M<     g 7fNnpanyisnan.0ivalss      kC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\statsmodels/tsa/seasonal.py	<genexpr>%_extrapolate_trend.<locals>.<genexpr>   s+      )ga1G)
   5A	Ar      c              3      #    U  H:  u  p[         R                  " [         R                  " U5      5      (       a  M6  Uv   M<     g 7fr   r   r   s      r   r   r   "   s/      
166"((4.) A1r   N)rcond)next	enumerateshapeminmaxr   linalglstsqc_arangeonesTndimsqueeze)	trendnpointsfrontback
front_last
back_firstknextras	            r   _extrapolate_trendr6      s   
  "5) E 	A
	
 
$U4R4[1
 
	
 	 U_d+JU7N+J99??
bii*BGGJ4F,GGHJ   		DA
 YYq% 2558+beeAh699EzzQ&5M99??
bii
)27743D+EEF   		DA
 YYtaxQ02558;beeAhFIIEzzQ$(*L    c                     [         R                  " [        U5       Vs/ s H  n[        XSU2   SS9PM     sn5      $ s  snf )z
Return means for each period in x. period is an int that gives the
number of periods per cycle. E.g., 12 for monthly. NaNs are ignored
in the mean.
Nr   axis)r   arrayrange
pd_nanmean)xperiodr   s      r   r   r   B   s8     88uV}M}!Z)V)15}MNNMs   >c                 x   Un[        U 5      nUc  [        [        U SS5      SS5      n[        U SSS9n [        U 5      n[        R
                  " [        R                  " U 5      5      (       d  [        S5      eUR                  S5      (       a)  [        R                  " U S	:*  5      (       a  [        S
5      eUc  Ub  [        U5      nUnO[        S5      eU R                  S	   SU-  :  a"  [        SSU-   SU R                  S	    S35      eUcL  US-  S	:X  a)  [        R                  " S/S/US-
  -  -   S/-   5      U-  nO[        R                  " SU-  U5      n[        U5      S-   n	[        XU	5      n
US:X  a  US-
  nUS	:  a  [!        XS-   5      n
UR                  S5      (       a  X
-  nOX
-
  n[#        X5      nUR                  S5      (       a  U[        R$                  " US	S9-  nOU[        R$                  " US	S9-  n[        R&                  " UR(                  X-  S-   5      R(                  SU nUR                  S5      (       a  X-  U
-  nOX-
  n/ n[+        XX4S5       H3  u  nnUR-                  UR/                  UR1                  5       US95        M5     [3        US	   US   US   US   S9$ )a  
Seasonal decomposition using moving averages.

Parameters
----------
x : array_like
    Time series. If 2d, individual series are in columns. x must contain 2
    complete cycles.
model : {"additive", "multiplicative"}, optional
    Type of seasonal component. Abbreviations are accepted.
filt : array_like, optional
    The filter coefficients for filtering out the seasonal component.
    The concrete moving average method used in filtering is determined by
    two_sided.
period : int, optional
    Period of the series (e.g., 1 for annual, 4 for quarterly, etc). Must
    be used if x is not a pandas object or if the index of x does not have
    a frequency. Overrides default periodicity of x if x is a pandas
    object with a timeseries index.
two_sided : bool, optional
    The moving average method used in filtering.
    If True (default), a centered moving average is computed using the
    filt. If False, the filter coefficients are for past values only.
extrapolate_trend : int or 'freq', optional
    If set to > 0, the trend resulting from the convolution is
    linear least-squares extrapolated on both ends (or the single one
    if two_sided is False) considering this many (+1) closest points.
    If set to 'freq', use `freq` closest points. Setting this parameter
    results in no NaN values in trend or resid components.

Returns
-------
DecomposeResult
    A object with seasonal, trend, and resid attributes.

See Also
--------
statsmodels.tsa.filters.bk_filter.bkfilter
    Baxter-King filter.
statsmodels.tsa.filters.cf_filter.cffilter
    Christiano-Fitzgerald asymmetric, random walk filter.
statsmodels.tsa.filters.hp_filter.hpfilter
    Hodrick-Prescott filter.
statsmodels.tsa.filters.convolution_filter
    Linear filtering via convolution.
statsmodels.tsa.seasonal.STL
    Season-Trend decomposition using LOESS.

Notes
-----
This is a naive decomposition. More sophisticated methods should
be preferred.

The additive model is Y[t] = T[t] + S[t] + e[t]

The multiplicative model is Y[t] = T[t] * S[t] * e[t]

The results are obtained by first estimating the trend by applying
a convolution filter to the data. The trend is then removed from the
series and the average of this de-trended series for each period is
the returned seasonal component.
Nindexinferred_freqr>      )maxdimz,This function does not handle missing valuesmr   zJMultiplicative seasonality is not appropriate for zero and negative valueszxYou must specify a period or x must be a pandas object with a PeriodIndex or a DatetimeIndex with a freq not set to Nonez'x must have 2 complete cycles requires z observations. x only has z observation(s)g      ?r   g      ?freqr9   )seasonalr-   residN)columns   )rG   r-   rH   observed)r   getattrr   lenr   allisfinite
ValueError
startswithr   r	   r"   r;   repeatintr   r6   r   meantiler*   zipappendwrapr,   r   )r>   modelfiltr?   	two_sidedextrapolate_trendpfreqpwnobsnsidesr-   	detrendedperiod_averagesrG   rH   resultssnames                     r   r
   r
   K   s   L E	q	B~7D1?DI1c!$Aq6D66"++a.!!GHH66!q&>>/ 
 ~"5)EFO  	wwqzAI5a%i[ A(()
|?D
 	

 |A:?88SEQC6A:$66#>?&HD99S6\62D^aFq/EF""QJ1"5a*?@I	I	#I6O277?;;277?;;ww(($.1*<=??FHu$$G	%#%I4 	rwwqyy{Dw9: ajaj	 r7   c                       \ rS rSrSrSS jr\S 5       r\S 5       r\S 5       r	\S 5       r
\S	 5       r\S
 5       r     SS jrSrg)r      a  
Results class for seasonal decompositions

Parameters
----------
observed : array_like
    The data series that has been decomposed.
seasonal : array_like
    The seasonal component of the data series.
trend : array_like
    The trend component of the data series.
resid : array_like
    The residual component of the data series.
weights : array_like, optional
    The weights used to reduce outlier influence.
Nc                     X l         X0l        UcT  [        R                  " U5      n[	        U[
        R                  5      (       a  [
        R                  " XQR                  SS9nXPl        X@l	        Xl
        g )Nweights)rA   re   )	_seasonal_trendr   	ones_like
isinstancepdSeriesrA   _weights_resid	_observed)selfrK   rG   r-   rH   ri   s         r   __init__DecomposeResult.__init__   sX    !?ll8,G(BII..))>>	  !r7   c                     U R                   $ )zObserved data)rr   rs   s    r   rK   DecomposeResult.observed        ~~r7   c                     U R                   $ )z The estimated seasonal component)rj   rw   s    r   rG   DecomposeResult.seasonal  ry   r7   c                     U R                   $ )zThe estimated trend component)rk   rw   s    r   r-   DecomposeResult.trend	       {{r7   c                     U R                   $ )zThe estimated residuals)rq   rw   s    r   rH   DecomposeResult.resid  r~   r7   c                     U R                   $ )z)The weights used in the robust estimation)rp   rw   s    r   ri   DecomposeResult.weights  s     }}r7   c                 .    U R                   R                  $ )zNumber of observations)rr   r"   rw   s    r   r_   DecomposeResult.nobs  s     ~~###r7   c                 (   SSK Jn  SSKJn  U" 5       nU" 5         U(       a  U R                  S4/O/ n	X(       a  U R
                  S4/O/ -  n	U R                  R                  S:X  a  X(       a  U R                  S4/O/ -  n	OU R                  R                  S:  a  [        U R                  [        R                  5      (       a<  U R                  R                   H!  n
U	U(       a  U R                  U
   S4/O/ -  n	M#     OK[        U R                  R                  S   5       H%  nU	U(       a  U R                  SS2U4   S4/O/ -  n	M'     X(       a  U R                  S	4/O/ -  n	X(       a  U R                  S
4/O/ -  n	[        U R                  [        R                  [        R                   45      (       aO  U R                  R                  S   nU R                  R"                  S   U R                  R"                  US-
     4nOSU R                  R                  S   S-
  4nUR%                  ['        U	5      SSS9u  p[)        [+        X5      5       H  u  nu  nu  n	nUS	:w  a  UR-                  U	5        O#UR-                  U	SSS9  UR-                  USSSS9  [/        U	SU5      nUS:w  a  UR1                  5       nUS:X  a  U(       a  UR2                  OUR4                  nU" U5        UR7                  U5        M     UR9                  5         U$ )a  
Plot estimated components

Parameters
----------
observed : bool
    Include the observed series in the plot
seasonal : bool
    Include the seasonal component in the plot
trend : bool
    Include the trend component in the plot
resid : bool
    Include the residual in the plot
weights : bool
    Include the weights in the plot (if any)

Returns
-------
matplotlib.figure.Figure
    The figure instance that containing the plot.
r   )register_matplotlib_converters)_import_mplObservedr-   r   rG   Nresidualri   T)sharexonone)marker	linestyle)r   r   z#000000)colorzorderre   )pandas.plottingr   statsmodels.graphics.utilsr   rr   r-   rG   r+   rm   rn   	DataFramerI   r<   r"   rH   ri   ro   rA   subplotsrM   r!   rV   plotrL   
capitalize	set_title
set_ylabelset_xlimtight_layout)rs   rK   rG   r-   rH   ri   r   r   pltseriescolr   r_   xlimfigaxsaxdef_namere   titles                       r   r   DecomposeResult.plot  s   : 	C:m&(3;4>>:./UDJJ():=="xz23RGF]]!#$--66==00C>F$--,j9:BF 1
 t}}22156A?G$--1-z:;RF 7
 	DJJ
+,2=DLL),-b@dnnr||RYY&?@@>>''*D>>''*DNN,@,@,JJDt~~++A.23D<<FQt<<+4S5E+F'A'&VX:%sf=fIbA6684D:%($%FxBLLR]]E$KKK ,G 	
r7   )rr   rq   rj   rk   rp   r   )TTTTF)__name__
__module____qualname____firstlineno____doc__rt   propertyrK   rG   r-   rH   ri   r_   r   __static_attributes__ r7   r   r   r      s    ""           $ $ Lr7   r   )additiveNNTr   )r   numpyr   pandasrn   pandas.core.nanopsr   r=   statsmodels.tools.validationr   r   statsmodels.tsa.stl._stlr   #statsmodels.tsa.filters.filtertoolsr   statsmodels.tsa.stl.mstlr   statsmodels.tsa.tsatoolsr	   __all__r6   r   r
   r   r   r7   r   <module>r      s[      4 B ( B ) 3(VO 	RjI Ir7   