Skip to content

Commit 178030d

Browse files
committed
fix doctest in distributions.py
1 parent 471c329 commit 178030d

File tree

1 file changed

+84
-96
lines changed

1 file changed

+84
-96
lines changed

src/statista/distributions.py

Lines changed: 84 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ def pdf(
814814
- Calculate PDF values with default parameters:
815815
```python
816816
>>> gumbel_dist = Gumbel(data)
817-
>>> gumbel_dist.fit_model() # doctest: +SKIP
817+
>>> gumbel_dist.fit_model()
818818
-----KS Test--------
819819
Statistic = 0.019
820820
Accept Hypothesis
@@ -895,27 +895,27 @@ def random(
895895
- Analyze the generated data:
896896
- Plot the PDF of the random data:
897897
```python
898-
>>> gumbel_dist.pdf(data=random_data, plot_figure=True, xlabel="Random data")
898+
>>> gumbel_dist.pdf(data=random_data, plot_figure=True, xlabel="Random data") #doctest: +SKIP
899899
900900
```
901901
![gamma-pdf](./../_images/distributions/gamma-random-1.png)
902902
903903
- Plot the CDF of the random data:
904904
```python
905-
>>> gumbel_dist.cdf(data=random_data, plot_figure=True, xlabel="Random data")
905+
>>> gumbel_dist.cdf(data=random_data, plot_figure=True, xlabel="Random data") #doctest: +SKIP
906906
907907
```
908908
![gamma-cdf](./../_images/distributions/gamma-cdf-1.png)
909909
910910
- Verify the parameters by fitting the model to the random data
911911
```python
912912
>>> gumbel_dist = Gumbel(data=random_data)
913-
>>> fitted_params = gumbel_dist.fit_model()
913+
>>> fitted_params = gumbel_dist.fit_model() # doctest: +SKIP
914914
-----KS Test--------
915915
Statistic = 0.018
916916
Accept Hypothesis
917917
P value = 0.9969602438295625
918-
>>> print(f"Fitted parameters: {fitted_params}")
918+
>>> print(f"Fitted parameters: {fitted_params}") # doctest: +SKIP
919919
Fitted parameters: {'loc': np.float64(-0.010212105435018243), 'scale': 1.010287499893525}
920920
921921
```
@@ -1021,7 +1021,7 @@ def cdf(
10211021
- Calculate CDF values with default parameters:
10221022
```python
10231023
>>> gumbel_dist = Gumbel(data)
1024-
>>> gumbel_dist.fit_model() # doctest: +SKIP
1024+
>>> gumbel_dist.fit_model()
10251025
-----KS Test--------
10261026
Statistic = 0.019
10271027
Accept Hypothesis
@@ -1115,6 +1115,7 @@ def return_period(
11151115
>>> return_level_100yr = gumbel_dist.inverse_cdf([cdf_value], parameters={"loc": 0, "scale": 1})[0]
11161116
>>> print(f"100-year return level: {return_level_100yr}")
11171117
100-year return level: 4.600149226776579
1118+
11181119
```
11191120
"""
11201121
if data is None:
@@ -1401,7 +1402,7 @@ def inverse_cdf(
14011402
```python
14021403
>>> cdf = [0.1, 0.2, 0.4, 0.6, 0.8, 0.9]
14031404
>>> data_values = gumbel_dist.inverse_cdf(cdf)
1404-
>>> print(data_values)
1405+
>>> print(data_values) #doctest: +SKIP
14051406
[-0.83403245 -0.475885 0.08742157 0.67172699 1.49993999 2.25036733]
14061407
14071408
```
@@ -1558,18 +1559,8 @@ def chisquare(self) -> tuple:
15581559
Accept Hypothesis
15591560
P value = 0.9937026761524456
15601561
{'loc': np.float64(0.010101355750222706), 'scale': 1.0313042643102108}
1561-
>>> chi2_stat, p_value = gumbel_dist.chisquare()
1562+
>>> gumbel_dist.chisquare() #doctest: +SKIP
15621563
1563-
```
1564-
- Interpret the results:
1565-
```python
1566-
>>> alpha = 0.05
1567-
>>> if p_value < alpha:
1568-
... print(f"Reject the null hypothesis (p-value: {p_value:.4f} < {alpha})")
1569-
... print("The data does not follow the fitted Gumbel distribution.")
1570-
>>> else:
1571-
... print(f"Cannot reject the null hypothesis (p-value: {p_value:.4f} >= {alpha})")
1572-
... print("The data may follow the fitted Gumbel distribution.")
15731564
```
15741565
"""
15751566
return super().chisquare()
@@ -1978,7 +1969,7 @@ def pdf(
19781969
>>> data = np.loadtxt("examples/data/gev.txt")
19791970
>>> parameters = {"loc": 0, "scale": 1, "shape": 0.1}
19801971
>>> gev_dist = GEV(data, parameters)
1981-
>>> gev_dist.pdf(plot_figure=True)
1972+
>>> gev_dist.pdf(plot_figure=True) #doctest: +SKIP
19821973
19831974
```
19841975
![gev-random-pdf](./../_images/gev-random-pdf.png)
@@ -2025,12 +2016,12 @@ def random(
20252016
```
20262017
- then we can use the `pdf` method to plot the pdf of the random data.
20272018
```python
2028-
>>> gev_dist.pdf(data=random_data, plot_figure=True, xlabel="Random data")
2019+
>>> gev_dist.pdf(data=random_data, plot_figure=True, xlabel="Random data") #doctest: +SKIP
20292020
20302021
```
20312022
![gev-random-pdf](./../_images/gev-random-pdf.png)
20322023
```
2033-
>>> gev_dist.cdf(data=random_data, plot_figure=True, xlabel="Random data")
2024+
>>> gev_dist.cdf(data=random_data, plot_figure=True, xlabel="Random data") #doctest: +SKIP
20342025
20352026
```
20362027
![gev-random-cdf](./../_images/gev-random-cdf.png)
@@ -2129,7 +2120,7 @@ def cdf(
21292120
>>> data = np.loadtxt("examples/data/gev.txt")
21302121
>>> parameters = {"loc": 0, "scale": 1, "shape": 0.1}
21312122
>>> gev_dist = GEV(data, parameters)
2132-
>>> gev_dist.cdf(plot_figure=True)
2123+
>>> gev_dist.cdf(plot_figure=True) #doctest: +SKIP
21332124
21342125
```
21352126
![gev-random-cdf](./../_images/gev-random-cdf.png)
@@ -2228,7 +2219,7 @@ def fit_model(
22282219
Statistic = 0.06
22292220
Accept Hypothesis
22302221
P value = 0.9942356257694902
2231-
>>> print(parameters)
2222+
>>> print(parameters) #doctest: +SKIP
22322223
{'loc': -0.05962776672431072, 'scale': 0.9114319092295455, 'shape': 0.03492066094614391}
22332224
22342225
```
@@ -2239,7 +2230,7 @@ def fit_model(
22392230
Statistic = 0.05
22402231
Accept Hypothesis
22412232
P value = 0.9996892272702655
2242-
>>> print(parameters)
2233+
>>> print(parameters) # doctest: +SKIP
22432234
{'loc': -0.07182150513604696, 'scale': 0.9153288314267931, 'shape': 0.018944589308927475}
22442235
22452236
```
@@ -2551,7 +2542,7 @@ def plot(
25512542
```
25522543
- to calculate the confidence interval, we need to provide the confidence level (`alpha`).
25532544
```python
2554-
>>> fig, ax = gumbel_dist.plot()
2545+
>>> fig, ax = gev_dist.plot()
25552546
>>> print(fig)
25562547
Figure(1000x500)
25572548
>>> print(ax)
@@ -3012,49 +3003,48 @@ def pdf(
30123003
30133004
Returns the value of Gumbel's pdf with parameters loc and scale at x.
30143005
3015-
Parameters
3016-
----------
3017-
parameters: Dict[str, str], optional, default is None.
3018-
if not provided, the parameters provided in the class initialization will be used.
3019-
{"loc": val, "scale": val}
3006+
Args:
3007+
parameters (Dict[str, str], optional):
3008+
if not provided, the parameters provided in the class initialization will be used. default is None
3009+
{"loc": val, "scale": val}
30203010
3021-
- loc: [numeric]
3022-
location parameter of the gumbel distribution.
3023-
- scale: [numeric]
3024-
scale parameter of the gumbel distribution.
3025-
data: np.ndarray, default is None.
3026-
array if you want to calculate the pdf for different data than the time series given to the constructor
3027-
method.
3028-
plot_figure: [bool]
3029-
Default is False.
3030-
kwargs:
3031-
fig_size: [tuple]
3032-
Default is (6, 5).
3033-
xlabel: [str]
3034-
Default is "Actual data".
3035-
ylabel: [str]
3036-
Default is "pdf".
3037-
fontsize: [int]
3038-
Default is 15
3011+
- loc (numeric):
3012+
location parameter of the gumbel distribution.
3013+
- scale (numeric):
3014+
scale parameter of the gumbel distribution.
3015+
data (np.ndarray):
3016+
array if you want to calculate the pdf for different data than the time series given to the
3017+
constructor. default is None.
3018+
method.
3019+
plot_figure (bool):
3020+
Default is False.
3021+
kwargs:
3022+
fig_size (tuple):
3023+
Default is (6, 5).
3024+
xlabel (str):
3025+
Default is "Actual data".
3026+
ylabel (str):
3027+
Default is "pdf".
3028+
fontsize (int):
3029+
Default is 15
30393030
3040-
Returns
3041-
-------
3042-
pdf: [array]
3043-
probability density function pdf.
3044-
fig: matplotlib.figure.Figure, if `plot_figure` is True.
3045-
Figure object.
3046-
ax: matplotlib.axes.Axes, if `plot_figure` is True.
3047-
Axes object.
3031+
Returns:
3032+
pdf(array):
3033+
probability density function pdf.
3034+
fig (matplotlib.figure.Figure):
3035+
Figure object. if `plot_figure` is True.
3036+
ax (matplotlib.axes.Axes):
3037+
Axes object. if `plot_figure` is True.
30483038
3049-
Examples
3050-
--------
3051-
>>> data = np.loadtxt("examples/data/expo.txt")
3052-
>>> parameters = {'loc': 0, 'scale': 2}
3053-
>>> expo_dist = Exponential(data, parameters)
3054-
>>> expo_dist.pdf(plot_figure=True)
3039+
Examples:
3040+
```python
3041+
>>> data = np.loadtxt("examples/data/expo.txt")
3042+
>>> parameters = {'loc': 0, 'scale': 2}
3043+
>>> expo_dist = Exponential(data, parameters)
3044+
>>> expo_dist.pdf(plot_figure=True) # doctest: +SKIP
30553045
3056-
.. image:: /_images/expo-random-pdf.png
3057-
:align: center
3046+
```
3047+
![expo-random-pdf](./../_images/expo-random-pdf.png)
30583048
"""
30593049
result = super().pdf(
30603050
parameters=parameters,
@@ -3073,42 +3063,41 @@ def random(
30733063
) -> Union[Tuple[np.ndarray, Figure, Any], np.ndarray]:
30743064
"""Generate Random Variable.
30753065
3076-
Parameters
3077-
----------
3078-
size: int
3079-
size of the random generated sample.
3080-
parameters: Dict[str, str]
3081-
{"loc": val, "scale": val}
3082-
3083-
- loc: [numeric]
3084-
location parameter of the gumbel distribution.
3085-
- scale: [numeric]
3086-
scale parameter of the gumbel distribution.
3087-
3088-
Returns
3089-
-------
3090-
data: [np.ndarray]
3091-
random generated data.
3066+
Args:
3067+
size: int
3068+
size of the random generated sample.
3069+
parameters: Dict[str, str]
3070+
{"loc": val, "scale": val}
30923071
3093-
Examples
3094-
--------
3095-
- To generate a random sample that follow the gumbel distribution with the parameters loc=0 and scale=1.
3072+
- loc: [numeric]
3073+
location parameter of the gumbel distribution.
3074+
- scale: [numeric]
3075+
scale parameter of the gumbel distribution.
30963076
3097-
>>> parameters = {'loc': 0, 'scale': 2}
3098-
>>> expon_dist = Exponential(parameters=parameters)
3099-
>>> random_data = expon_dist.random(1000)
3077+
Returns:
3078+
data ([np.ndarray]):
3079+
random generated data.
31003080
3101-
- then we can use the `pdf` method to plot the pdf of the random data.
3081+
Examples:
3082+
- To generate a random sample that follow the gumbel distribution with the parameters loc=0 and scale=1.
3083+
```python
3084+
>>> parameters = {'loc': 0, 'scale': 2}
3085+
>>> expon_dist = Exponential(parameters=parameters)
3086+
>>> random_data = expon_dist.random(1000)
31023087
3103-
>>> expon_dist.pdf(data=random_data, plot_figure=True, xlabel="Random data")
3088+
```
3089+
- then we can use the `pdf` method to plot the pdf of the random data.
3090+
```python
3091+
>>> expon_dist.pdf(data=random_data, plot_figure=True, xlabel="Random data") #doctest: +SKIP
31043092
3105-
.. image:: /_images/expo-random-pdf.png
3106-
:align: center
3093+
```
3094+
![expo-random-pdf](./../_images/expo-random-pdf.png)
31073095
3108-
>>> expon_dist.cdf(data=random_data, plot_figure=True, xlabel="Random data")
3096+
```python
3097+
>>> expon_dist.cdf(data=random_data, plot_figure=True, xlabel="Random data") #doctest: +SKIP
31093098
3110-
.. image:: /_images/expo-random-cdf.png
3111-
:align: center
3099+
```
3100+
![expo-random-cdf](./../_images/expo-random-cdf.png)
31123101
"""
31133102
# if no parameters are provided, take the parameters provided in the class initialization.
31143103
if parameters is None:
@@ -3260,8 +3249,7 @@ def fit_model(
32603249
Statistic = 0.019
32613250
Accept Hypothesis
32623251
P value = 0.9937026761524456
3263-
Out[14]: {'loc': 0.0009, 'scale': 2.0498075}
3264-
>>> print(parameters)
3252+
>>> print(parameters) # doctest: +SKIP
32653253
{'loc': 0, 'scale': 2}
32663254
32673255
- You can also use the `lmoments` method to estimate the distribution parameters.
@@ -3271,7 +3259,7 @@ def fit_model(
32713259
Statistic = 0.021
32723260
Accept Hypothesis
32733261
P value = 0.9802627322900355
3274-
>>> print(parameters)
3262+
>>> print(parameters) #doctest: +SKIP
32753263
{'loc': -0.00805012182182141, 'scale': 2.0587576218218215}
32763264
"""
32773265
# obj_func = lambda p, x: (-np.log(Gumbel.pdf(x, p[0], p[1]))).sum()

0 commit comments

Comments
 (0)