Python for Finance: Analyze Big Financial Data

(Elle) #1
In  [ 45 ]: print “%15s %15s”   %   (‘Mean’,    ‘Std.   Deviation’)
print 31 * “-”
for i in range( 1 , 31 , 2 ):
npr.seed( 1000 )
sn = npr.standard_normal(i ** 2 * 10000 )
print “%15.12f %15.12f” % (sn.mean(), sn.std())
Out[45]: Mean Std. Deviation
––––––––––-
-0.011870394558 1.008752430725
-0.002815667298 1.002729536352
-0.003847776704 1.000594044165
-0.003058113374 1.001086345326
-0.001685126538 1.001630849589
-0.001175212007 1.001347684642
-0.000803969036 1.000159081432
-0.000601970954 0.999506522127
-0.000147787693 0.999571756099
-0.000313035581 0.999646153704
-0.000178447061 0.999677277878
0.000096501709 0.999684346792
-0.000135677013 0.999823841902
-0.000015726986 0.999906493379
-0.000039368519 1.000063091949
In [ 46 ]: i ** 2 * 10000
Out[46]: 8410000

The results show that the statistics “somehow” get better the larger the number of draws


becomes. But they still do not match the desired ones, even in our largest sample with


more than 8,000,000 random numbers.


Fortunately, there are easy-to-implement, generic variance reduction techniques available


to improve the matching of the first two moments of the (standard) normal distribution.


The first technique is to use antithetic variates. This approach simply draws only half the


desired number of random draws, and adds the same set of random numbers with the


opposite sign afterward.


[ 38 ]

For example, if the random number generator (i.e., the


respective Python function) draws 0.5, then another number with value –0.5 is added to


the set.


With NumPy this is concisely implemented by using the function concatenate:


In  [ 47 ]: sn  =   npr.standard_normal( 10000  /    2 )
sn = np.concatenate((sn, -sn))
np.shape(sn)
Out[47]: (10000,)

The following repeats the exercise from before, this time using antithetic variates:


In  [ 48 ]: print “%15s %15s”   %   (‘Mean’,    ‘Std.   Deviation’)
print 31 * “-”
for i in range( 1 , 31 , 2 ):
npr.seed( 1000 )
sn = npr.standard_normal(i ** 2 * 10000 / 2 )
sn = np.concatenate((sn, -sn))
print “%15.12f %15.12f” % (sn.mean(), sn.std())
Out[48]: Mean Std. Deviation
––––––––––-
0.000000000000 1.009653753942
-0.000000000000 1.000413716783
0.000000000000 1.002925061201
-0.000000000000 1.000755212673
0.000000000000 1.001636910076
-0.000000000000 1.000726758438
-0.000000000000 1.001621265149
0.000000000000 1.001203722778
-0.000000000000 1.000556669784
0.000000000000 1.000113464185
-0.000000000000 0.999435175324
0.000000000000 0.999356961431
Free download pdf