.. code:: python
from d2l import mxnet as d2l
from mxnet import np, npx
import random
npx.set_np()
# Sample datapoints and create y coordinate
epsilon = 0.1
random.seed(8675309)
xs = np.random.normal(loc=0, scale=1, size=(300,))
ys = [np.sum(np.exp(-(xs[:i] - xs[i])**2 / (2 * epsilon**2))
/ np.sqrt(2*np.pi*epsilon**2)) / len(xs) for i in range(len(xs))]
# Compute true density
xd = np.arange(np.min(xs), np.max(xs), 0.01)
yd = np.exp(-xd**2/2) / np.sqrt(2 * np.pi)
# Plot the results
d2l.plot(xd, yd, 'x', 'density')
d2l.plt.scatter(xs, ys)
d2l.plt.axvline(x=0)
d2l.plt.axvline(x=np.mean(xs), linestyle='--', color='purple')
d2l.plt.title(f'sample mean: {float(np.mean(xs)):.2f}')
d2l.plt.show()
.. figure:: output_statistics_bc7dd5_3_0.svg
.. raw:: html

.. raw:: html
.. code:: python
from d2l import torch as d2l
import torch
torch.pi = torch.acos(torch.zeros(1)) * 2 #define pi in torch
# Sample datapoints and create y coordinate
epsilon = 0.1
torch.manual_seed(8675309)
xs = torch.randn(size=(300,))
ys = torch.tensor(
[torch.sum(torch.exp(-(xs[:i] - xs[i])**2 / (2 * epsilon**2))\
/ torch.sqrt(2*torch.pi*epsilon**2)) / len(xs)\
for i in range(len(xs))])
# Compute true density
xd = torch.arange(torch.min(xs), torch.max(xs), 0.01)
yd = torch.exp(-xd**2/2) / torch.sqrt(2 * torch.pi)
# Plot the results
d2l.plot(xd, yd, 'x', 'density')
d2l.plt.scatter(xs, ys)
d2l.plt.axvline(x=0)
d2l.plt.axvline(x=torch.mean(xs), linestyle='--', color='purple')
d2l.plt.title(f'sample mean: {float(torch.mean(xs).item()):.2f}')
d2l.plt.show()
.. figure:: output_statistics_bc7dd5_6_0.svg
.. raw:: html

.. raw:: html
.. code:: python
from d2l import tensorflow as d2l
import tensorflow as tf
tf.pi = tf.acos(tf.zeros(1)) * 2 # define pi in TensorFlow
# Sample datapoints and create y coordinate
epsilon = 0.1
xs = tf.random.normal((300,))
ys = tf.constant(
[(tf.reduce_sum(tf.exp(-(xs[:i] - xs[i])**2 / (2 * epsilon**2)) \
/ tf.sqrt(2*tf.pi*epsilon**2)) / tf.cast(
tf.size(xs), dtype=tf.float32)).numpy() \
for i in range(tf.size(xs))])
# Compute true density
xd = tf.range(tf.reduce_min(xs), tf.reduce_max(xs), 0.01)
yd = tf.exp(-xd**2/2) / tf.sqrt(2 * tf.pi)
# Plot the results
d2l.plot(xd, yd, 'x', 'density')
d2l.plt.scatter(xs, ys)
d2l.plt.axvline(x=0)
d2l.plt.axvline(x=tf.reduce_mean(xs), linestyle='--', color='purple')
d2l.plt.title(f'sample mean: {float(tf.reduce_mean(xs).numpy()):.2f}')
d2l.plt.show()
.. figure:: output_statistics_bc7dd5_9_0.svg
.. raw:: html

.. raw:: html
.. code:: python
# Statistical bias
def stat_bias(true_theta, est_theta):
return(np.mean(est_theta) - true_theta)
# Mean squared error
def mse(data, true_theta):
return(np.mean(np.square(data - true_theta)))
.. raw:: html

.. raw:: html
.. code:: python
# Statistical bias
def stat_bias(true_theta, est_theta):
return(torch.mean(est_theta) - true_theta)
# Mean squared error
def mse(data, true_theta):
return(torch.mean(torch.square(data - true_theta)))
.. raw:: html

.. raw:: html
.. code:: python
# Statistical bias
def stat_bias(true_theta, est_theta):
return(tf.reduce_mean(est_theta) - true_theta)
# Mean squared error
def mse(data, true_theta):
return(tf.reduce_mean(tf.square(data - true_theta)))
.. raw:: html

