Beispiel #1
0
ax.set_xlabel(r'$\mathbb{E}[Q_N]$')
ax.set_ylabel(r'$\mathbb{P}(\mathbb{E}[Q_N])$')
_ = ax.legend(loc='upper left')

#%%
#The numerical results match our theory. Specifically the estimator is unbiased( i.e. mean zero, and the variance of the estimator is :math:`\var{Q_{0,N}}=\var{Q_{0}}/N=1/N`.
#
#The variance of the estimator can be driven to zero by increasing the number of samples :math:`N`. However when the variance becomes less than the bias, i.e. :math:`\left(\mean{Q_{\alpha}-Q}\right)^2>\var{Q_{\alpha}}/N`, then the MSE will not decrease and any further samples used to reduce the variance are wasted.
#
#Let our true model be :math:`f_0` above. The following code compues the bias induced by using :math:`f_\alpha=f_1` and also plots the contours of :math:`f_0(\rv)-f_1(\rv)`.

integrand_f0 = model.A0 * (sp.cos(model.theta0) * z1**5 +
                           sp.sin(model.theta0) * z2**5) * 0.25
exact_integral_f0 = float(
    sp.integrate(integrand_f0, (z1, ranges[0], ranges[1]),
                 (z2, ranges[2], ranges[3])))
bias = (exact_integral_f0 - exact_integral_f1)**2
print('MC f1 bias =', bias)
print('MC f1 variance =', means.var())
print('MC f1 MSE =', bias + means.var())

fig, ax = plt.subplots()
X, Y, Z = pya.get_meshgrid_function_data(lambda z: model.m0(z) - model.m1(z),
                                         [-1, 1, -1, 1], 50)
cset = ax.contourf(X, Y, Z, levels=np.linspace(Z.min(), Z.max(), 20))
_ = plt.colorbar(cset, ax=ax)
#plt.show()

#%%
#As :math:`N\to\infty` the MSE will only converge to the bias (:math:`s_1`). Try this by increasing :math:`\texttt{nsamples}`.
Beispiel #2
0
axplot = axs[1].plot(x_truth, data_obs, 'ok', ms=10)

#%%
#Lets also plot the joint distribution and marginals in a 3d plot


def data_obs_limit_state(samples, vals, data_obs):
    vals = np.ones((samples.shape[1]), float)
    I = np.where(samples[1, :] <= data_obs[0, 0])[0]
    return I, 0.


num_pts_1d = 100
limit_state = partial(data_obs_limit_state, data_obs=data_obs)
X, Y, Z = pya.get_meshgrid_function_data(joint.pdf,
                                         joint.plot_limits,
                                         num_pts_1d,
                                         qoi=0)
offset = -(Z.max() - Z.min())
samples = np.array([[x_truth, data_obs[0, 0], offset]]).T
ax = pya.create_3d_axis()
pya.plot_surface(X,
                 Y,
                 Z,
                 ax,
                 axis_labels=[r'$x_1$', r'$d$', ''],
                 limit_state=limit_state,
                 alpha=0.3,
                 cmap=mpl.cm.coolwarm,
                 zorder=3,
                 plot_axes=False)
num_contour_levels = 30