McKinney Chapter 4 - Practice Blank

FINA 6333 for Spring 2024

Author

Richard Herron

1 Announcements

2 Practice

import numpy as np
%precision 4
'%.4f'

2.1 Create a 1-dimensional array named a1 that counts from 0 to 24 by 1.

2.2 Create a 1-dimentional array named a2 that counts from 0 to 24 by 3.

2.3 Create a 1-dimentional array named a3 that counts from 0 to 100 by multiples of 3 and 5.

2.4 Create a 1-dimensional array a3 that contains the squares of the even integers through 100,000.

How much faster is the NumPy version than the list comprehension version?

2.5 Write a function that mimic Excel’s pv function.

2.6 Write a function that mimic Excel’s fv function.

2.7 Replace the negative values in data with -1 and positive values with +1.

np.random.seed(42)
data = np.random.randn(7, 7)
data
array([[ 0.4967, -0.1383,  0.6477,  1.523 , -0.2342, -0.2341,  1.5792],
       [ 0.7674, -0.4695,  0.5426, -0.4634, -0.4657,  0.242 , -1.9133],
       [-1.7249, -0.5623, -1.0128,  0.3142, -0.908 , -1.4123,  1.4656],
       [-0.2258,  0.0675, -1.4247, -0.5444,  0.1109, -1.151 ,  0.3757],
       [-0.6006, -0.2917, -0.6017,  1.8523, -0.0135, -1.0577,  0.8225],
       [-1.2208,  0.2089, -1.9597, -1.3282,  0.1969,  0.7385,  0.1714],
       [-0.1156, -0.3011, -1.4785, -0.7198, -0.4606,  1.0571,  0.3436]])

2.8 Write a function npmts() that calculates the number of payments that generate \(x\%\) of the present value of a perpetuity.

Your npmts() should accept arguments c1, r, and g that represent \(C_1\), \(r\), and \(g\). The present value of a growing perpetuity is \(PV = \frac{C_1}{r - g}\), and the present value of a growing annuity is \(PV = \frac{C_1}{r - g}\left[ 1 - \left( \frac{1 + g}{1 + r} \right)^t \right]\).

2.9 Write a function that calculates the internal rate of return given a NumPy array of cash flows.

2.10 Write a function returns() that accepts NumPy arrays of prices and dividends and returns a NumPy array of returns.

prices = np.array([100, 150, 100, 50, 100, 150, 100, 150])
dividends = np.array([1, 1, 1, 1, 2, 2, 2, 2])

2.11 Rewrite the function returns() so it returns NumPy arrays of returns, capital gains yields, and dividend yields.

2.12 Rescale and shift numbers so that they cover the range [0, 1]

Input: np.array([18.5, 17.0, 18.0, 19.0, 18.0])
Output: np.array([0.75, 0.0, 0.5, 1.0, 0.5])

numbers = np.array([18.5, 17.0, 18.0, 19.0, 18.0])

2.13 Write functions var() and std() that calculate variance and standard deviation.

NumPy’s .var() and .std() methods return population statistics (i.e., denominators of \(n\)). The pandas equivalents return sample statistics (denominators of \(n-1\)), which are more appropriate for financial data analysis where we have a sample instead of a population.

Both function should have an argument sample that is True by default so both functions return sample statistics by default.

Use the output of returns() to compare your functions with NumPy’s .var() and .std() methods.