Exercises
2.2.1 Add to Gaussian (PROGRAM 2.1.2) an implementation of the three-argument static method pdf(x, mu, sigma) specified in the API that computes the Gaussian probability density function with a given mean μ and standard deviation σ, based on the formula ϕ(x, μ, σ) = ϕ(( x– μ) / σ)/σ. Also add an implementation of the associated cumulative distribution function cdf(z, mu, sigma), based on the formula Φ(z, μ, σ) = Φ((z – μ) / σ).
2.2.2 Write a library of static methods that implements the hyperbolic functions based on the definitions sinh(x) = (ex – e–x) / 2 and cosh(x) = (ex + e–x) / 2, with tanh(x), coth(x), sech(x), and csch(x) defined in a manner analogous to standard trigonometric functions.
2.2.3 Write a test client for both StdStats and StdRandom that checks that the methods in both libraries operate as expected. Take a command-line argument n, generate n random numbers using each of the methods in StdRandom, and print their statistics. Extra credit: Defend the results that you get by comparing them to those that are to be expected from analysis.
2.2.4 Add to StdRandom a method shuffle() that takes an array of double values as argument and rearranges them in random order. Implement a test client that checks that each permutation of the array is produced about the same number of times. Add overloaded methods that take arrays of integers and strings.
2.2.5 Develop a client that does stress testing for StdRandom. Pay particular attention to discrete(). For example, do the probabilities sum to 1?
2.2.6 Write a static method that takes double values ymin and ymax (with ymin strictly less than ymax), and a double array a[] as arguments and uses the StdStats library to linearly scale the values in a[] so that they are all between ymin and ymax.
2.2.7 Write a Gaussian and StdStats client that explores the effects of changing the mean and standard deviation for the Gaussian probability density function. Create one plot with the Gaussian distributions having a fixed mean and various standard deviations and another with Gaussian distributions having a fixed standard deviation and various means.
2.2.8 Add a method exp() to StdRandom that takes an argument λ and returns a random number drawn from the exponential distribution with rate λ. Hint: If x is a random number uniformly distributed between 0 and 1, then –ln x / λ is a random number from the exponential distribution with rate λ.
2.2.9 Add to StdRandom a static method maxwellBoltzmann() that returns a random value drawn from a Maxwell–Boltzmann distribution with parameter σ. To produce such a value, return the square root of the sum of the squares of three random numbers drawn from the Gaussian distribution with mean 0 and standard deviation σ. The speeds of molecules in an ideal gas obey a Maxwell–Boltzmann distribution.
2.2.10 Modify Bernoulli (PROGRAM 2.2.6) to animate the bar graph, replotting it after each experiment, so that you can watch it converge to the Gaussian distribution. Then add a command-line argument and an overloaded binomial() implementation to allow you to specify the probability p that a biased coin comes up heads, and run experiments to get a feeling for the distribution corresponding to a biased coin. Be sure to try values of p that are close to 0 and close to 1.
2.2.11 Develop a full implementation of StdArrayIO (implement all 12 methods indicated in the API).
2.2.12 Write a library Matrix that implements the following API:
public class Matrix
double dot(double[] a, double[] b)
vector dot product
double[][] multiply(double[][] a, double[][] b)
matrix-matrix product
double[][] transpose(double[][] a)
transpose
double[] multiply(double[][] a, double[] x)
matrix-vector product
double[] multiply(double[] x, double[][] a)
vector-matrix product
(See SECTION 1.4.) As a test client, use the following code, which performs the same calculation as Markov (PROGRAM 1.6.3):
public static void main(String[] args)
{
int trials = Integer.parseInt(args[0]);
double[][] p = StdArrayIO.readDouble2D();
double[] ranks = new double[p.length];
rank[0] = 1.0;
for (int t = 0; t < trials; t++)
ranks = Matrix.multiply(ranks, p);
StdArrayIO.print(ranks);
}Mathematicians and scientists use mature libraries or special-purpose matrix-processing languages for such tasks. See the booksite for details on using such libraries.
2.2.13 Write a Matrix client that implements the version of Markov described in SECTION 1.6 but is based on squaring the matrix, instead of iterating the vector–matrix multiplication.
2.2.14 Rewrite RandomSurfer (PROGRAM 1.6.2) using the StdArrayIO and StdRandom libraries.
Partial solution.
...
double[][] p = StdArrayIO.readDouble2D();
int page = 0; // Start at page 0.
int[] freq = new int[n];
for (int t = 0; t < trials; t++)
{
page = StdRandom.discrete(p[page]);
freq[page]++;
}
...