import numpy as npMcKinney Chapter 4 - Practice - Blank
FINA 6333 for Spring 2025
%precision 4'%.4f'
Announcements
Five-Minute Review
Practice
Create a 1-dimensional array a1 that counts from 0 to 24 by 1.
Create a 1-dimentional array a2 that counts from 0 to 24 by 3.
Create a 1-dimentional array a3 that counts from 0 to 100 by multiples of 3 or 5.
Create a 1-dimensional array a4 that contains the squares of the even integers through 100,000.
Write a function calc_pv() that mimic Excel’s PV function.
Excel’s present value function is: =PV(rate, nper, pmt, [fv], [type])
The present value of an annuity payment is: \(PV_{pmt} = \frac{pmt}{rate} \times \left(1 - \frac{1}{(1+rate)^{nper}} \right)\)
The present value of a lump sum is: \(PV_{fv} = \frac{fv}{(1+rate)^{nper}}\)
Write a function calc_fv() that mimic Excel’s FV function.
Excel’s future value function is: =FV(rate, nper, pmt, [pv], [type])
Replace the negative values in data with -1 and positive values with +1.
np.random.seed(42)
data = np.random.randn(4, 4)Write a function calc_n() that calculates the number of payments that generate x% of the present value of a perpetuity.
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]\).
Write a function that calculates the internal rate of return of a NumPy array of cash flows.
Write a function calc_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])Rewrite the function calc_returns() as calc_returns_2() so it returns NumPy arrays of returns, capital gains yields, and dividend yields.
Write a function rescale() to 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])
Write functions calc_var() and calc_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.
Your calc_var() and calc_std() functions should have a sample argument that is True by default so both functions return sample statistics by default.
Write a function calc_ret() to convert quantitative returns to qualitative returns
Returns within one standard deviation of the mean are “Medium”. Returns less than one standard deviation below the mean are “Low”, and returns greater than one standard deviation above the mean are “High”.
np.random.seed(42)
returns = np.random.randn(100)