Skip to content

Instantly share code, notes, and snippets.

Stuart Gordon Reid StuartGordonReid

View GitHub Profile
@StuartGordonReid
StuartGordonReid / AntNeighbourhoodFunction.py
Last active Aug 6, 2016
Ant Colony Neighbourhood Function
View AntNeighbourhoodFunction.py
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
"""
View AntColonyOptimizationGist.py
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 Sep 20, 2019
Measured of Risk-adjusted Return
View RiskAdjustedReturnMetrics.py
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 Jun 12, 2015
Volatility Risk Metrics
View Volatility.py
import numpy
import numpy.random as nrand
def vol(returns):
# Return the standard deviation of returns
return numpy.std(returns)
def beta(returns, market):
View ValueAtRisk.py
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 Jun 12, 2015
Lower and Higher Partial Moments
View PartialMoments.py
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
View MaximumDrawdown.py
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 Jun 12, 2015
Risk adjusted returns based on volatility
View VolatilityAdjustedReturns.py
"""
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 Jun 12, 2015
Risk adjusted returns based on Value at Risk
View VaRAdjustedReturns.py
"""
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 Jun 12, 2015
Risk adjusted returns based on Lower Partial Moments
View PartialMomentsAdjustedReturns.py
"""
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)
You can’t perform that action at this time.