
    M/PhC'                         d Z ddlmZmZ ddlmZ ddlZddlZddl	Z
ddlmZ ddlmZ ddlmZ ddlmZ  G d	 d
          ZdS )a  
Author: Kishan Manani
License: BSD-3 Clause

An implementation of MSTL [1], an algorithm for time series decomposition when
there are multiple seasonal components.

This implementation has the following differences with the original algorithm:
- Missing data must be handled outside of this class.
- The algorithm proposed in the paper handles a case when there is no
seasonality. This implementation assumes that there is at least one seasonal
component.

[1] K. Bandura, R.J. Hyndman, and C. Bergmeir (2021)
MSTL: A Seasonal-Trend Decomposition Algorithm for Time Series with Multiple
Seasonal Patterns
https://arxiv.org/pdf/2107.13462.pdf
    )OptionalUnion)SequenceN)boxcox)ArrayLike1D)STL)freq_to_periodc                      e Zd ZdZdddddddedeeeee         f                  deeeee         f                  deee	e
f                  d	ed
eee
eeedf         f                  fdZd Zd Zdeeee         df         deeee         df         deee         ee         f         fdZdeeee         df         dee         fdZdeeee         df         dedee         fdZdefdZedeee         ee         f         fd            Zed
edefd            Zededee         fd            Zed             ZdS )MSTLa
  
    MSTL(endog, periods=None, windows=None, lmbda=None, iterate=2,
         stl_kwargs=None)

    Season-Trend decomposition using LOESS for multiple seasonalities.

    .. versionadded:: 0.14.0

    Parameters
    ----------
    endog : array_like
        Data to be decomposed. Must be squeezable to 1-d.
    periods : {int, array_like, None}, optional
        Periodicity of the seasonal components. If None and endog is a pandas
        Series or DataFrame, attempts to determine from endog. If endog is a
        ndarray, periods must be provided.
    windows : {int, array_like, None}, optional
        Length of the seasonal smoothers for each corresponding period.
        Must be an odd integer, and should normally be >= 7 (default). If None
        then default values determined using 7 + 4 * np.arange(1, n + 1, 1)
        where n is number of seasonal components.
    lmbda : {float, str, None}, optional
        The lambda parameter for the Box-Cox transform to be applied to `endog`
        prior to decomposition. If None, no transform is applied. If "auto", a
        value will be estimated that maximizes the log-likelihood function.
    iterate : int, optional
        Number of iterations to use to refine the seasonal component.
    stl_kwargs: dict, optional
        Arguments to pass to STL.

    See Also
    --------
    statsmodels.tsa.seasonal.STL

    References
    ----------
    .. [1] K. Bandura, R.J. Hyndman, and C. Bergmeir (2021)
        MSTL: A Seasonal-Trend Decomposition Algorithm for Time Series with
        Multiple Seasonal Patterns. arXiv preprint arXiv:2107.13462.

    Examples
    --------
    Start by creating a toy dataset with hourly frequency and multiple seasonal
    components.

    >>> import numpy as np
    >>> import matplotlib.pyplot as plt
    >>> import pandas as pd
    >>> pd.plotting.register_matplotlib_converters()
    >>> np.random.seed(0)
    >>> t = np.arange(1, 1000)
    >>> trend = 0.0001 * t ** 2 + 100
    >>> daily_seasonality = 5 * np.sin(2 * np.pi * t / 24)
    >>> weekly_seasonality = 10 * np.sin(2 * np.pi * t / (24 * 7))
    >>> noise = np.random.randn(len(t))
    >>> y = trend + daily_seasonality + weekly_seasonality + noise
    >>> index = pd.date_range(start='2000-01-01', periods=len(t), freq='h')
    >>> data = pd.DataFrame(data=y, index=index)

    Use MSTL to decompose the time series into two seasonal components
    with periods 24 (daily seasonality) and 24*7 (weekly seasonality).

    >>> from statsmodels.tsa.seasonal import MSTL
    >>> res = MSTL(data, periods=(24, 24*7)).fit()
    >>> res.plot()
    >>> plt.tight_layout()
    >>> plt.show()

    .. plot:: plots/mstl_plot.py
    N   )periodswindowslmbdaiterate
stl_kwargsendogr   r   r   r   r   c                   || _         |                     |          | _        | j        j        d         | _        || _        |                     ||          \  | _        | _        || _	        | 
                    |r|ni           | _        d S )Nr   )r   _to_1d_array_yshapenobsr   _process_periods_and_windowsr   r   r   _remove_overloaded_stl_kwargs_stl_kwargs)selfr   r   r   r   r   r   s          X/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/tsa/stl/mstl.py__init__zMSTL.__init__h   s     
##E**GM!$	
%)%F%FW&
 &
"dl ==$,JJ"
 
    c           	         t          | j                  }|dk    rdn| j        }| j        dk    r!t	          | j        d          \  }}|| _        n*| j        rt	          | j        | j                  }n| j        }| j                            dd          }| j                            dd          }t          j
        || j        f          }|}t          |          D ]t}	t          |          D ]b}
|||
         z   }t          d|| j        |
         | j        |
         d| j                            ||	          }|j        ||
<   |||
         z
  }cut          j        |j                  }|j        }|j        }||z
  }t+          | j        t.          j        t.          j        f          r| j        j        }t/          j        ||d
          }t/          j        ||d          }t/          j        ||d          }t/          j        ||d          }d | j        D             }|j        dk    rt/          j        ||d          }nt/          j        |||          }ddlm}  ||||||          S )z
        Estimate a trend component, multiple seasonal components, and a
        residual component.

        Returns
        -------
        DecomposeResult
            Estimation results.
           autoN)r   
