Professional Documents
Culture Documents
Question 1: Write a function non_increasing_tst(ts, delta) that returns a Boolean value indicating
whether the time series is 'non-increasing', i.e., successive values never increase by more
delta: an integer ≥ 0 such that the positive change between two successive values in the time
series must be greater than delta for the change to be considered as an 'increase'.
The function should return True if there is no pair of consecutive values ts[i] and ts[i+1] in the
time series such that (ts[i+1] – ts[i]) > delta, or False otherwise.
You can assume that the input arguments are correctly formatted.
For example- you are given a time series in the form of a list of integers e.g., the daily Covid
case count leckietopia = [58, 54, 54, 52, 54, 50, 47]
The time series in leckietopia has a small increase from 52 to 54 However, if we tolerate noise of
at least 2 in the differences between successive readings, then this time series could be
False
False
True
True
Question 2: We might be interested in testing whether a time series contains a particular 'pattern',
such as two decreases in value in successive values (e.g., the definition of a recession), or
patient).
To do this we first need to a way to specify the pattern to search for. We will represent a pattern
as a string, which can contain the characters 'u' for up (or increase), 'd' for down (or
decrease), and 's' for steady (no change). For example, 'dd' represents two successive
Write a function contains_tst(ts, pattern, delta) that returns whether the given time series matches
pattern: a string of one or more characters, where the possible characters that can appear in
delta: an integer ≥ 0 such that the absolute value of the change between two successive values in
the time series must be less than or equal to delta for the two successive values to be
The function returns True if given pattern matches a sequence of consecutive values in ts and if
Assumptions:
You can assume that the input arguments are syntactically correct given the description on this
True
4 Questions Python
False
True
True
True
False
Sometimes we want to compare two time series to see how similar they are. Or given a set of
time series, we may want to compare a given example time series to each time series in
the set, and decide which one is most similar. To do this, we need a way of calculating a
distance measure between two time series. If two time series are identical, the 'distance'
between them should be zero. If two time series are very dissimilar, the 'distance'
We are given two time series ts1 and ts2, both of length n:
First, compute a new time series (we’ll call it diff) that is the absolute value of the differences of
...
Next, compute the mean of diff, i.e., the mean of the absolute values of the differences between
mean
n
4 Questions Python
diff
x
4 Questions Python
1
4 Questions Python
distance
abs
4 Questions Python
mean
Question 3: Write a function closest_tst(ts, ts_list) that given a time series ts, and a list of time
series ts_list, returns the index of the time series in ts_list that has the smallest distance to
ts_list: a list of one or more time series, where each time series in ts_list is a list of integers, and
The function returns an integer corresponding to index of the time series in ts_list that has the
smallest distance to ts (if there are multiple time series in ts_list that have the equal
smallest distance to ts, then return the index of the first such time series)
Assumptions
You can assume that the input arguments are correctly formatted and all time series have the
same length.
Notes: For 4
The final task is to detect whether a given time series contains an unusual subsequence of values
For example, the time series ts = [3, 0, 2, 40, 1] contains 4 subsequences of length w = 2,
specifically [3, 0], [0, 2], [2, 40], [40,1]. Likewise, this time series contains 3
subsequences of length w = 3, being [3, 0, 2], [0, 2, 40], [2, 40, 1].
Our aim is to find whether any of these subsequences is an anomaly, compared to the rest of the
time series. We can do this by calculating the “distance” of each subsequence to every
question, we will use Euclidean distance as our distance measure between a pair of
distance
1
4 Questions Python
]
4 Questions Python
3.6
4 Questions Python
Similarly, the distance of subsequence [3, 0] to subsequence [2, 40] is 40, and to subsequence
[40,1] is 37. Thus, the mean distance of subsequence [3, 0] to all other subsequences is
Likewise, the mean distance of subsequence [0, 2] to all other subsequences is 27.2; for [2, 40] it
is 44.2; and for [40, 1] it is 43.8. In this sequence, the subsequence [2, 40] has the greatest
If threshold for deciding whether a subsequence is an anomaly is given as 40, then this
subsequence will be reported as an anomaly, i.e., it has the highest mean distance, and
Question 4: Write a function anomaly_tst(ts, w, threshold) that given a time series ts, a subsequence
length w, and an anomaly cut-off threshold threshold , returns the index of the subsequence in ts that
has the highest mean distance above the threshold (or -1 if no subsequence has a mean distance above
the threshold). If there is more than one subsequence that has the same mean distance that is the max
and above the threshold, then return the index of the first such time series.
threshold: a integer > 0, where the mean distance of a subsequence must be greater than this threshold
if it is to be considered as an anomaly.
Assumptions:
4 Questions Python
You can assume that the input arguments are correctly formatted.
-1
-1