
    M/Ph#Z                        d Z ddlmZm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mZmZmZmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZm Z  erddl!Z"de#dej$        dej$        fdZ% G d d          Z& G d d          Z'dS )a  
Implementation of the Theta forecasting method of

Assimakopoulos, V., & Nikolopoulos, K. (2000). The theta model: a decomposition
approach to forecasting. International journal of forecasting, 16(4), 521-530.

and updates in

Hyndman, R. J., & Billah, B. (2003). Unmasking the Theta method. International
Journal of Forecasting, 19(2), 287-290.

Fioruci, J. A., Pellegrini, T. R., Louzada, F., & Petropoulos, F. (2015).
The optimized theta method. arXiv preprint arXiv:1503.03529.
    )TYPE_CHECKINGOptionalN)stats)Summary)SimpleTable)
array_like	bool_like
float_likeint_likestring_like)DeterministicTerm)seasonal_decompose)ExponentialSmoothing)SARIMAX)acf)	add_trendfreq_to_periodstepsindexreturnc                 ,    t          j        ||           S )N)r   _extend_index)r   r   s     a/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/tsa/forecasting/theta.pyextend_indexr   +   s    *5%888    c                   (   e Zd ZdZdddddddee         ded	ed
ededdfdZddZ	de
ej        ej        f         fdZ	 ddededdfdZedefd            Zedefd            Zedefd            Zedefd            Zedefd            ZdS )
ThetaModelaS  
    The Theta forecasting model of Assimakopoulos and Nikolopoulos (2000)

    Parameters
    ----------
    endog : array_like, 1d
        The data to forecast.
    period : int, default None
        The period of the data that is used in the seasonality test and
        adjustment. If None then the period is determined from y's index,
        if available.
    deseasonalize : bool, default True
        A flag indicating whether the deseasonalize the data. If True and
        use_test is True, the data is only deseasonalized if the null of no
        seasonal component is rejected.
    use_test : bool, default True
        A flag indicating whether test the period-th autocorrelation. If this
        test rejects using a size of 10%, then decomposition is used. Set to
        False to skip the test.
    method : {"auto", "additive", "multiplicative"}, default "auto"
        The model used for the seasonal decomposition. "auto" uses a
        multiplicative if y is non-negative and all estimated seasonal
        components are positive. If either of these conditions is False,
        then it uses an additive decomposition.
    difference : bool, default False
        A flag indicating to difference the data before testing for
        seasonality.

    See Also
    --------
    statsmodels.tsa.statespace.exponential_smoothing.ExponentialSmoothing
        Exponential smoothing parameter estimation and forecasting
    statsmodels.tsa.statespace.sarimax.SARIMAX
        Seasonal ARIMA parameter estimation and forecasting

    Notes
    -----
    The Theta model forecasts the future as a weighted combination of two
    Theta lines.  This class supports combinations of models with two
    thetas: 0 and a user-specified choice (default 2). The forecasts are
    then

    .. math::

       \hat{X}_{T+h|T} = \frac{\theta-1}{\theta} b_0
                         \left[h - 1 + \frac{1}{\alpha}
                         - \frac{(1-\alpha)^T}{\alpha} \right]
                         + \tilde{X}_{T+h|T}

    where :math:`\tilde{X}_{T+h|T}` is the SES forecast of the endogenous
    variable using the parameter :math:`\alpha`. :math:`b_0` is the
    slope of a time trend line fitted to X using the terms 0, 1, ..., T-1.

    The model is estimated in steps:

    1. Test for seasonality
    2. Deseasonalize if seasonality detected
    3. Estimate :math:`\alpha` by fitting a SES model to the data and
       :math:`b_0` by OLS.
    4. Forecast the series
    5. Reseasonalize if the data was deseasonalized.

    The seasonality test examines where the autocorrelation at the
    seasonal period is different from zero. The seasonality is then
    removed using a seasonal decomposition with a multiplicative trend.
    If the seasonality estimate is non-positive then an additive trend
    is used instead. The default deseasonalizing method can be changed
    using the options.

    References
    ----------
    .. [1] Assimakopoulos, V., & Nikolopoulos, K. (2000). The theta model: a
       decomposition approach to forecasting. International Journal of
       Forecasting, 16(4), 521-530.
    .. [2] Hyndman, R. J., & Billah, B. (2003). Unmasking the Theta method.
       International Journal of Forecasting, 19(2), 287-290.
    .. [3] Fioruci, J. A., Pellegrini, T. R., Louzada, F., & Petropoulos, F.
       (2015). The optimized theta method. arXiv preprint arXiv:1503.03529.
    NTautoF)perioddeseasonalizeuse_testmethod
