U
    ^»{j×'  ã                   @   sl  d dl mZmZmZmZ d dlT ddlmZ ddlm	Z	 d dl
Zd!dd	„Zd
d„ Zd"dd„Zdd„ Zd#dd„Zedk�rhd dlZdd„ Zeejƒdk r¦edƒ eƒ  nÂejd dkr¼eƒ  n¬ejd dkrÔedƒ n”ejd dkrêeƒ  n~ejd dk�r&eejƒdk �rdneejd ƒZeeƒ nBejd dk�r>eƒ  n*ejd dk�rZee	jƒ ned ƒ eƒ  dS )$é    )Úabsolute_importÚdivisionÚprint_functionÚunicode_literals)Ú*é   )Úmeasures)ÚdatasetsNé'  c              	      s€  ddl m} ddddddd	d
dg	}dddddddddg	}dd„ }|t |¡ƒ}|t |¡ƒ}g }tddƒD ]„}d| ‰ dt d|¡ ‰t ‡ fdd„t| ƒD ƒ¡}t ‡ ‡fdd„t| ƒD ƒ¡}t ‡ ‡fdd„t| ƒD ƒ¡}	| |||	f¡ qj| t dd¡|¡}
| t dd¡|¡}| t dd¡|¡}| 	|| |
 d¡ | 
t dd¡dt dd¡ ¡ | d ¡ | d!¡ | ¡  dS )"uÍ  
  Recreates figure 2 of [w]_ comparing the reported values by Weron to the
  values obtained by the functions in this package.

  The experiment consists of n iterations where the hurst exponent of randomly
  generated gaussian noise is calculated. This is done with differing sequence
  lengths of 256, 512, 1024, ...., 65536. The average estimated hurst exponent
  over all iterations is plotted for the following configurations:

  * ``weron`` is the Anis-Lloyd-corrected Hurst exponent calculated by Weron
  * ``rs50`` is the Anis-Lloyd-corrected Hurst exponent calculated by Nolds with
    the same parameters as used by Weron
  * ``weron_raw`` is the uncorrected Hurst exponent calculated by Weron
  * ``rs50_raw`` is the uncorrected Hurst exponent calculated by Nolds with the
    same parameters as used by Weron
  * ``rsn`` is the Anis-Lloyd-corrected Hurst exponent calculated by Nolds with
    the default settings of Nolds

  The values reported by Weron are only measured from the plot in the PDF
  version of the paper and can therefore have some small inaccuracies.

  This function requires the package ``matplotlib``.

  References:

  .. [w] R. Weron, â€œEstimating long-range dependence: finite sample
     properties and confidence intervals,â€� Physica A: Statistical Mechanics
     and its Applications, vol. 312, no. 1, pp. 285â€“299, 2002.

  Kwargs:
    n (int):
      number of iterations of the experiment (Weron used 10000, but this takes
      a while)
  r   Ng¢E¶óýÔ@gÛù~j¼4*@g=
×£p=4@g�•C‹ì5@g¨ÆK7‰A6@gé&1¬8@gçû©ñÒ8@g�•C‹L:@gƒÀÊ¡å:@gºI+d@gð§ÆK7µa@gJ+‡`@gsh‘í|ç\@g‹lçû©éY@gX9´ÈÞW@g¤p=
×³U@gB`åÐ"sT@gáz®GS@c                 S   s   d| d d  S )Ng\�Âõ(\ß?g¾Ÿ/Ýä=@ç{®Gáz„?© )Úheightr   r   úM/var/www/html/bwave/python/venv/lib/python3.8/site-packages/nolds/examples.pyÚheight_to_h6   s    z'weron_2002_figure2.<locals>.height_to_hé   é   é   é   c                    s$   g | ]}t jtjjˆ d �dd�‘qS )©ÚsizeÚpoly)Úfit©ÚnoldsÚhurst_rsÚnpÚrandomÚnormal©Ú.0Ú_)Úlr   r   Ú
<listcomp>>   s   ÿz&weron_2002_figure2.<locals>.<listcomp>c                    s&   g | ]}t jtjjˆ d �dˆd�‘qS )r   r   )r   Únvalsr   r   ©r!   r#   r   r   r"   B   s   ÿc                    s(   g | ] }t jtjjˆ d �dˆdd�‘qS )r   r   F)r   r#   Ú	correctedr   r   r$   r   r   r"   F   s   ÿ)ZweronZ	weron_rawÚrsnÚrs50Úrs50_rawzsequence lengthzestimated hurst exponent)Úmatplotlib.pyplotÚpyplotr   ÚarrayÚrangeÚarangeÚmeanÚappendÚplotÚlegendZxticksÚxlabelÚylabelÚshow)ÚnÚpltZreportedZreported_rawr   ÚdataÚer&   r'   r(   ÚlinesÚrZrrr   r$   r   Úweron_2002_figure2   sB    $ÿ ÿþþþ 

