Percent change (also called rate of return) measures the relative change between consecutive observations in a time series. It is fundamental in finance for computing stock returns, and in data science for normalizing series of different scales so they can be compared. Converting absolute values to percent changes also helps achieve stationarity.
Given a time series, compute the fractional change between each consecutive pair of values. If the previous value is zero, return 0.0 for that position.
The output has length n - 1.
Input:
series = [100, 110, 105]
Output:
[0.1, -0.04545]
(110-100)/100 = 0.1 (10% increase). (105-110)/110 = -0.04545 (4.5% decrease).
Input:
series = [50, 100, 200]
Output:
[1.0, 1.0]
Each value doubles the previous one: (100-50)/50 = 1.0 and (200-100)/100 = 1.0, both representing 100% increases.
Loop from index 1 to len(series)-1. For each i, compute (series[i] - series[i-1]) / series[i-1]. Check if series[i-1] is 0 first to avoid division by zero.
The output list has one fewer element than the input. Make sure you divide by the PREVIOUS value (series[i-1]), not the current value (series[i]).
Sign in to take notes on this problem
Accepts: array
Percent change (also called rate of return) measures the relative change between consecutive observations in a time series. It is fundamental in finance for computing stock returns, and in data science for normalizing series of different scales so they can be compared. Converting absolute values to percent changes also helps achieve stationarity.
Given a time series, compute the fractional change between each consecutive pair of values. If the previous value is zero, return 0.0 for that position.
The output has length n - 1.
Input:
series = [100, 110, 105]
Output:
[0.1, -0.04545]
(110-100)/100 = 0.1 (10% increase). (105-110)/110 = -0.04545 (4.5% decrease).
Input:
series = [50, 100, 200]
Output:
[1.0, 1.0]
Each value doubles the previous one: (100-50)/50 = 1.0 and (200-100)/100 = 1.0, both representing 100% increases.
Loop from index 1 to len(series)-1. For each i, compute (series[i] - series[i-1]) / series[i-1]. Check if series[i-1] is 0 first to avoid division by zero.
The output list has one fewer element than the input. Make sure you divide by the PREVIOUS value (series[i-1]), not the current value (series[i]).
Sign in to take notes on this problem
Accepts: array