Python Code: Problem – numlib.daily_returns() - Define a function called numlib.daily_returns, which accepts a list of (
Posted: Sun May 15, 2022 8:42 am
Python Code:
Problem – numlib.daily_returns() - Define a function called
numlib.daily_returns, which accepts a list of (strictly) positive
floats as an argument, and returns a new list that which contains
the daily returns. Clearly, the returned list should have length
one less than the input list. If the input’s length is less than
two, you should return an empty list. Finally, your function should
not modify the input list in any way. For example, the function
call daily_returns([100.0, 80.0, 100.0, 110.0]) should evaluate to
[-0.2, 0.25, 0.10]. More specifically, the i-th return rate, ri, is
defined as ri = (pi+1 − pi)/pi, for all 0 ≤i < N− 1, where pi
(for 0 ≤i < N) is the i-th price value, and N is the length of
the input prices sequence.
Problem – numlib.daily_prices() - Define a function
numlib.daily_prices, which accepts to input arguments: a float
start, and a list of floats rates. The items of rates
represent
daily rates of return. The return value should be a new list which
contains the corresponding daily prices, beginning with start. To
be clear, the start value should always be included as the first
element of the returned list (which, therefore, should have length
len(rates)+1). Again, your function should not modify the input
list in any way. For example, the function call daily_prices(100.0,
[-0.2, 0.25, 0.10]) should evaluate to [100.0, 80.0, 100.0, 110.0].
More formally, the i-th price, pi is defined as pi = start if i = 0
pi−i(1+ ri−1) if 1 ≤i < N+ 1, where ri (for 0 ≤i < N) is the
i-th input rate, and N is the length of the input rates
sequence.
Remark: In general, daily_prices is the inverse operation of
daily_returns, in the sense that, for all possible non-empty lists
prices, the expression daily_prices(prices[0],
daily_returns(prices[1:])) should always evaluate to a list equal
to prices. Although this observation isn’t really relevant to how
you code the two functions, it may help clarify the specifications
for daily_prices.
Problem – numlib.daily_returns() - Define a function called
numlib.daily_returns, which accepts a list of (strictly) positive
floats as an argument, and returns a new list that which contains
the daily returns. Clearly, the returned list should have length
one less than the input list. If the input’s length is less than
two, you should return an empty list. Finally, your function should
not modify the input list in any way. For example, the function
call daily_returns([100.0, 80.0, 100.0, 110.0]) should evaluate to
[-0.2, 0.25, 0.10]. More specifically, the i-th return rate, ri, is
defined as ri = (pi+1 − pi)/pi, for all 0 ≤i < N− 1, where pi
(for 0 ≤i < N) is the i-th price value, and N is the length of
the input prices sequence.
Problem – numlib.daily_prices() - Define a function
numlib.daily_prices, which accepts to input arguments: a float
start, and a list of floats rates. The items of rates
represent
daily rates of return. The return value should be a new list which
contains the corresponding daily prices, beginning with start. To
be clear, the start value should always be included as the first
element of the returned list (which, therefore, should have length
len(rates)+1). Again, your function should not modify the input
list in any way. For example, the function call daily_prices(100.0,
[-0.2, 0.25, 0.10]) should evaluate to [100.0, 80.0, 100.0, 110.0].
More formally, the i-th price, pi is defined as pi = start if i = 0
pi−i(1+ ri−1) if 1 ≤i < N+ 1, where ri (for 0 ≤i < N) is the
i-th input rate, and N is the length of the input rates
sequence.
Remark: In general, daily_prices is the inverse operation of
daily_returns, in the sense that, for all possible non-empty lists
prices, the expression daily_prices(prices[0],
daily_returns(prices[1:])) should always evaluate to a list equal
to prices. Although this observation isn’t really relevant to how
you code the two functions, it may help clarify the specifications
for daily_prices.