inner_iter
outer_iter)r   r   periodseasonal)r"   r#   observed)indexnametrendresidrobust_weightc                     g | ]}d | S )	seasonal_ ).0r%   s     r   
<listcomp>zMSTL.fit.<locals>.<listcomp>   s!    DDDV(((DDDr   r&   )r(   columnsr   )DecomposeResultr/   )lenr   r   r   r   r   	est_lmbdar   popnpzerosr   ranger   r   fitr&   squeezeTr*   weights
isinstancer   pdSeries	DataFramer(   ndimstatsmodels.tsa.seasonalr3   )r   num_seasonsr   yr   stl_inner_iterstl_outer_iterr&   deseas_iresr*   rwr+   r(   colsr3   s                     r   r:   zMSTL.fit~   s    $,''"a''!!T\ :dgT222HAu"DNNZ 	twdj111AAA )--lDAA)--lDAA 8;	":;;;w 
	. 
	.A;'' 	. 	.(1+-  <?!\!_  &	 
 #N#KK  "l(1+-	. :hj))	[ dj29bl";<< 
	MJ$E	!5z:::AIe5w???EIe5w???E2UAAABDDt|DDDD}!!9XULLL<tLLL 	=<<<<<q(E5"===r   c           	      H    d| j          d| j         d| j         d| j         d	S )NzMSTL(endog, periods=z
, windows=z, lmbda=z
, iterate=))r   r   r   r   )r   s    r   __str__zMSTL.__str__   sQ    (( (( ( j( ( 	( ( (	
r   returnc                 H                          |          }|r>                     |t          |                    }                     ||          \  }}n3                     |t          |                    }t	          |          }t          |          t          |          k    rt          d          t           fd|D                       rLt          j        dt                     t           fd|D                       }|d t          |                   }||fS )N)rD   )Periods and windows must have same lengthc              3   4   K   | ]}|j         d z  k    V  dS r   Nr   r0   r%   r   s     r   	<genexpr>z4MSTL._process_periods_and_windows.<locals>.<genexpr>   s.      ==6vQ&======r   zTA period(s) is larger than half the length of time series. Removing these period(s).c              3   8   K   | ]}|j         d z  k     |V  dS rU   rV   rW   s     r   rX   z4MSTL._process_periods_and_windows.<locals>.<genexpr>   s<        !Q0F0F0F0F0F0F r   )_process_periods_process_windowsr4   _sort_periods_and_windowssorted
ValueErroranywarningswarnUserWarningtuple)r   r   r   s   `  r   r   z!MSTL._process_periods_and_windows   sD   
 ''00 	&++GW+NNG#==gwOOGWW++GW+NNGWooGw<<3w<<''HIII ====W===== 	.M-.9       %,    G nGn-Gr   c                 j    ||                                  f}nt          |t                    r|f}n	 |S N)_infer_periodr>   int)r   r   s     r   rZ   zMSTL._process_periods   sB     ?))++-GG%% 	jGGr   rD   c                 j    ||                      |          }nt          |t                    r|f}n	 |S re   )_default_seasonal_windowsr>   rg   )r   r   rD   s      r   r[   zMSTL._process_windows   sA    
 ?44[AAGG%% 	jGGr   c                     d }t          | j        t          j        t          j        f          rt          | j        j        dd           }|t          d          t          |          }|S )Ninferred_freqz%Unable to determine period from endog)	r>   r   r?   r@   rA   getattrr(   r^   r	   )r   freqr%   s      r   rf   zMSTL._infer_period   sa    dj29bl";<< 	D4:+_dCCD<DEEE%%r   c                     t          |           t          |          k    rt          d          t          t          t          | |                     \  } }| |fS )NrS   )r4   r^   zipr]   )r   r   s     r   r\   zMSTL._sort_periods_and_windows  sV     w<<3w<<''HIIIs7G'<'< = =>r   c                 D    g d}|D ]}|                      |d            | S )Nr$   )r6   )r   argsargs      r   r   z"MSTL._remove_overloaded_stl_kwargs  s9    ... 	& 	&CNN3%%%%r   nc                 V    t          d t          d| dz             D                       S )Nc              3   &   K   | ]}d d|z  z   V  dS )      Nr/   )r0   rJ   s     r   rX   z1MSTL._default_seasonal_windows.<locals>.<genexpr>  s*      881QQY888888r   r    )rc   r9   )rs   s    r   ri   zMSTL._default_seasonal_windows  s+    88aQ888888r   c                     t          j        t          j        t          j        |                     t           j                  }|j        dk    rt          d          |S )N)dtyper    zy must be a 1d array)r7   ascontiguousarrayr;   asarraydoublerB   r^   )xrE   s     r   r   zMSTL._to_1d_array  sJ     BJqMM!:!:")LLL6Q;;3444r   )__name__
__module____qualname____doc__r   r   r   rg   r   floatstrdictboolr   r:   rP   rc   r   rZ   r[   rf   staticmethodr\   r   ri   r   r/   r   r   r   r       s       E EV 8<7;-1BF
 
 

 %Xc] 234	

 %Xc] 234
 eSj)*
 
 T#uS$_'="=>?
 
 
 
,>> >> >>@
 
 
 sHSM4/0  sHSM4/0  
x}hsm+	,	       <	S(3-56		#	 	 	 	sHSM4/0  
#	   s      	x}hsm+	,      \  $ 4    \ 9S 9Xc] 9 9 9 \9   \  r   r   )r   typingr   r   collections.abcr   r`   numpyr7   pandasr?   scipy.statsr   statsmodels.tools.typingr   statsmodels.tsa.stl._stlr   statsmodels.tsa.tsatoolsr	   r   r/   r   r   <module>r      s    $ # " " " " " " " $ $ $ $ $ $                0 0 0 0 0 0 ( ( ( ( ( ( 3 3 3 3 3 3         r   