r;   c                  C   sL   ddl m}  dd„ tdƒD ƒ}| j|dd� |  d¡ |  d	¡ |  ¡  dS )
zŸ
  Plots a histogram of values obtained for the hurst exponent of uniformly
  distributed white noise.

  This function requires the package ``matplotlib``.
  r   Nc                 S   s$   g | ]}t jtjjd d�dd�‘qS )r
   r   T)r%   )r   r   r   r   r   r   r   r   r"   ]   s     z#plot_hurst_hist.<locals>.<listcomp>éd   é   )Zbinsz esimated value of hurst exponentznumber of experiments)r)   r*   r,   Úhistr2   r3   r4   )r6   Úhsr   r   r   Úplot_hurst_histT   s    

r@   Úlogisticc                    sð  ddl m} d‰d‰d}| dkrbd}t dd	d
¡}t ‡‡fdd„|D ƒ¡}dd„ t||ƒD ƒ}nh| dkr¾d}t ddd
¡}t ‡‡fdd„|D ƒ¡}tj||dkd�}tj|t |dk¡< nt	d|  ƒ‚dddœ‰ dddddœ‰‡ fdd„|D ƒ}‡fdd„|D ƒ}t 
||¡}	t |dd…| d…f ||jd  ¡}
| d|  ¡ |j||ddd� d}d}|j||d|d � |j||d!|d � | |t t|ƒ¡d"¡ |j|	|
d#dd$d%� | d&¡ | |¡ | d'| |f ¡ |jd(d)� | ¡  dS )*u*  
  Plots a bifurcation plot of the given map and superimposes the true
  lyapunov exponent as well as the estimates of the largest lyapunov exponent
  obtained by ``lyap_r`` and ``lyap_e``. The idea for this plot is taken from [ll]_.

  This function requires the package ``matplotlib``.

  References:

  .. [ll] Manfred FÃ¼llsack, "Lyapunov exponent",
     url: http://systems-sciences.uni-graz.at/etextbook/sw2/lyapunov.html

  Kwargs:
    maptype (str):
      can be either ``"logistic"`` for the logistic map or ``"tent"`` for the tent
      map.
  r   Ngš™™™™™¹?éŒ   é(   rA   r:   r   é   r   c                    s$   g | ]}t jt ˆˆ |¡d d�‘qS ©Zfloat32)Zdtype)r   Úfromiterr	   Zlogistic_map)r   r:   ©r5   Úx_startr   r   r"   ~   s   ÿzplot_lyap.<locals>.<listcomp>c                 S   s>   g | ]6\}}t  t  t|d | |t  |dk¡   ƒ¡¡‘qS )r   ç      à?)r   r.   ÚlogÚabsÚwhere)r   Úxr:   r   r   r   r"   Š   s   ÿÚtentz$\mu$c                    s$   g | ]}t jt ˆˆ |¡d d�‘qS rE   )r   rF   r	   Ztent_map)r   ÚmurG   r   r   r"   ‘   s   ÿ)rL   zmaptype %s not recognizedr   )Úemb_dimZ
