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 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 |
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 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. | |
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 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 |
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 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. |
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 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): | |
""" |
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
# 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 |
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 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. | |
""" | |
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 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 |
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 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. |
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 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 |
NewerOlder