Chapter 6: Operations Research¶
Authors: Alice Paul and Susan Martonosi
Digital resources supplementing the chapter
Sample R code¶
R code for generating Figure 6.7 of the text
Figure 6.7 shows two images: 1. a histogram of 1000 randomly generated U(0,1) values overlaid with the density of that distribution 2. a histogram of X=-\frac{\ln(1-U)}{\lambda} with \lambda=5 created from the first distribution, with the exponential distribution (with \lambda=5) overlaid
R code for generating Table 6.1 of the text
Table 6.1 illustrates several different probability distributions, including * discrete uniform, * binomial, * geometric, * Poisson, * uniform, * triangle, * normal, and * exponential.
Each of those distributions can be accessed using code in a variety of languages, as shown below. The reader may also be interested in the individual documentation for probability distributions in R, MATLAB Python, or Julia.
Code reference for random number generation¶
In each example below, n is the number of values to generate.
All Python code assumes you have installed SciPy
and have imported its statistics package with import scipy.stats as stats
.
All Julia code assumes you have installed
Distributions.jl
and have imported it with using Distributions
.
Discrete uniform distribution on \{a,a+1,\ldots,b\}¶
In R:
sample( a:b, n, replace=TRUE )
In MATLAB:
randi( [a,b], n, 1 )
In Python:
stats.randint.rvs( a, b, size=n )
In Julia:
rand( a:b, n )
Binomial distribution with k trials each having probability p¶
In R:
rbinom( n, k, p )
In MATLAB:
random( "Binomial", k, p, n, 1 )
In Python:
stats.binom.rvs( k, p, size=n )
In Julia:
rand( Binomial( k, p ), n )
Geometric distribution with success probability p¶
(specifically, the number of failures observed before a success was observed)
In R:
rgeom( n, p )
In MATLAB:
random( "Geometric", p, n, 1 )
In Python:
stats.geom.rvs( p, size=n )
In Julia:
rand( Geometric( p ), n )
Poisson distribution with expected rate \lambda¶
In R:
rpois( n, lambda )
In MATLAB:
random( "Poisson", lambda, n, 1 )
In Python:
stats.poisson( lambda, size=n )
In Julia:
rand( Poisson( lambda ), n )
Uniform distribution on the interval [a,b]¶
In R:
runif( n, a, b )
In MATLAB:
random( "Uniform", a, b, n, 1 )
In Python:
stats.uniform.rvs( a, b, size=n )
In Julia:
rand( Uniform( a, b ), n )
Triangular distribution on the interval [a,b] with mode c¶
In R:
(This assumes you have done install.packages('triangle')
and
library('triangle')
.)
rtriangle( n, a, b, c )
In MATLAB this distribution is not built in.
In Python:
stats.triang.rvs( (c-a)/(b-a), a, b-a, size=n )
Distributions.js does not contain this distribution.
Normal distribution with mean \mu and standard deviation \sigma¶
In R:
rnorm( n, mu, sigma )
In MATLAB:
random( "Normal", mu, sigma, n, 1 )
In Python:
stats.norm.rvs( mu, sigma, size=n )
In Julia:
rand( Normal( mu, sigma ), n )
Exponential distribution with rate \lambda¶
In R:
rexp( n, lambda )
In MATLAB:
random( "Exponential", lambda, n, 1 )
In Python:
stats.expon.rvs( lambda, size=n )
In Julia:
rand( Exponential( lambda ), n )