matrix_dimr=   )rP   ZlagZmin_tsepZtrajectory_lenc                    s   g | ]}t tj|fˆ Žƒ‘qS r   )Úmaxr   Úlyap_e©r   Úd)Úkwargs_er   r   r"       s     c                    s   g | ]}t j|fˆ Ž‘qS r   )r   Zlyap_rrS   )Úkwargs_rr   r   r"   ¡   s     zLyapunov exponent of the %s mapzb-ztrue lyap. exponent)Úlabelzestimation using lyap_ezestimation using lyap_rz#00AAAA)ÚcolorrW   z#AA00AAzg--Úrozbifurcation plot)ÚalpharW   )éþÿÿÿr   zlyap. exp / %s(x, %s)Úbest)Úloc)r)   r*   r   r-   r+   ÚziprJ   ÚnanrL   ÚErrorÚrepeatZreshapeÚshapeÚtitler0   ZzerosÚlenZylimr2   r3   r1   r4   )Zmaptyper6   ZnbifurÚ
param_nameZparam_rangeZ	full_dataZlambdasZ	lambdas_eZ	lambdas_rZbifur_xZbifurZelabZrlabr   )rU   rV   r5   rH   r   Ú	plot_lyapc   sR    þþþ
(

rf   c                  C   s>   ddl } d}t tj |¡d ¡}|  ddtjid|i¡ dS )z‰
  Runs a profiling test for the function ``lyap_e`` (mainly used for development)

  This function requires the package ``cProfile``.
  r   Nr
   rI   zlyap_e(data)rR   r7   )ÚcProfiler   Zcumsumr   Zrunctxr   rR   )rg   r5   r7   r   r   r   Ú	profiling³   s    rh   c                 C   s
  ddl m} t | ¡} t dt| ƒd ¡}tj| |ddd�}tj| ddd�}t t 	|d d ¡¡ 
d	¡}|t t| ƒ|dd
…  dk¡ }tj| |ddd�}dd„ }	| |d d |d d |	|ƒ d¡}
| |d d |d d |	|ƒ d¡}| |d d |d d |	|ƒ d¡}g }g }|dk	�rltj| |ddd�}| |d d |d d |	|ƒ d¡}|}dg}| d¡ | d¡ | |
| | | dddg| ¡ t|d |d |d gdddgƒ}|D ]\} }td|| f ƒ �qÈ|dk	�rþtd|d  ƒ | ¡  dS )ay  
  Creates a plot that compares the results of different choices for nvals
  for the function hurst_rs.

  Args:
    data (array-like of float):
      the input data from which the hurst exponent should be estimated

  Kwargs:
    nvals (array of int):
      a manually selected value for the nvals parameter that should be plotted
      in comparison to the default choices
  r   Nr   r   Tr   )r#   Ú
debug_datar   )ri   r   Zint32éÿÿÿÿc                 S   s   dd„ | D ƒS )Nc                 S   s   g | ]}t  t |¡¡‘qS r   )r   rJ   r   Zexpected_rs)r   r5   r   r   r   r"   Õ   s     z5hurst_compare_nvals.<locals>.corr.<locals>.<listcomp>r   )r#   r   r   r   ÚcorrÔ   s    z!hurst_compare_nvals.<locals>.corrÚoZcustomzlog(n)zlog((R/S)_n - E[(R/S)_n])ÚallÚdefaultZdivisorsÚdefÚdivz%s: %.3fzcustom: %.3f)r)   r*   r   Zasarrayr-   rd   r   r   ÚroundÚexpZastyperL   r0   r2   r3   r1   r^   Úprintr4   )r7   r#   r6   Zn_allZdd_allZdd_defZn_defZn_divZdd_divrk   Zl_allZl_defZl_divZl_cstZt_cstZdd_cstZlabeled_datarW   r   r   r   Úhurst_compare_nvals¾   s8    
"&&&
&

""
rt   Ú__main__c                   C   s<   t dƒ t dƒ t dƒ t dƒ t dƒ t dƒ t dƒ d S )Nzoptions are:z  lyapunov-logisticz  lyapunov-tentz  profilingz  hurst-weron2z  hurst-histz  hurst-nvals)rs   r   r   r   r   Úprint_optionsñ   s    rv   r   z*please tell me which tests you want to runzlyapunov-logisticzlyapunov-tentrN   zhurst-weron2é   iè  z
hurst-histzhurst-nvalsz#i do not know any test of that name)r
   )rA   )N)Ú
__future__r   r   r   r   ÚbuiltinsÚ r   r   r	   Únumpyr   r;   r@   rf   rh   rt   Ú__name__Úsysrv   rd   Úargvrs   Úintr5   Zbrown72r   r   r   r   Ú<module>   s<   
I
P
/

"
