This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import random | |
import csv | |
import numpy as np | |
import cProfile | |
import hashlib | |
memoization = {} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import random | |
import csv | |
import cProfile | |
import numpy as np | |
import hashlib | |
memoization = {} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy | |
import numpy.random as nrand | |
def vol(returns): | |
# Return the standard deviation of returns | |
return numpy.std(returns) | |
def beta(returns, market): |