Book Image

Python for Finance

By : Yuxing Yan
Book Image

Python for Finance

By: Yuxing Yan

Overview of this book

Table of Contents (20 chapters)
Python for Finance
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Generating random numbers from a standard normal distribution


Normal distributions play a central role in finance. A major reason is that many finance theories, such as option theory and applications, are based on the assumption that stock returns follow a normal distribution. It is quite often that we need to generate n random numbers from a standard normal distribution. For this purpose, we have the following two lines of code:

>>>import scipy as sp
>>>x=sp.random.standard_normal(size=10)

The basic random numbers in SciPy/NumPy are created by Mersenne Twister PRNG in the numpy.random function. The random numbers for distributions in numpy.random are in cython/pyrex and are pretty fast. To print the first few observations, we use the print() function as follows:

>>>print x[0:5]
[-0.55062594 -0.51338547 -0.04208367 -0.66432268  0.49461661]
>>>

Alternatively, we could use the following code:

>>>import scipy as sp
>>>x=sp.random.normal...