Skip to content

Instantly share code, notes, and snippets.

View StuartGordonReid's full-sized avatar

Stuart Gordon Reid StuartGordonReid

View GitHub Profile
@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
@StuartGordonReid
StuartGordonReid / Monobit.py
Last active April 7, 2024 14:44
Python implementation of the Monobit test for randomness
def monobit(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 this test is the proportion of zeros and ones for the entire sequence. The purpose of this test is
to determine whether the number of ones and zeros in a sequence are approximately the same as would be expected
for a truly random sequence. This test assesses the closeness of the fraction of ones to 1/2, that is the number
of ones and zeros ina sequence should be about the same. All subsequent tests depend on this test.
@StuartGordonReid
StuartGordonReid / ApproximateEntropy.py
Last active April 7, 2024 11:19
Python implementation of the Approximate Entropy cryptographic test for randomness
def approximate_entropy(self, bin_data: str, pattern_length=10):
"""
Note that this description is taken from the NIST documentation [1]
[1] http://csrc.nist.gov/publications/nistpubs/800-22-rev1a/SP800-22rev1a.pdf
As with the Serial test of Section 2.11, the focus of this test is the frequency of all possible overlapping
m-bit patterns across the entire sequence. The purpose of the test is to compare the frequency of overlapping
blocks of two consecutive/adjacent lengths (m and m+1) against the expected result for a random sequence.
:param bin_data: a binary string
@StuartGordonReid
StuartGordonReid / NonOverlappingPatterns.py
Created August 25, 2015 16:01
Python implementation of the Non Overlapping Patterns cryptographic test for randomness
def non_overlapping_patterns(self, bin_data: str, pattern="000000001", num_blocks=8):
"""
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 number of occurrences of pre-specified target strings. The purpose of this
test is to detect generators that produce too many occurrences of a given non-periodic (aperiodic) pattern.
For this test and for the Overlapping Template Matching test of Section 2.8, an m-bit window is used to
search for a specific m-bit pattern. If the pattern is not found, the window slides one bit position. If the
pattern is found, the window is reset to the bit after the found pattern, and the search resumes.
@StuartGordonReid
StuartGordonReid / ClusteringObjectiveFunctions.py
Created June 15, 2015 14:43
Clustering Objective Functions
class ClusteringQuality:
"""
Instances of this class implement the two measures of clustering quality discussed in the article, namely the davies
bouldin index and the silhouette index. It also implements a number of useful helper methods.
:param solution: the clustering solution of type Clustering
:param minimum: the minimum distance allowable
"""
def __init__(self, solution, minimum):
"""
@StuartGordonReid
StuartGordonReid / BasicPSO.py
Created June 23, 2015 11:16
Python Particle Swarm Optimization
# Portfolio optimization using particle swarm optimization article - PSO bare bones code
import random
w = 0.729844 # Inertia weight to prevent velocities becoming too large
c1 = 1.496180 # Scaling co-efficient on the social component
c2 = 1.496180 # Scaling co-efficient on the cognitive component
dimension = 20 # Size of the problem
iterations = 3000
swarmSize = 30
@StuartGordonReid
StuartGordonReid / RiskAdjustedReturnMetrics.py
Last active June 13, 2023 06:40
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 / LinearComplexity.py
Last active June 2, 2023 03:10
Python implementation of the linear complexity cryptographic test for randomness. This includes the Berklekamp Massey algorithm.
def linear_complexity(self, bin_data, block_size=500):
"""
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 length of a linear feedback shift register (LFSR). The purpose of this test is to
determine whether or not the sequence is complex enough to be considered random. Random sequences are
characterized by longer LFSRs. An LFSR that is too short implies non-randomness.
:param bin_data: a binary string
@StuartGordonReid
StuartGordonReid / RandomExcursionsVariant.py
Created September 13, 2015 13:13
Python implementation of the NIST random excursions variant cryptographic test for randomness
def random_excursions_variant(self, bin_data):
"""
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 total number of times that a particular state is visited (i.e., occurs) in a
cumulative sum random walk. The purpose of this test is to detect deviations from the expected number of visits
to various states in the random walk. This test is actually a series of eighteen tests (and conclusions), one
test and conclusion for each of the states: -9, -8, …, -1 and +1, +2, …, +9.
@StuartGordonReid
StuartGordonReid / BinaryMatrixRank.py
Created August 25, 2015 15:57
Python implementation of the Binary Matrix Rank cryptographic test for randomness
def matrix_rank(self, bin_data: str, q=32):
"""
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 test is the rank of disjoint sub-matrices of the entire sequence. The purpose of this test is
to check for linear dependence among fixed length sub strings of the original sequence. Note that this test
also appears in the DIEHARD battery of tests.
:param bin_data: a binary string