Problem 1: Factorials using numpy
prod A numpy function that returns the product of all elements in an array
cumprod A numpy function on an ndarray of size n that returns an array of size n where each element i is the product of all elements from 0 to i
In [ ]:
“””
Replace pass with the code that uses ndarray, prod and cumprod
to calculate the factorial of n
The function should take an integer as an argument and return an integer
“””
def factorial(n):
pass
In [ ]:
“””
Replace pass with the code that uses ndarray, prod and cumprod to
calculate the factorials of all values from 1 to n
The function should take an integer as an argument and return an ndarray
of size n
“””
def all_factorials(n):
pass
Problem 2: Percent changes
Write a function that constructs an ndarray from data in a file and returns a 1-period percent change on an ndarray after removing any nan values. Your function must make use of the following functions:
np.genfromtxt: https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.genfromtxt.html
np.diff: https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.diff.html
np.isnan: https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.isnan.html
Test your function out using the attached apple.csv file. Your function can assume that the file structure is (date,price)
Note that percent change is defined as (p(t)-p(t-1))/p(t-1)
Example:
If the data array is: np.array([1,2,np.NaN,4,8,np.NAN])
The output array should be
array([ 1., 1., 1.])
In [6]:
def get_pct_changes(file_name):
pass
Problem 3: Pandas Grouping
Write a program that reads timeseries pricing data from a file into a pandas dataframe and then groups the data as follows:
Adds a column, one-day percent change to the file
Groups the data into four categories:
•
• “High+” if the percent change is greater than 2.5%
• “Low+” if the percent change is positive and less than or equal to 2.5%
• “Low-” if the percent change is negative but greater than or equal to -2.5%
• “High-” if the percent change is less than -2.5%
• Report the size and the mean of each of the groups
In [ ]:
datafile = “apple.csv”
import pandas as pd
df = pd.read_csv(‘apple.csv’,na_values=’null’)
In [ ]: