Return estimation
If we have price data, we have to calculate returns. In addition, sometimes we have to convert daily returns to weekly or monthly, or convert monthly returns to quarterly or annual. Thus, understanding how to estimate returns and their conversion is vital. Assume that we have four prices and we choose the first and last three prices as follows:
>>>import numpy as np >>>p=np.array([1,1.1,0.9,1.05])
It is important how these prices are sorted. If the first price happened before the second price, we know that the first return should be (1.1-1)/1=10%. Next, we learn how to retrieve the first n-1 and the last n-1 records from an n-record array. To list the first n-1 prices, we use p[:-1]
, while for the last three prices we use p[1:]
as shown in the following code:
>>>print(p[:-1]) >>>print(p[1:]) [ 1. 1.1 0.9] [ 1.1 0.9 1.05]
To estimate returns, use the following code:
>>>ret=(p[1:]-p[:-1])/p[:-1] >>>print ret [ 0...