<jupyter_start><jupyter_text>Monte CarloThis is the simple Monte Carlo example you worked on in class.Consider the following objective and constraint\begin{align*}f(x) &= x_1^2 + 2x_2^2 + 3x_3^2\\c(x) &= x_1 + x_2 + x_3 \le 3.5\end{align*}At the point:$$x = [1, 1, 1]$$the standard deviation in $x$ is (normally distributed)$$\sigma_x = [0.0, 0.06, 0.2]$$Compute the following:- Output statistics for $f$ (mean, standard deviation, histogram)- Reliability of $c$<jupyter_code>def func(x):
    return x[0]**2 + 2*x[1]**2 + 3*x[2]**2

def con(x):
    return x[0] + x[1] + x[2] - 3.5  # rewritten in form c <= 0
x = [1.0, 1.0, 1.0]
sigma = [0.00, 0.06, 0.2]<jupyter_output><empty_output><jupyter_text>We will use randn, which gives us a random number k sampled from a normal distribution.  It is sampled from a unit normal with zero mean and a standard deviation of 1 so to translate to an arbitrary mean and standard deviation the random value will be$$ x = \mu + k \sigma $$If we sample enough times, we should get reliable statistics<jupyter_code>import numpy as np

def stats(n):
    f = np.zeros(n)
    c = np.zeros(n)

    for i in range(n):
        x1 = x[0]
        x2 = x[1] + np.random.randn(1)*sigma[1]
        x3 = x[2] + np.random.randn(1)*sigma[2]

        f[i] = func([x1, x2, x3])
        c[i] = con([x1, x2, x3])
        
    # mean
    mu = np.average(f)
    
    # standard deviation
    std = np.std(f, ddof=1)  #ddof=1 gives an unbiased estimate (np.sqrt(1.0/(n-1)*(np.sum(f**2) - n*mu**2)))

    return mu, std, f, c<jupyter_output><empty_output><jupyter_text>Let's evaluate this function for different values of n (number of samples) to see how long it takes to converge.<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt

nvec = np.logspace(1, 6, 20)
muvec = np.zeros(20)
stdvec = np.zeros(20)

for i, n in enumerate(nvec):
    muvec[i], stdvec[i], _, _ = stats(int(n))
    print i
    
plt.figure()
plt.semilogx(nvec, muvec, '-o')

plt.figure()
plt.semilogx(nvec, stdvec, '-o')
plt.show()<jupyter_output>0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19<jupyter_text>Note that it takes about 100,000 simulations for the statistics to converge.  Let's rerun that case and check our the histogram and statistics.<jupyter_code>n = 1e5
mu, std, f, c = stats(int(n))
print 'mu =', mu
print 'sigma =', std
plt.figure()
plt.hist(f, bins=20);<jupyter_output>mu = 6.13291096795
sigma = 1.24154429602<jupyter_text>Notice that it skews to the right.  Because of the square terms in the function, any deviation causes the function to increase.We can also estimate our reliability just by counting up our failures (or counting up the number of times the constraint was satisfied).<jupyter_code>reliability = np.count_nonzero(c <= 0.0)/float(n)
print 'reliability = ', reliability*100, '%'<jupyter_output>reliability =  99.168 %<jupyter_text>Monte Carlo with LHSIn our discussion on surrogate-based optimization we learned about Latin Hypercube Sampling (LHS).  Let's apply LHS to Monte Carlo to see if it speeds up convergence.<jupyter_code>from pyDOE import lhs
from scipy.stats.distributions import norm

def statsLHS(n):
    f = np.zeros(n)
    c = np.zeros(n)
    
    # generate latin hypercube sample points beforehand from normal dist
    lhd = lhs(2, samples=n)
    rpt = norm(loc=0, scale=1).ppf(lhd)

    for i in range(n):
        x1 = x[0]
        x2 = x[1] + rpt[i, 0]*sigma[1]
        x3 = x[2] + rpt[i, 1]*sigma[2]

        f[i] = func([x1, x2, x3])
        c[i] = con([x1, x2, x3])
        
    # mean
    mu = np.average(f)
    
    # standard deviation
    std = np.std(f, ddof=1)  #ddof=1 gives an unbiased estimate (np.sqrt(1.0/(n-1)*(np.sum(f**2) - n*mu**2)))

    return mu, std, f, c


muLHS = np.zeros(20)
stdLHS = np.zeros(20)

for i, n in enumerate(nvec):
    muLHS[i], stdLHS[i], _, _ = statsLHS(int(n))
    print i
    
plt.figure()
plt.semilogx(nvec, muvec, '-o')
plt.semilogx(nvec, muLHS, '-o')

plt.figure()
plt.semilogx(nvec, stdvec, '-o')
plt.semilogx(nvec, stdLHS, '-o')
plt.show()<jupyter_output>0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{'fraction_non_alphanumeric': 0.1391664635632464, 'fraction_numerical': 0.05288813063611991, 'mean_word_length': 3.5298013245033113, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '100709', 'license': 'MIT', 'n_tokens_mistral': 1750, 'n_tokens_neox': 1612, 'n_words': 495, 'path': 'MonteCarlo.ipynb', 'repo_name': 'BYUFLOWLab/MDOnotebooks', 'size': '4103'}