Skip to content

Instantly share code, notes, and snippets.

View StuartGordonReid's full-sized avatar

Stuart Gordon Reid StuartGordonReid

View GitHub Profile
#' @title A more efficient estimator for the value of sigma. Sigma is the
#' standard deviation of the random component of returns in the Geometric
#' Brownian Motion model. This estimate can be calculated in an unbiased manner.
#'
#' @description Given a log price process and a parameter, q, which specifies
#' the sampling intervel this function estimates the value of Sigma. Sigma
#' represents the standarddeviation of the random disturbance component of
#' daily returns. This estimate can be annualized and can be computed in
#' a biased or unbiased manner.
#'
#' @title Estimator for the value of sigma. Sigma is the standard deviation of
#' the random component of returns in the Geometric Brownian Motion model. This
#' estimate can be calculated in a biased or unbiased manner.
#'
#' @description Given a log price process and a parameter, q, which specifies
#' the sampling intervel this function estimates the value of Sigma. Sigma
#' represents the standarddeviation of the random disturbance component of daily
#' returns. This estimate can be annualized and can be computed in a biased or
#' unbiased manner.
#'
#' @title Estimator for the value of mu. Mu is the drift component in the
#' Geometric Brownian Motion model.
#'
#' @description Given a log price process, this function estimates the value of
#' mu. Mu is the daily component of the returns which is attributable to upward,
#' or downward, drift. This estimate can be annualized.
#'
#' @param X vector :: A log price process.
#' @param annualize logical :: Annualize the parameter estimate.
#' @return mu.est double :: The estimated value of mu.
# Plot fifteen asset price paths without stochastic volatility.
charts.PerformanceSummary(returnProcesses(15, t = (252*5),
stochastic.volatility = FALSE),
colorset = seq(1,15))
# Plot fifteen asset price paths with stochastic volatility.
charts.PerformanceSummary(returnProcesses(15, t = (252*5),
stochastic.volatility = TRUE),
colorset = seq(1,15))
# Load the required packages.
library(PerformanceAnalytics)
library(xts)
#' @title Sample a random disturbance from either a normal distribution with a
#' constant standard deviation (Geometric Brownian Motion model) or from a
#' distribution with a stochastic standard deviation (Stochastic Volatility GBM)
#'
#' @description Given a long run random disturbance mean, mu, and a standard
@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 / RandomExcursions.py
Created September 13, 2015 13:12
Python implementation of the random excursions NIST cryptographic tests for randomness
def random_excursions(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 number of cycles having exactly K visits in a cumulative sum random walk. The
cumulative sum random walk is derived from partial sums after the (0,1) sequence is transferred to the
appropriate (-1, +1) sequence. A cycle of a random walk consists of a sequence of steps of unit length taken at
random that begin at and return to the origin. The purpose of this test is to determine if the number of visits
to a particular state within a cycle deviates from what one would expect for a random sequence. This test is
@StuartGordonReid
StuartGordonReid / CumulativeSums.py
Created September 13, 2015 13:10
Python implementation of the cumulative sums NIST cryptographic test for randomness
def cumulative_sums(self, bin_data: str, method="forward"):
"""
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 maximal excursion (from zero) of the random walk defined by the cumulative sum of
adjusted (-1, +1) digits in the sequence. The purpose of the test is to determine whether the cumulative sum of
the partial sequences occurring in the tested sequence is too large or too small relative to the expected
behavior of that cumulative sum for random sequences. This cumulative sum may be considered as a random walk.
For a random sequence, the excursions of the random walk should be near zero. For certain types of non-random
@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 / 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.