Problems
Loading...
1 / 1

Percent Change

Time Series
Easy

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.

Algorithm

pct[i]=x[i]x[i1]x[i1]\text{pct}[i] = \frac{x[i] - x[i-1]}{x[i-1]}

The output has length n - 1.

Loading visualization...

Examples

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.

Hint 1

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.

Hint 2

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]).

Requirements

  • Compute the fractional change between each consecutive pair
  • Divide by the previous value, not the current value
  • If the previous value is zero, return 0.0 for that position
  • Return a list of floats of length n - 1

Constraints

  • series has at least 2 elements
  • Return a list of floats
  • Time limit: 300 ms
Try Similar Problems