Skip to main content

Lesson 16

Boolean Selection

This lesson covers:

  • Boolean selection
  • where

Begin by loading the data in momentum.csv.

In [ ]:
# Setup: Load the momentum data

import numpy as np
import pandas as pd

momentum = pd.read_csv("data/momentum.csv", index_col="date", parse_dates=True)

print(momentum.head())

Problem: Selecting rows with boolean conditions

Select the rows in momentum where all returns on a day are negative.

In [ ]:
 

Problem: Selecting rows

Select the rows in momentum where 50% or more of the returns on a day are negative.

In [ ]:
 

Problem: Selecting columns

Select the columns in momentum what have the smallest and second smallest average returns.

In [ ]:
 
In [ ]:
 
In [ ]:
 

Problem: Selecting rows and columns

Select the returns for the column with the single most negative return on days where all of the returns are negative.

In [ ]:
 
In [ ]:
 

Problem: Selecting Elements using Logical Statements

For portfolio 1 and portfolio 10 compute the correlation when both returns are negative and when both are positive.

In [ ]:
 
In [ ]:
 
In [ ]:
# Setup: Reproducible random numbers

rs = np.random.RandomState(19991231)
x = rs.randint(1, 11, size=(10,3))
x

Problem: Select the columns of x that means >= $E[x]$

In [ ]:
 

Problem: Select the rows of x that means >= $E[x]$

In [ ]:
 

Problem: Select the rows and column of x where both have means < $E[x]$

In [ ]:
 

Problem: Using where

Use where to select the index of the elements in portfolio 5 that are negative. Next, use the where command in its two output form to determine which elements of the portfolio return matrix are less than -2%.

In [ ]:
 
In [ ]:
 
In [ ]:
 

Exercises

Exercise: Select the Most Volatile Portfolio

Select the column in momentum that has the highest standard deviation.

In [ ]:
 

Exercise: Select the High Kurtosis Portfolios

Select the columns that have kurtoses above the median kurtosis.

In [ ]:
 

Exercise: Select

Select the rows where all of the returns in the row are less than the 25% quantile for their portfolio.

Note: Comparisons between DataFrames and Series works like mathematical operations (+, -, etc.).

In [ ]:
 
In [ ]: