a
    B>9dL  ã                   @   sÜ   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
 d dlmZ d dlmZ d dlZej d¡ d dlmZ d dlm  mZ d dlmZ d dlmZ d dlm Z m!Z! d d	l"m#Z# d
d„ Z$dd„ Z%ddd„Z&dS )é    N)Úmean_absolute_error)Úmean_squared_error)Ú	plot_pacf)Úsqrtzseaborn-darkgrid)Úadfuller)ÚacfÚpacf)ÚARIMAc                 C   sz  t | |ƒ}td| ƒ t| |ƒ}td| ƒ tt| |ƒƒ}td| ƒ d| | |   ¡  ¡  }td| ƒ | | }| j}tjdddd	\}\}	}
}| j	|	d
d |j	|	dd |	 
d¡ |	 d¡ |
j|j|jdd |
 
d¡ |
 d¡ |
 d¡ |
j|ddd |
j t d¡¡ |
j t d¡¡ tjdd}tjjj||dd | d¡ | 
d¡ | d¡ t ¡  t ¡  dS )zÍThis function will print the Mean Absolute Error, Mean Squared Error,
    Root Mean Squared Error and Mean Absolute Percentage Error.
    This function will also display the residula plot and the ACF.
    zThe Mean Absolute Error is %.2fzThe Mean Squared Error is %.2fz#The Root Mean Squared Error is %.2féd   z*The Mean Absolute Percentage Error is %.2fé   é   )é
   é   ©ÚfigsizeÚpurple)ÚaxÚcolorÚgreenZPricezPredicted vs. observedÚred©r   ÚErrorÚDateÚ	Residualsé-   Úright©ÚrotationÚhaú%d-%m-%Y)r   ÚblueZLagszPartial autocorrelationz$Partial autocorrelation of residualsN)r   Úprintr   r   ÚabsÚmeanÚindexÚpltÚsubplotsÚplotÚ
set_ylabelÚ	set_titleÚfill_betweenÚvaluesÚ
set_xlabelÚset_xticklabelsÚxaxisÚset_major_formatterÚmdatesÚDateFormatterÚset_minor_formatterÚxticksÚsmÚgraphicsÚtsar   Útight_layoutÚshow)ÚobservedÚ	predictedÚmaeÚmseÚrmseZmapeÚ	residualsÚlabelsÚfigÚax1Úax2Zax3Ú_© rD   ú:C:\Users\nburg\Desktop\EPAT\ASQ02 Code NB\tsa_functions.pyÚmodel_performance   s:    	









rF   c           
      C   s°  |   ¡ |  ¡  t d¡ }| d  ¡ }tj | ¡ ¡}d||dk < d|| d  }| ¡ }t	d| ƒ t	d|d d d  d ƒ t	d| d ƒ | j
}tjd	dd
d\}\}}	|j|dd |jddd |jddd |jddd |j|ddd |j t d¡¡ |j t d¡¡ |	j|dd |	j|j
|jdd |	jddd |	jddd |	jddd |	j|ddd |	j t d¡¡ |	j t d¡¡ t ¡  t ¡  d S )Néü   r   r
   zThe Sharpe Ratio is %.2fzThe cumulative return is %.2féÿÿÿÿú%zThe maximum drawdown is %.2fé   )r   é   r   Úbr   zCumulative returnsé   )Úfontsizeé   r   r   r   r   r   r   zStrategy drawdownzDrawdown (%))r#   ÚstdÚnpr   ÚcumprodÚmaximumÚ
accumulateÚdropnaÚminr!   r$   r%   r&   r'   r)   r(   r,   r-   r.   r/   r0   r1   r2   r*   r+   r7   r8   )
Zstrategy_returnsZsharpe_ratioZcumulative_returnsZrunning_maxZdrawdownZmax_ddr?   r@   rA   rB   rD   rD   rE   Úanalyze_strategyQ   s@    ÿÿrW   é   rG   é(   ©é   rO   c                 C   sÆ  | j |d ¡ }| j |d ¡ }| j |d ¡ }| j |d ¡ }tjdd}	tjdd d}
t |
d¡}t |
d¡}t |
d	¡}t |
d
¡}| j|d |j|d |j|d |j|d |j|d | 	d¡ |j
d|› d|› dgdd | 	d¡ |j
|› d|› dgdd tjjj| ||dd tjjj| ||dd tdƒ tdƒ tdƒ t| dd}tj|dd… g d¢d}|d  ¡ D ] \}}d |¡|d|› d< qt|ƒ tdƒ d S )!z‰ Checks the stationarity of a pandas Series (default is daily prices or returns),
        using plots, correlograms and the ADF test
    )Úwindow)r[   rM   r   r   )Ú
font_scale)rJ   rJ   )r   r   )r   r   )r   r   )r   r   )r   zRolling means over timer9   z-period MA of observedÚbest)ÚloczRolling volatility over timez-period MA of volatilitygš™™™™™©?)Úlagsr   Úalphaz>--------------------------------------------------------------z>--------- The augmented Dickey-Fuller test results -----------ÚAIC)Úautolagr   é   )zTest Statisticzp-valuez	# of Lagsz# of Observations)r$   z{0:.3f}zCritical Value (ú)N)Úrollingr#   rP   r%   ÚfigureÚsnsÚsetÚsubplot2gridr'   r)   Úlegendr4   r5   r6   Úplot_acfr   r!   r   ÚpdÚSeriesÚitemsÚformat)ÚyZwl1Zwl2r`   r   Zrolling_wl1_meanZrolling_wl2_meanZrolling_wl1_volZrolling_wl2_volr@   ÚlayoutZy_axZvol_axZacf_axZpacf_axZadftestÚresultsÚkeyÚvaluerD   rD   rE   Úcheck_stationarity…   s>    
 
rv   )rX   rG   rY   rZ   )'ÚnumpyrQ   Úpandasrm   Zsklearn.metricsr   r   Ústatsmodels.graphics.tsaplotsr   Úmathr   Úmatplotlib.pyplotÚpyplotr%   Úmatplotlib.datesÚdatesr0   Úseabornrh   ÚstyleÚuseÚstatsmodels.apiÚapir4   Zstatsmodels.formula.apiÚformulaÚsmfÚstatsmodels.statsÚstatsÚsmsÚstatsmodels.tsa.stattoolsr   r   r   Ústatsmodels.tsa.arima.modelr	   rF   rW   rv   rD   rD   rD   rE   Ú<module>   s$   :4