A Discrete-Time Stochastic Process Implementation
A stochastic process can be loosely defined as a set of random variables from some state space (and probability distribution) that are indexed by a set of values (such as a subset of the real line) called the index set. This is a very general idea that could be a good model for probabilistically triggering behavior in a program. While the analysis of stochastic processes is involved, the mechanics, and therefore program implementation, are relatively straightforward, at least in the simple case defined below.
In this example, the state space is defined as the possible return values of a pseudorandom number generator (such as the uniformly-distributed Mersenne Twister algorithm), and the index set as the readings of a monotonic clock on a certain frequency. At each tick of the clock, a number is generated and evaluated at some probability measure (this evaluation behaves like a cumulative distribution function), and either a success or failure event is interpreted by producing an indicator random variable represented by a boolean value, where true means 1 (success) and false means 0 (failure). The result is essentially a Bernoulli Process that can be used to signal events.
process(prob, interval, signal): clock = newClock(interval) clock.start() while clock.isRunning == true: t = clock.tick() if randNum() <= prob: signal(t) return
process
is called with an appropriately chosen probability measure
prob
, a unit of time interval
that determines the frequency of
clock
, and a signal handler function signal
to call when the
success event occurs. randNum
represents a pseudorandom number generator
like the one previously defined, which can be found in the standard libraries of many programming
languages. (Often, the range of possible values of randNum
is the interval [0, 1),
in which case prob
should also be in this interval. Different ranges could
require some form of normalization.) One could imagine changing process
to generate numbers from a different state space and distribution.
clock
is run and its tick
method blocks until interval
time has passed, at which point it unblocks and assigns to t
a value representing
some measurement of the current time. randNum
is then called, and its return value is
evaluated at prob
. In the case of the success event, signal
is called
with t
.
process
should execute concurrently so that it doesn't block the calling code.
signal
communicates with this client code, triggering some behavior. This
design could vary greatly depending on the implementation of concurrency of the programming
language.