In the exercise, it was calculated using linear regression. For triples, how can I calculate HL?
Thanks,
Bruno
For triplets, you can compute the spread using the hedge ratio. You can find the hedge ratio for triplet using the Johansen test method. You can more details on forming spread for triplet here: https://quantra.quantinsti.com/startCourseDetails?cid=55§ion_no=3&unit_no=18#course_type=paid&unit_type=ZipFiles
Half life computation for triplet
Once, you have the spread computed for the triplet, then rest of the code to compute the half life covered here remains the same.
Hi Ishan,
I am actually doing this course but cannot understand how to compute hedge ratio of a triplet spread from the explanation given here. Are you able to expand on that?
Thanks
Sumit
The eigen vectors are the hedge ratios which are used to compute the spread. The eigen vectors can be found using Johansen test
The steps are
1. Call the coint_johasen to run the Johansen test and store the output of test in the result.
result = coint_johansen(df[:90], 0, 1)
2. We are interested in the eigen vectors to form the spread. This can be accessed using the evec property or result object.
ev = result.evec
print(ev)
output:
array([[ 0.53497407, -0.4363222 , 0.27592177], [-0.73867001, -0.10072507, -0.25778167], [ 0.0293279 , 0.14875475, -0.25864276]])
3. The rows 0.53, -0.73 and 0.02 are the hedge ratio. This can be extracted as shown below
ev = result.evec.T[0]
print(ev)
output
array([ 0.53497407, -0.73867001, 0.0293279 ])
4. We will divide the above array with 0.53 to normalize or make the first value as 1.
# Normalise the eigenvectors by dividing the row with the first value of eigenvector
ev = ev/ev[0]
Output: array([ 1. , -1.38075851, 0.05482116])
5. For the spread using this eigen vector or hedge ratio
# Print the mean reverting spread
print("\nSpread = {}.GLD + ({}).GDX + ({}).USO".format(ev[0], ev[1], ev[2]))
Output:
Spread = 1.0.GLD + (-1.3807585120691197).GDX + (0.05482115507237646).USO
Thank you Ishan. I am trying to write the code in the Jupyter notebook and I am getting this error –
duleNotFoundError Traceback (most recent call last) <ipython-input-16-77e5cf636314> in <module>() 3 import numpy as np 4 import matplotlib.pyplot as plt ----> 5 from johansen import coint_johansen ModuleNotFoundError: No module named 'johansen'
Is there something I am not doing right?
Thanks
You need to download the zip file at the end of the course. There you will find all codes and notebook. In the triplet section there is a file name johansen.py. Copy paste that file in the same folder as the notebook which is throwing the error. This should help resolve the error.
The error indicates that the johansen.py (module) is not found. Python looks for file in default path and in same directory. So placing the file in same directory should solve the problem.
I hope this helps.