differencer   r    r!   r"   r#   r   c                   t          |dd          | _        t          |t          j                  r|j        d d df         | _        n|| _        t          |dd          | _        t          |d          | _
        t          |d	          o| j
        | _        t          |d
          | _        t          |dd          | _        | j        dk    r&| j                                        dk    rdnd| _        | j        f| j
        r_t!          |dd           }d }|$t!          |dd           }|t!          |dd           }|t#          |          | _        nt%          d          | j
        | _        d S )Nendog   )ndimr   r   T)optionalr    r!   r#   model)r   additivemultiplicativemuladd)optionsr   r,   r-   r   freqinferred_freqzkYou must specify a period or endog must be a pandas object with a DatetimeIndex with a freq not set to None)r   _y
isinstancepd	DataFrameiloc
endog_origr   _periodr	   _deseasonalize	_use_test_diffr   _methodmingetattrr   
ValueError_has_seasonality)	selfr%   r   r    r!   r"   r#   idxpfreqs	            r   __init__zThetaModel.__init__   s    UG!444eR\** 	$#jA.DOO#DO4@@@'GGh
++C0C 	 z<88
"H
 
 

 <6!!$(GKKMMA$5$5555DL<D$7%$//CEVT22=#C$??E -e44 -   !% 3r   c                    | j         }| j        rt          j        |          }t	          || j        d          }|j        d         }||d         dz  z  t          j        |d d         dz            z  }|dk    | _        d S )NT)nlagsfftr      gi@)	r1   r:   npdiffr   r7   shapesumr?   )r@   yrhonobsstats        r   _test_seasonalityzThetaModel._test_seasonality   s    G: 	

A!4<T222wqzc"gl"RVCHM%:%:: $'8 8r   c                 z   | j         }| j        s| j         t          j        d          fS t	          || j        | j                  }|j                                        dk    r<d| _        t	          |d| j                  }||j        z
  |j        d | j                 fS ||j        z  |j        d | j                 fS )Nr   )r)   r   r-   )	r1   r?   rI   emptyr   r;   r7   seasonalr<   )r@   rM   ress      r   _deseasonalize_datazThetaModel._deseasonalize_data   s    G$ 	(7BHQKK'' $,t|LLL<"" DL$QeDLIIICs|#S\.DL.%AAAs|#S\.DL.%AAAr   use_mledispThetaModelResultsc           	      \   | j         r| j        r|                                  |                                 \  }}|rt	          |dd          }|                    |          }t          j        |j                  }|d         dz   }|dk    r5d}|	                    d|dz
  i          }t          j        |j                  }|d         }	|d	         }
|
                    d          |	z
  }nt          |d
d          ddddf         }|dddfxx         dz  cc<   t          j                            ||d          d         \  }}	t          ||d         d                              |          }|j        d         }d}
|
                    d          }t          |	||