.. raw:: html
.. code:: python
theta_true = 1
sigma = 4
sample_len = 10000
samples = np.random.normal(theta_true, sigma, sample_len)
theta_est = np.mean(samples)
theta_est
.. parsed-literal::
:class: output
array(0.9503336)
.. raw:: html

.. raw:: html
.. code:: python
theta_true = 1
sigma = 4
sample_len = 10000
samples = torch.normal(theta_true, sigma, size=(sample_len, 1))
theta_est = torch.mean(samples)
theta_est
.. parsed-literal::
:class: output
tensor(1.0170)
.. raw:: html

.. raw:: html
.. code:: python
theta_true = 1
sigma = 4
sample_len = 10000
samples = tf.random.normal((sample_len, 1), theta_true, sigma)
theta_est = tf.reduce_mean(samples)
theta_est
.. parsed-literal::
:class: output
.. raw:: html

.. raw:: html
.. code:: python
mse(samples, theta_true)
.. parsed-literal::
:class: output
array(15.781996)
.. raw:: html

.. raw:: html
.. code:: python
mse(samples, theta_true)
.. parsed-literal::
:class: output
tensor(16.0298)
.. raw:: html

.. raw:: html
.. code:: python
mse(samples, theta_true)
.. parsed-literal::
:class: output
.. raw:: html

.. raw:: html
.. code:: python
bias = stat_bias(theta_true, theta_est)
np.square(samples.std()) + np.square(bias)
.. parsed-literal::
:class: output
array(15.781995)
.. raw:: html

.. raw:: html
.. code:: python
bias = stat_bias(theta_true, theta_est)
torch.square(samples.std(unbiased=False)) + torch.square(bias)
.. parsed-literal::
:class: output
tensor(16.0298)
.. raw:: html

.. raw:: html
.. code:: python
bias = stat_bias(theta_true, theta_est)
tf.square(tf.math.reduce_std(samples)) + tf.square(bias)
.. parsed-literal::
:class: output
.. raw:: html

.. raw:: html
.. code:: python
# Number of samples
N = 1000
# Sample dataset
samples = np.random.normal(loc=0, scale=1, size=(N,))
# Lookup Students's t-distribution c.d.f.
t_star = 1.96
# Construct interval
mu_hat = np.mean(samples)
sigma_hat = samples.std(ddof=1)
(mu_hat - t_star*sigma_hat/np.sqrt(N), mu_hat + t_star*sigma_hat/np.sqrt(N))
.. parsed-literal::
:class: output
(array(-0.07853346), array(0.04412608))
.. raw:: html

.. raw:: html
.. code:: python
# PyTorch uses Bessel's correction by default, which means the use of ddof=1
# instead of default ddof=0 in numpy. We can use unbiased=False to imitate
# ddof=0.
# Number of samples
N = 1000
# Sample dataset
samples = torch.normal(0, 1, size=(N,))
# Lookup Students's t-distribution c.d.f.
t_star = 1.96
# Construct interval
mu_hat = torch.mean(samples)
sigma_hat = samples.std(unbiased=True)
(mu_hat - t_star*sigma_hat/torch.sqrt(torch.tensor(N, dtype=torch.float32)),\
mu_hat + t_star*sigma_hat/torch.sqrt(torch.tensor(N, dtype=torch.float32)))
.. parsed-literal::
:class: output
(tensor(-0.0568), tensor(0.0704))
.. raw:: html

.. raw:: html
.. code:: python
# Number of samples
N = 1000
# Sample dataset
samples = tf.random.normal((N,), 0, 1)
# Lookup Students's t-distribution c.d.f.
t_star = 1.96
# Construct interval
mu_hat = tf.reduce_mean(samples)
sigma_hat = tf.math.reduce_std(samples)
(mu_hat - t_star*sigma_hat/tf.sqrt(tf.constant(N, dtype=tf.float32)), \
mu_hat + t_star*sigma_hat/tf.sqrt(tf.constant(N, dtype=tf.float32)))
.. parsed-literal::
:class: output
(,
)
.. raw:: html

.. raw:: html
`Discussions `__
.. raw:: html

.. raw:: html
`Discussions `__
.. raw:: html

.. raw:: html
`Discussions `__
.. raw:: html

.. raw:: html