All problems
Log Returns
Problem
Given a price series, return the log returns r[t] = ln(prices[t] / prices[t-1]) for t = 1, ..., n-1.
Prices are assumed positive. A series with fewer than two prices yields an empty list.
Hint
Import math and divide consecutive prices before taking the log.
Test cases
case 1
in: [[100,110,121]]
expected: [0.09531017980432493,0.09531017980432493]
case 2
in: [[1,2.718281828459045]]
expected: [1]
case 3
in: [[5,5]]
expected: [0]
case 4
in: [[2]]
expected: []
Starter codePython
import math
def log_returns(prices):
# Your code here
pass4 test cases · run your code against them in the editor