Lecture 4 Flashcards
RNG, properties
Random Number Generators (truly-random method and pseudorandom method)
- Random pattern
- Long period
- Efficiency
- Repeatability
- Portability
period of a pseudorandom number generator
maximum length of the repetition-free prefix of the sequence
LCG
Linear Congruential Generator
x_k = (ax_{k−1}+c) mod M
a, c integers, x_0 seed. Period can not exceed M.
Monte Carlo
Algorithms that rely on repeated random sampling to approximate a desired quantity.
- Nondeterministic processes
- Complicated deterministic problems with high dimensionality
Monte Carlo error, dependance
err → Z/√(n), with Z ~ N(0,σ^2)
asymptotic behavior O(1/√(n)), n number of samples
MC depends on sample variance and number of samples.
Monte Carlo:
n1 samples give an error e1. How many samples to get an error e2?
n1 * (e1/e2)^2
Where to use MC?
- Integration of high-dimensional functions
- Problems where high accuracy is not required
- To determine the matrix conditioning by sampling vectors
Generate a random vector of n integers in [0,10]
v = np.random.choice(range(11), n, False)
Get index of first item satisfying a condition in an array
index = np.argmax(array condition)
Classic plot
import matplotlib
import matplotlib.pyplot as plt
plt. plot(x, y)
plt. title(‘title’)
plt. ylabel(‘y label’)
plt. xlabel(‘x label’)
plt. show()
Plot loglog semilog…
Log scaling on the x axis: plt.semilogx(x,y)
plt. semilogy(x,y)
plt. loglog(x,y)
Examples loglog semilog…
y=a.x^b
y=a.b^x
y=a.log(b.x)
To get a line:
loglog: log(y) = log(a) + b.log(x)
semilogy: log(y) = log(a) + x.log(b) = ^a.x + b
semilogx: y = a.log(b) + log(x) = ^a + log(x)
Monte Carlo integration problem, estimation:
mu = int(x1,x2) int(y1,y2) f(x,y)dxdy
1/N Sum^N_{i=1} f(xi,yi) * (x2-x1) * (y2-y1) → mu when N → ∞ (Law large numbers)
With points generated within x1, x2, y1, y2
Monte Carlo pros/cons
Cons: Slow convergence rate
Pros: Efficiency does not degrade with increase in the dimension of the problem