Skip to content

Instantly share code, notes, and snippets.

View StuartGordonReid's full-sized avatar

Stuart Gordon Reid StuartGordonReid

View GitHub Profile
@StuartGordonReid
StuartGordonReid / AntNeighbourhoodFunction.py
Last active August 6, 2016 12:06
Ant Colony Neighbourhood Function
def get_probability(self, d, y, x, n, c):
"""
This gets the probability of drop / pickup for any given Datum, d
:param d: the datum
:param x: the x location of the datum / ant carrying datum
:param y: the y location of the datum / ant carrying datum
:param n: the size of the neighbourhood function
:param c: constant for convergence control
:return: the probability of
"""
import os
import math
import time
import numpy
import pandas
import random
import matplotlib
import numpy.random as nrand
import matplotlib.pylab as plt
from sklearn.preprocessing import normalize
@StuartGordonReid
StuartGordonReid / RiskAdjustedReturnMetrics.py
Last active June 9, 2024 23:00
Measured of Risk-adjusted Return
import math
import numpy
import numpy.random as nrand
"""
Note - for some of the metrics the absolute value is returns. This is because if the risk (loss) is higher we want to
discount the expected excess return from the portfolio by a higher amount. Therefore risk should be positive.
"""
@StuartGordonReid
StuartGordonReid / Volatility.py
Created June 12, 2015 11:33
Volatility Risk Metrics
import numpy
import numpy.random as nrand
def vol(returns):
# Return the standard deviation of returns
return numpy.std(returns)
def beta(returns, market):
@StuartGordonReid
StuartGordonReid / ValueAtRisk.py
Created June 12, 2015 11:35
Value at Risk Gist
import numpy
import numpy.random as nrand
"""
Note - for some of the metrics the absolute value is returns. This is because if the risk (loss) is higher we want to
discount the expected excess return from the portfolio by a higher amount. Therefore risk should be positive.
"""
@StuartGordonReid
StuartGordonReid / PartialMoments.py
Created June 12, 2015 11:37
Lower and Higher Partial Moments
import numpy
import numpy.random as nrand
def lpm(returns, threshold, order):
# This method returns a lower partial moment of the returns
# Create an array he same length as returns containing the minimum return threshold
threshold_array = numpy.empty(len(returns))
threshold_array.fill(threshold)
# Calculate the difference between the threshold and the returns
import numpy
import numpy.random as nrand
"""
Note - for some of the metrics the absolute value is returns. This is because if the risk (loss) is higher we want to
discount the expected excess return from the portfolio by a higher amount. Therefore risk should be positive.
"""
@StuartGordonReid
StuartGordonReid / VolatilityAdjustedReturns.py
Created June 12, 2015 13:38
Risk adjusted returns based on volatility
"""
Note that this Gist uses functions made available in another Gist -
https://gist.github.com/StuartGordonReid/67a1ec4fbc8a84c0e856
"""
def treynor_ratio(er, returns, market, rf):
return (er - rf) / beta(returns, market)
@StuartGordonReid
StuartGordonReid / VaRAdjustedReturns.py
Created June 12, 2015 13:46
Risk adjusted returns based on Value at Risk
"""
Note that this Gist uses functions made available in another Gist -
https://gist.github.com/StuartGordonReid/67a1ec4fbc8a84c0e856
"""
def excess_var(er, returns, rf, alpha):
return (er - rf) / var(returns, alpha)
@StuartGordonReid
StuartGordonReid / PartialMomentsAdjustedReturns.py
Created June 12, 2015 13:54
Risk adjusted returns based on Lower Partial Moments
"""
Note that this Gist uses functions made available in another Gist -
https://gist.github.com/StuartGordonReid/67a1ec4fbc8a84c0e856
"""
def omega_ratio(er, returns, rf, target=0):
return (er - rf) / lpm(returns, target, 1)