import numpy as np
%precision 4'%.4f'
FINA 6333 for Spring 2024
import numpy as np
%precision 4'%.4f'
a1 that counts from 0 to 24 by 1.a2 that counts from 0 to 24 by 3.a3 that counts from 0 to 100 by multiples of 3 and 5.a3 that contains the squares of the even integers through 100,000.How much faster is the NumPy version than the list comprehension version?
pv function.fv function.data with -1 and positive values with +1.np.random.seed(42)
data = np.random.randn(7, 7)
dataarray([[ 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]])
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]\).
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])returns() so it returns NumPy arrays of returns, capital gains yields, and dividend yields.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])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.