Python for Finance: Analyze Big Financial Data

(Elle) #1
plt.grid(True)
plt.xlabel(‘theoretical quantiles’)
plt.ylabel(‘sample quantiles’)

Figure 11-3. Quantile-quantile plot for log returns

However appealing the graphical approaches might be, they generally cannot replace more


rigorous testing procedures. The function normality_tests combines three different


statistical tests:


Skewness test (skewtest)


This tests whether the skew of the sample data is “normal” (i.e., has a value close


enough to zero).


Kurtosis test (kurtosistest)


Similarly, this tests whether the kurtosis of the sample data is “normal” (again, close


enough to zero).


Normality test (normaltest)


This combines the other two test approaches to test for normality.


We define this function as follows:


In  [ 13 ]: def normality_tests(arr):
”’ Tests for normality distribution of given data set.

                                                    Parameters
==========
array: ndarray
object to generate statistics on
”’
print “Skew of data set %14.3f” % scs.skew(arr)
print “Skew test p-value %14.3f” % scs.skewtest(arr)[ 1 ]
print “Kurt of data set %14.3f” % scs.kurtosis(arr)
print “Kurt test p-value %14.3f” % scs.kurtosistest(arr)[ 1 ]
print “Norm test p-value %14.3f” % scs.normaltest(arr)[ 1 ]

The test values indicate that the log returns are indeed normally distributed — i.e., they


show p-values of 0.05 or above:


In  [ 14 ]: normality_tests(log_returns.flatten())
Out[14]: Skew of data set 0.001
Skew test p-value 0.430
Kurt of data set 0.001
Kurt test p-value 0.541
Norm test p-value 0.607
Free download pdf