Skip to content

Instantly share code, notes, and snippets.

View StuartGordonReid's full-sized avatar

Stuart Gordon Reid StuartGordonReid

View GitHub Profile
@StuartGordonReid
StuartGordonReid / MonteCarloKMeansClustering.py
Created June 15, 2015 14:53
Monte Carlo K-Means Clustering
import math
import random
import csv
import numpy as np
import cProfile
import hashlib
memoization = {}
@StuartGordonReid
StuartGordonReid / DistanceMetrics.py
Created June 15, 2015 14:36
Implementation of various distance metrics in Python
import math
import random
import csv
import cProfile
import numpy as np
import hashlib
memoization = {}
@StuartGordonReid
StuartGordonReid / LongestRuns.py
Created August 25, 2015 15:54
Python implementation of the Longest Runs cryptographic test for randomness
def longest_runs(self, bin_data: str):
"""
Note that this description is taken from the NIST documentation [1]
[1] http://csrc.nist.gov/publications/nistpubs/800-22-rev1a/SP800-22rev1a.pdf
The focus of the tests is the longest run of ones within M-bit blocks. The purpose of this tests is to determine
whether the length of the longest run of ones within the tested sequences is consistent with the length of the
longest run of ones that would be expected in a random sequence. Note that an irregularity in the expected
length of the longest run of ones implies that there is also an irregularity ub tge expected length of the long
est run of zeroes. Therefore, only one test is necessary for this statistical tests of randomness
@StuartGordonReid
StuartGordonReid / BinaryMatrix.py
Created August 25, 2015 15:59
A Python class for computing the rank of a binary matrix. This is used by the Binary Matrix Rank cryptographic test for randomness
class BinaryMatrix:
def __init__(self, matrix, rows, cols):
"""
This class contains the algorithm specified in the NIST suite for computing the **binary rank** of a matrix.
:param matrix: the matrix we want to compute the rank for
:param rows: the number of rows
:param cols: the number of columns
:return: a BinaryMatrix object
"""
self.M = rows
@StuartGordonReid
StuartGordonReid / OrnsteinUhlenbeck.py
Created June 15, 2015 14:23
Ornstein Uhlenbeck Stochastic Process
import math
import numpy
import random
import decimal
import scipy.linalg
import numpy.random as nrand
import matplotlib.pyplot as plt
"""
Note that this Gist uses the Model Parameters class found here - https://gist.github.com/StuartGordonReid/f01f479c783dd40cc21e
@StuartGordonReid
StuartGordonReid / BlockFrequency.py
Created August 24, 2015 21:05
Python implementation of the Block Frequency cryptographic test for randomness
def block_frequency(self, bin_data: str, block_size=128):
"""
Note that this description is taken from the NIST documentation [1]
[1] http://csrc.nist.gov/publications/nistpubs/800-22-rev1a/SP800-22rev1a.pdf
The focus of this tests is the proportion of ones within M-bit blocks. The purpose of this tests is to determine
whether the frequency of ones in an M-bit block is approximately M/2, as would be expected under an assumption
of randomness. For block size M=1, this test degenerates to the monobit frequency test.
:param bin_data: a binary string
@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)
@StuartGordonReid
StuartGordonReid / DrawdownAdjustedReturns.py
Created June 12, 2015 13:58
Risk adjusted returns based on Drawdown risk
"""
Note that this Gist uses functions made available in another Gist -
https://gist.github.com/StuartGordonReid/67a1ec4fbc8a84c0e856
"""
def calmar_ratio(er, returns, rf):
return (er - rf) / max_dd(returns)
@StuartGordonReid
StuartGordonReid / Serial.py
Last active May 20, 2020 15:34
Python implementation of the Serial cryptographic test for randomness
def serial(self, bin_data, pattern_length=16, method="first"):
"""
Note that this description is taken from the NIST documentation [1]
[1] http://csrc.nist.gov/publications/nistpubs/800-22-rev1a/SP800-22rev1a.pdf
The focus of this test is the frequency of all possible overlapping m-bit patterns across the entire
sequence. The purpose of this test is to determine whether the number of occurrences of the 2m m-bit
overlapping patterns is approximately the same as would be expected for a random sequence. Random
sequences have uniformity; that is, every m-bit pattern has the same chance of appearing as every other
m-bit pattern. Note that for m = 1, the Serial test is equivalent to the Frequency test of Section 2.1.
@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):