Book Image

NumPy Cookbook - Second Edition

By : Ivan Idris
Book Image

NumPy Cookbook - Second Edition

By: Ivan Idris

Overview of this book

<p>NumPy has the ability to give you speed and high productivity. High performance calculations can be done easily with clean and efficient code, and it allows you to execute complex algebraic and mathematical computations in no time.</p> <p>This book will give you a solid foundation in NumPy arrays and universal functions. Starting with the installation and configuration of IPython, you'll learn about advanced indexing and array concepts along with commonly used yet effective functions. You will then cover practical concepts such as image processing, special arrays, and universal functions. You will also learn about plotting with Matplotlib and the related SciPy project with the help of examples. At the end of the book, you will study how to explore atmospheric pressure and its related techniques. By the time you finish this book, you'll be able to write clean and fast code with NumPy.</p>
Table of Contents (19 chapters)
NumPy Cookbook Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Estimating correlation of stock returns with pandas


A pandas DataFrame is a matrix and dictionary-like data structure similar to the functionality available in R. In fact, it is the central data structure in pandas, and you can apply all kinds of operations on it. It is quite common to take a look, for instance, at the correlation matrix of a portfolio, so let's do that.

How to do it...

First, we will create the DataFrame with pandas for each symbol's daily log returns. Then we will join these on the date. At the end, the correlation will be printed and a plot will appear:

  1. To create the data frame, create a dictionary containing stock symbols as keys and the corresponding log returns as values. The data frame itself has the date as the index and the stock symbols as column labels:

    data = {}
    
    for i, symbol in enumerate(symbols):
       data[symbol] = np.diff(np.log(close[i]))
    
    # Convention: import pandas as pd
    df = pd.DataFrame(data, index=dates[0][:-1], columns=symbols)

    We can now perform operations...