<jupyter_start><jupyter_text>Benchmark $g(x) = (n+a\sigma \sqrt{n}) - \sum_{i=1}^n x_i $<jupyter_code>import scipy
def g(x, n, a=3, sigma=0.2):
    """
    x shape [ndim, nsamples]
    """
    y = n+a*sigma*np.sqrt(n) - np.sum(x, axis=0)
    return y
x = np.arange(0,5,1)[1:]
y = np.arange(0,5,1)[1:]
xx,yy = np.meshgrid(x, y)
x = xx.reshape(1,-1)
y = yy.reshape(1,-1)
xy = np.concatenate((x,y), axis=0)
print(xy.shape)
y = g(xy, 2)
y = y.reshape(xx.shape)
plt.figure()
plt.contour(xx, yy,y)
print(y>0)
n, mu, sigma = 2, 1, 0.2

n_samples = int(1e6)
x = scipy.stats.lognorm.rvs(sigma,scale=np.exp(mu), size=(n,n_samples))
print(np.mean(np.log(x),axis=1))
print(np.std(np.log(x),axis=1))

print(x.shape)
gx = g(x,n,sigma=sigma)
print(gx)
pf = np.sum(gx<0)/n_samples
print(np.around(pf,10))
def g(x,c):
    x  = np.array(x)
    x1,x2 = x
    g1 = c-1-x2+np.exp(-x1**2/10)+ (x1/5)**4
    g2 = c**2/2 - x1*x2
    return np.minimum(g1,g2)
c, mu, sigma = 3, 0, 1
n_samples = np.logspace(2, 5,10,dtype=np.int)
pf_mean = []
pf_cov  = []
for isamples in n_samples:
    pf_ = []
    for _ in range(100):
        x = scipy.stats.norm.rvs(loc=0,scale=sigma, size=(2,isamples))
        gx= g(x, c)
        pf_.append(np.sum(gx<0)/isamples)
    pf_mean.append(np.mean(pf_))
    pf_cov.append(np.std(pf_)/abs(np.mean(pf_))) 
    
plt.figure()
plt.plot(n_samples, pf_mean)
# plt.plot(n_samples, pf_cov)
plt.yscale('log')<jupyter_output><empty_output><jupyter_text>$f(x) =x , x\sim N(0,1)$<jupyter_code>from scipy.stats import norm
### Brute monte carlo
n_samples = int(1e)
x_p = norm.rvs(0,1,n_samples)
mu  = np.mean(x_p)
sigma = np.std(x_p)
print(mu, sigma)
n_samples = int(1e7)
mu, sigma = 0,np.arange(np.sqrt(0.5),5,1)[1:]
# mu, sigma = 0,np.array([1])
sigma_q_analytical = sigma/(2-sigma**-2)**1.5
mu_hat = []
sigma_hat = []
for isigma in sigma:    
    x_q = norm.rvs(mu, isigma,n_samples)
    w   = norm.pdf(x_q, 0,1)/norm.pdf(x_q, mu, isigma)
    mu_hat.append(np.mean(x_q*w))
    sigma_hat.append(np.std(x_q*w))

plt.figure()
plt.plot(sigma, sigma_hat, label='IS')
plt.plot(sigma, sigma_q_analytical, label='Analytical')
plt.plot(sigma, mu_hat, label = r'$\mu$')
# plt.plot([0,5],[0,0])
plt.ylim()
plt.legend()
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.colors as mplc

x, y = np.mgrid[0:1:0.01, 0:1:0.01]
r = np.sqrt(x ** 2 + y ** 2)
z = np.sin(6 * np.pi * r)

norm = mplc.Normalize()
print(norm.vmin, norm.vmax)

norm = mplc.Normalize(0, 0.5)
print(norm.vmin, norm.vmax)
fig0, ax0 = plt.subplots(1, 1, )
cf0 = ax0.contourf(x, y, z, np.arange(0, .5, .01), norm=norm,
                   extend='both', cmap='viridis')
cbar0 = plt.colorbar(cf0,)<jupyter_output>None None
0.0 0.5
{'fraction_non_alphanumeric': 0.21224489795918366, 'fraction_numerical': 0.04452690166975881, 'mean_word_length': 3.946788990825688, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '555894', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 1388, 'n_tokens_neox': 1328, 'n_words': 227, 'path': 'examples/JupyterNotebook/ISPCE.ipynb', 'repo_name': 'Jinsongl/UQRA', 'size': '2695'}