Question: python function using numpy Task 2.5: Computing Stringency Index Our fifth last task for this homework is to implement compute_stringency_index. This function takes stringency_values as

python function using numpy

Task 2.5: Computing Stringency Index

Our fifth last task for this homework is to implement compute_stringency_index. This function takes stringency_values as argument, and computes the daily stringency index for each country.

This function returns the daily stringency index for each country as a 2D np.ndarray such that the (,)(,) entry corresponds to the stringency index in the -th country on the (+1)(+1)-th day. In this case, the higher the stringency index, the more restrictive the measures are.

Recall that on each day, each country has four stringency values for school closing, workplace closing, stay at home requirements and international travel controls. In this case, we shall assume that 'stay at home requirements' is the most restrictive regulation among the other regulations; 'international travel controls' is more restrictive than 'school closing' and 'workplace closing'; and 'school closing' and 'workplace closing' are equally restrictive. Thus, to compute the stringency index, we shall weigh each stringency value by 1, 1, 3 and 2 for 'school closing', 'workplace closing', 'stay at home requirements' and 'international travel controls', respectively. Then, the stringency index for the th country on the (+1)(+1)th day is given by:

stringency_values[i, j, 0] + stringency_values[i, j, 1] + 3 * stringency_values[i, j, 2] + 2 * stringency_values[i, j, 3]

Note: Use the matrix multiplication operator (@ operator) to compute the stringency index. Please do not use iterative approaches like for-loops.

In [ ]:

def compute_stringency_index(stringency_values):
 '''
 Computes the daily stringency index for each country.
 Parameters
 ----------
 stringency_values: np.ndarray
 3D `ndarray` with each row representing the data of a country, and the columns
 of each row representing the time series data of the stringency values as a
 vector. To be specific, on each day, there are four different stringency
 values for 'school closing', 'workplace closing', 'stay at home requirements'
 and 'international travel controls', respectively. For instance, the (i, j, 0)
 entry represents the `school closing` stringency value for the ith country
 on the (j + 1)th day.
 
 Returns
 -------
 Daily stringency index for each country as a 2D `ndarray` such that the (i, j)
 entry corresponds to the stringency index in the ith country on the (j + 1)th
 day.
 In this case, we shall assume that 'stay at home requirements' is the most
 restrictive regulation among the other regulations, 'international travel
 controls' is more restrictive than 'school closing' and 'workplace closing',
 and 'school closing' and 'workplace closing' are equally restrictive. Thus,
 to compute the stringency index, we shall weigh each stringency value by 1,
 1, 3 and 2 for 'school closing', 'workplace closing', 'stay at home
 requirements' and 'international travel controls', respectively. Then, the 
 index for the ith country on the (j + 1)th day is given by
 `stringency_values[i, j, 0] + stringency_values[i, j, 1] +
 3 * stringency_values[i, j, 2] + 2 * stringency_values[i, j, 3]`.
 Note
 ----
 Use matrix operations and broadcasting to complete this question. Please do
 not use iterative approaches like for-loops.
 '''
 
 # TODO: add your solution here and remove `raise NotImplementedError`
 raise NotImplementedError

In [ ]:

 
# Test case for Task 2.5
stringency_values = np.ones((10, 20, 4))
stringency_values[:, 10:, :] *= 2
actual = compute_stringency_index(stringency_values)
expected = np.ones((10, 20)) * (1 + 1 + 3 + 2)
expected[:, 10:] *= 2
assert(np.all(actual == expected))
 
stringency_values2 = np.array([[[0, 0, 0, 0], [1, 0, 0, 0]], [[0, 0, 0, 0], [0, 1, 2, 0]]])
actual2 = compute_stringency_index(stringency_values2)
expected2 = np.array([[0, 1], [0, 7]])
assert(np.all(actual2 == expected2))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!