||||           S )a$  
        Estimate model parameters.

        Parameters
        ----------
        use_mle : bool, default False
            Estimate the parameters using MLE by fitting an ARIMA(0,1,1) with
            a drift.  If False (the default), estimates parameters using OLS
            of a constant and a time-trend and by fitting a SES to the model
            data.
        disp : bool, default True
            Display iterative output from fitting the model.

        Notes
        -----
        When using MLE, the parameters are estimated from the ARIMA(0,1,1)

        .. math::

           X_t = X_{t-1} + b_0 + (\alpha-1)\epsilon_{t-1} + \epsilon_t

        When estimating the model using 2-step estimation, the model
        parameters are estimated using the OLS regression

        .. math::

           X_t = a_0 + b_0 (t-1) + \eta_t

        and the SES

        .. math::

           \tilde{X}_{t+1} = \alpha X_{t} + (1-\alpha)\tilde{X}_{t}

        Returns
        -------
        ThetaModelResult
            Model results and forecasting
        r   r&   r&   cordertrendrX   r&   g<Nё\?zma.L1r   rG   ctT)prependNrH   )rcondknown)initial_levelinitialization_method)r8   r9   rQ   rV   r   fitrI   asarrayparamsfit_constrainedforecastr   linalglstsqr   rY   )r@   rW   rX   rM   rT   modrU   ri   alphab0sigma2one_stepra   _s                 r   rg   zThetaModel.fit   s   T  	%4> 	%""$$$..008 	'!9C888C''t'$$CZ
++F1IMEqyy))7EAI*>??CJ//BBZF||A+HH1dD111!!!RaR%8Bqqq!tHHHMHHHIOOBO66q9EAr&1W  ctcnn  JqMEF||AH vx7D
 
 	
r   c                     | j         S )z!Whether to deseasonalize the data)r8   r@   s    r   r    zThetaModel.deseasonalize  s     ""r   c                     | j         S )zThe period of the seasonality)r7   ru   s    r   r   zThetaModel.period       |r   c                     | j         S )z(Whether to test the data for seasonality)r9   ru   s    r   r!   zThetaModel.use_test  s     ~r   c                     | j         S )z7Whether the data is differenced in the seasonality test)r:   ru   s    r   r#   zThetaModel.difference  s     zr   c                     | j         S )z)The method used to deseasonalize the data)r;   ru   s    r   r"   zThetaModel.method   rw   r   )r   N)FF)__name__
__module____qualname____doc__r   intboolstrrC   rQ   tuplerI   ndarrayrV   rg   propertyr    r   r!   r#   r"    r   r   r   r   /   s       N Nh !%" ,4 ,4 ,4 	,4
 ,4 ,4 ,4 ,4 
,4 ,4 ,4 ,4\9 9 9 9BU2:rz+A%B B B B B 38E
 E
E
+/E
	E
 E
 E
 E
N #t # # # X#     X $    X D    X     X  r   r   c                   v   e Zd ZdZdededee         dedej        dede	d	d
fdZ
ed	ej        fd            Zed	efd            Zed	e	fd            Zddeded	ej        fdZdded	ej        fdZd	efdZ	 d dededed	ej        fdZ	 	 	 	 	 	 d!dededee         deded         deeef         d	dfdZd
S )"rY   a6  
    Results class from estimated Theta Models.

    Parameters
    ----------
    b0 : float
        The estimated trend slope.
    alpha : float
        The estimated SES parameter.
    sigma2 : float
        The estimated residual variance from the SES/IMA model.
    one_step : float
        The one-step forecast from the SES.
    seasonal : ndarray
        An array of estimated seasonal terms.
    use_mle : bool
        A flag indicating that the parameters were estimated using MLE.
    model : ThetaModel
        The model used to produce the results.
    rp   ro   rq   rr   rT   rW   r)   r   Nc                     || _         || _        || _        || _        |j        j        d         | _        || _        || _        || _	        d S )Nr   )
_b0_alpha_sigma2	_one_stepr6   rK   _nobs_model	_seasonal_use_mle)r@   rp   ro   rq   rr   rT   rW   r)   s           r   rC   zThetaModelResults.__init__<  sK     !%+A.
!r   c                 J    t          j        | j        | j        gddg          S )z The forecasting model parametersrp   ro   r   )r3   Seriesr   r   ru   s    r   ri   zThetaModelResults.paramsO  s&     y$(DK0wHHHHr   c                     | j         Vt          | j        j        dd          }|                    d          }t          j        |j                  d         | _         | j         J | j         S )zThe estimated residual varianceNr[   r\   r]   Fr`   rG   )r   r   r)   r1   rg   rI   rh   ri   )r@   rn   rU   s      r   rq   zThetaModelResults.sigma2T  se     <$*-yDDDC''u'%%C:cj11"5DL|'''|r   c                     | j         S )z%The model used to produce the results)r   ru   s    r   r)   zThetaModelResults.model^  s     {r   r&   rH   r   thetac                 &   t          |d          }|dk     rt          d          t          |d          }|dk     rt          d          dt          j        t          j                  j        z  }||k     r|dz
  |z  nd}|                     |          }||j        z  t          j	        |j
                  z   }| j        j        rCt          j	        |j                  }| j        j                            d	          r||z  }n||z  }d
|_        |S )a  
        Forecast the model for a given theta

        Parameters
        ----------
        steps : int
            The number of steps ahead to compute the forecast components.
        theta : float
            The theta value to use when computing the weight to combine
            the trend and the SES forecasts.

        Returns
        -------
        Series
            A Series containing the forecasts

        Notes
        -----
        The forecast is computed as

        .. math::

           \hat{X}_{T+h|T} = \frac{\theta-1}{\theta} b_0
                             \left[h - 1 + \frac{1}{\alpha}
                             - \frac{(1-\alpha)^T}{\alpha} \right]
                             + \tilde{X}_{T+h|T}

        where :math:`\tilde{X}_{T+h|T}` is the SES forecast of the endogenous
        variable using the parameter :math:`\alpha`. :math:`b_0` is the
        slope of a time trend line fitted to X using the terms 0, 1, ..., T-1.

        This expression follows from [1]_ and [2]_ when the combination
        weights are restricted to be (theta-1)/theta and 1/theta. This nests
        the original implementation when theta=2 and the two weights are both
        1/2.

        References
        ----------
        .. [1] Hyndman, R. J., & Billah, B. (2003). Unmasking the Theta method.
           International Journal of Forecasting, 19(2), 287-290.
        .. [2] Fioruci, J. A., Pellegrini, T. R., Louzada, F., & Petropoulos,
           F. (2015). The optimized theta method. arXiv preprint
           arXiv:1503.03529.
        r   r&    steps must be a positive integerr   ztheta must be a float >= 1g      @      ?)r   r,   rk   )r   r>   r
   rI   finfodoubleepsforecast_componentsr_   rh   sesr)   r    rT   r"   
startswithname)r@   r   r   threshtrend_weightcompfcastrT   s           r   rk   zThetaModelResults.forecastc  s   \ ((199?@@@5'**1999:::rx	**...3fnn	U**#''e'44tz)BJtx,@,@@:# 	"z$-00Hz ++E22 "!!
r   c                 Z   t          |d          }|dk     rt          d          | j        }| j        }| j        }t          j        d|dz   t
          j                  dz
  }|dk    r|d|z  d|z
  |z  |z  z
  z  }||z  }| j        t          j	        |          z  }| j
        j                            d          rt          j        |          }nt          j	        |          }| j
        j        rI| j        }	| j
        j        }
|t          j        |          z   }||
z  }|	j        d         r|	|         |dd<   t%          | j
        j        dd          }|*t)          j        d| j
        j        j        d                   }t-          ||          }t)          j        |||d	|
          }|S )a  
        Compute the three components of the Theta model forecast

        Parameters
        ----------
        steps : int
            The number of steps ahead to compute the forecast components.

        Returns
        -------
        DataFrame
            A DataFrame with three columns: trend, ses and seasonal containing
            the forecast values of each of the three components.

        Notes
        -----
        For a given value of :math:`\theta`, the deseasonalized forecast is
        `fcast = w * trend + ses` where :math:`w = \frac{theta - 1}{theta}`.
        The reseasonalized forecasts are then `seasonal * fcast` if the
        seasonality is multiplicative or `seasonal + fcast` if the seasonality
        is additive.
        r   r&   r   )dtyper   r-   Nr   )r_   r   rT   r   )r   r>   r   r   r   rI   arangefloat64r   onesr)   r"   r   zerosr    r   r   rK   r=   r6   r3   
RangeIndexr   r4   )r@   r   ro   rp   rO   hr_   r   seasonrT   r   oos_idxseasonal_locsr   dfs                  r   r   z%ThetaModelResults.forecast_components  s   . ((199?@@@XzIa"*5559199Uq5yT1E9::AQnrwu~~-:''.. 	$Xe__FFWU^^F:# 	4~HZ&FRYu---G#f,M~a  4$]3qqq	
-w===M!TZ%:%@%CDDEUE**\CV<<E
 
 
 	r   c                    | j         }t                      }t          |          j        }|dz   }| j        rdnd}t          |j        t          j                  }t          |j        dd          }|ret          |t          j
        t          j        f          r?|d                             d          g}|d|d	                             d          z   gz  }n.t          d          t          |j        j        d                   g}t          |j        d
d          pd}	d|	gfd|gfddd|d         gfd|d         gfg}
|j                            d          rdnd}dt          | j                  gfdt          |j                  gfg}|j        r8|                    d|gfdt          |j                  gfddgfddgfg           n|                    ddgfgdz             |                    | |
||           ddgdd}t-          j        | j                  dddf         }t3          |d d!gt5          | j        j                  d"|#          }|j                            |           |S )$a  
        Summarize the model

        Returns
        -------
        Summary
            This holds the summary table and text, which can be printed or
            converted to various output formats.

        See Also
        --------
        statsmodels.iolib.summary.Summary
        z ResultsMLEzOLS/SESr   Nr   z%m-%d-%Yz- rG   r   r%   zDep. Variable:zMethod:)zDate:N)zTime:NzSample: r&   r,   MultiplicativeAdditivezNo. Observations:zDeseasonalized:zDeseas. Method:zPeriod:   )gleftgrighttitlez%sz%#0.4gr)	data_fmtsdata_aligns
ParametersEstimatezParameter Estimates)r   txt_fmt)r)   r   typer{   r   r2   r6   r3   r   r=   DatetimeIndexPeriodIndexstrftimer   rK   r"   r   r   r    extendr   add_table_2colsrI   rh   ri   r   listr   tablesappend)r@   r)   smry
model_namer   r"   	is_seriesr   sampledep_nametop_left	top_right	table_fmtdatasts                  r   summaryzThetaModelResults.summary  s    
yy%[[)
Z'-6Yu/;;	('488 	>EB,<bn+MNN 	>Ah''
334FteBi00<<<==FF!ffc%"2"8";<<=F5+VW==Hz*!$&)
 !& 7 7 > >NJ 	 !3tz??"34U%8!9!9 :;
	
  
	/&1U\!2!2 34"J"J	    rB4j\A-...% 	 	
 	
 	
 $("23GG	z$+&&qqq$w/:&"##'
 
 
 	2r   皙?c                 \   | j         j        d         }dt          j        |          d|dz
  dz  z   z  z   | j        z  }t          j        |          }t          j                            |dz            }| 	                    ||          }t          j        |||z  z   ||| z  z   d          S )a/  
        Parameters
        ----------
        steps : int, default 1
            The number of steps ahead to compute the forecast components.
        theta : float, default 2
            The theta value to use when computing the weight to combine
            the trend and the SES forecasts.
        alpha : float, default 0.05
            Significance level for the confidence intervals.

        Returns
        -------
        DataFrame
            DataFrame with columns lower and upper

        Notes
        -----
        The variance of the h-step forecast is assumed to follow from the
        integrated Moving Average structure of the Theta model, and so is
        :math:`\sigma^2(1 + (h-1)(1 + (\alpha-1)^2)`. The prediction interval
        assumes that innovations are normally distributed.
        r&   rH   )lowerupper)ri   r5   rI   r   rq   sqrtr   normppfrk   r3   r4   )	r@   r   r   ro   model_alphasigma2_hsigma_hquantilepredictionss	            r   prediction_intervalsz&ThetaModelResults.prediction_intervals)  s    4 k&q)	%  AqQ(>$>??K '(##:>>%!),,mmE511|$w'99$w(':: 
 
 	
r   F	in_samplefigzmatplotlib.figure.Figurefigsizec                    ddl m}m}  |              |||          }|J |                     ||          }	|	j        }
|                    d          }| j        j        j        d         }t          j
        t          j        |                    }|rUt          | j        j        t          j                  r| j        j        j        }|                    || j        j                   |                    |
|	           |G|                     |||          }d|z
  dd}|                    |
|d         |d	         d
d|           |                    dd           |                    d           |S )a  
        Plot forecasts, prediction intervals and in-sample values

        Parameters
        ----------
        steps : int, default 1
            The number of steps ahead to compute the forecast components.
        theta : float, default 2
            The theta value to use when computing the weight to combine
            the trend and the SES forecasts.
        alpha : {float, None}, default 0.05
            The tail probability not covered by the confidence interval. Must
            be in (0, 1). Confidence interval is constructed assuming normally
            distributed shocks. If None, figure will not show the confidence
            interval.
        in_sample : bool, default False
            Flag indicating whether to include the in-sample period in the
            plot.
        fig : Figure, default None
            An existing figure handle. If not provided, a new figure is
            created.
        figsize: tuple[float, float], default None
            Tuple containing the figure size.

        Returns
        -------
        Figure
            Figure handle containing the plot.

        Notes
        -----
        The variance of the h-step forecast is assumed to follow from the
        integrated Moving Average structure of the Theta model, and so is
        :math:`\sigma^2(\alpha^2 + (h-1))`. The prediction interval assumes
        that innovations are normally distributed.
        r   )_import_mplcreate_mpl_figNo   r&   z.0%z confidence intervalr   r   grayg      ?)colorro   labelbestF)locframeonr   )pad)statsmodels.graphics.utilsr   r   rk   r   add_subplotr)   r6   rK   r3   IndexrI   r   r2   r   plotr   fill_betweenlegendtight_layout)r@   r   r   ro   r   r   r   r   r   r   
pred_indexaxrO   r   pir   s                   r   plot_predictzThetaModelResults.plot_predictQ  s   Z 	KJJJJJJJnS'**mmE511 &
__S!!z$*1-4)) 	2$*/;; 4
-3GGE4:0111

K(((**5%??B5y::::EOO77     			fe	,,,S!!!
r   )r&   rH   )r&   )r&   rH   r   )r&   rH   r   FNN)r{   r|   r}   r~   floatr   rI   r   r   r   rC   r   r3   r   ri   rq   r)   r   rk   r4   r   r   r   r   r   r   r   r   r   rY   rY   &  s[        *     	 
   *      
       & I	 I I I XI     X z    XA Ac Ae ABI A A A AF6 6 6R\ 6 6 6 6pI I I I IX @D&
 &
&
%*&
7<&
	&
 &
 &
 &
T !%48'+L LL L 	L
 L 01L ue|$L 
$L L L L L Lr   rY   )(r~   typingr   r   numpyrI   pandasr3   scipyr   statsmodels.iolib.summaryr   statsmodels.iolib.tabler   statsmodels.tools.validationr   r	   r
   r   r   statsmodels.tsa.deterministicr   statsmodels.tsa.seasonalr   0statsmodels.tsa.statespace.exponential_smoothingr   "statsmodels.tsa.statespace.sarimaxr   statsmodels.tsa.stattoolsr   statsmodels.tsa.tsatoolsr   r   matplotlib.figure
matplotlibr   r   r   r   rY   r   r   r   <module>r	     s    + * * * * * * *               - - - - - - / / / / / /              < ; ; ; ; ; 7 7 7 7 7 7      7 6 6 6 6 6 ) ) ) ) ) ) > > > > > > > > 9 9BH 9 9 9 9 9t t t t t t t tnw w w w w w w w w wr   