forked from finanzascuantitativasfc/codigos_clase_2021-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01_simulate_rv.py
65 lines (54 loc) · 1.77 KB
/
01_simulate_rv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 1 09:22:02 2021
@author: Meva
"""
import numpy as np
import pandas as pd
import matplotlib as mpl
import scipy
import importlib
import matplotlib.pyplot as plt
from scipy.stats import skew, kurtosis, chi2
nb_sims = 10**6
df = 9
dist_name = 'chi-square' # student normal exponential uniform chi-square
dist_type = 'simulated RV' # real custom
if dist_name == 'normal':
x = np.random.standard_normal(nb_sims)
x_description = dist_type + ' ' + dist_name
elif dist_name == 'exponential':
x = np.random.standard_exponential(nb_sims)
x_description = dist_type + ' ' + dist_name
elif dist_name == 'uniform':
x = np.random.uniform(0,1,nb_sims)
x_description = dist_type + ' ' + dist_name
elif dist_name == 'student':
x = np.random.standard_t(df=df, size=nb_sims)
x_description = dist_type + ' ' + dist_name + ' | df = ' + str(df)
elif dist_name == 'chi-square':
x = np.random.chisquare(df=df, size=nb_sims)
x_description = dist_type + ' ' + dist_name + ' | df = ' + str(df)
'''
Goal: create a Jarque-Bera normality test
'''
x_mean = np.mean(x)
x_std = np.std(x)
x_skew = skew(x)
x_kurtosis = kurtosis(x) # excess kurtosis
x_jb_stat = nb_sims/6*(x_skew**2 + 1/4*x_kurtosis**2)
x_p_value = 1 - chi2.cdf(x_jb_stat, df=2)
x_is_normal = (x_p_value > 0.05) # equivalently jb < 6
print(x_description)
print('mean is ' + str(x_mean))
print('standard deviation is ' + str(x_std))
print('skewness is ' + str(x_skew))
print('kurtosis is ' + str(x_kurtosis))
print('JB statistic is ' + str(x_jb_stat))
print('p-value ' + str(x_p_value))
print('is normal ' + str(x_is_normal))
# plot histogram
plt.figure()
plt.hist(x,bins=100)
plt.title(x_description)
plt.show()