Hide code cell source
MAKE_BOOK_FIGURES=Trueimport numpy as npimport scipy.stats as stimport matplotlib as mplimport matplotlib.pyplot as plt%matplotlib inlineimport matplotlib_inlinematplotlib_inline.backend_inline.set_matplotlib_formats('svg')import seaborn as snssns.set_context("paper")sns.set_style("ticks")def set_book_style():    plt.style.use('seaborn-v0_8-white')     sns.set_style("ticks")    sns.set_palette("deep")    mpl.rcParams.update({        # Font settings        'font.family': 'serif',  # For academic publishing        'font.size': 8,  # As requested, 10pt font        'axes.labelsize': 8,        'axes.titlesize': 8,        'xtick.labelsize': 7,  # Slightly smaller for better readability        'ytick.labelsize': 7,        'legend.fontsize': 7,                # Line and marker settings for consistency        'axes.linewidth': 0.5,        'grid.linewidth': 0.5,        'lines.linewidth': 1.0,        'lines.markersize': 4,                # Layout to prevent clipped labels        'figure.constrained_layout.use': True,                # Default DPI (will override when saving)        'figure.dpi': 600,        'savefig.dpi': 600,                # Despine - remove top and right spines        'axes.spines.top': False,        'axes.spines.right': False,                # Remove legend frame        'legend.frameon': False,                # Additional trim settings        'figure.autolayout': True,  # Alternative to constrained_layout        'savefig.bbox': 'tight',    # Trim when saving        'savefig.pad_inches': 0.1   # Small padding to ensure nothing gets cut off    })def set_notebook_style():    plt.style.use('seaborn-v0_8-white')    sns.set_style("ticks")    sns.set_palette("deep")    mpl.rcParams.update({        # Font settings - using default sizes        'font.family': 'serif',        'axes.labelsize': 10,        'axes.titlesize': 10,        'xtick.labelsize': 9,        'ytick.labelsize': 9,        'legend.fontsize': 9,                # Line and marker settings        'axes.linewidth': 0.5,        'grid.linewidth': 0.5,        'lines.linewidth': 1.0,        'lines.markersize': 4,                # Layout settings        'figure.constrained_layout.use': True,                # Remove only top and right spines        'axes.spines.top': False,        'axes.spines.right': False,                # Remove legend frame        'legend.frameon': False,                # Additional settings        'figure.autolayout': True,        'savefig.bbox': 'tight',        'savefig.pad_inches': 0.1    })def save_for_book(fig, filename, is_vector=True, **kwargs):    """    Save a figure with book-optimized settings.        Parameters:    -----------    fig : matplotlib figure        The figure to save    filename : str        Filename without extension    is_vector : bool        If True, saves as vector at 1000 dpi. If False, saves as raster at 600 dpi.    **kwargs : dict        Additional kwargs to pass to savefig    """        # Set appropriate DPI and format based on figure type    if is_vector:        dpi = 1000        ext = '.pdf'    else:        dpi = 600        ext = '.tif'        # Save the figure with book settings    fig.savefig(f"{filename}{ext}", dpi=dpi, **kwargs)def make_full_width_fig():    return plt.subplots(figsize=(4.7, 2.9), constrained_layout=True)def make_half_width_fig():    return plt.subplots(figsize=(2.35, 1.45), constrained_layout=True)if MAKE_BOOK_FIGURES:    set_book_style()else:    set_notebook_style()make_full_width_fig = make_full_width_fig if MAKE_BOOK_FIGURES else lambda: plt.subplots()make_half_width_fig = make_half_width_fig if MAKE_BOOK_FIGURES else lambda: plt.subplots()

The Uniform Distribution#

The uniform distribution is the most common continuous distribution. It corresponds to a random variable equally likely to take a value within a given interval. We write:

\[ X\sim U([0,1]), \]

and we read \(X\) follows a uniform distribution taking values in \([0,1]\).

The PDF of the uniform is constant in \([0,1]\) and zero outside it. We have:

\[\begin{split} f_X(x) := U(x|[0,1]) := \begin{cases} 1,&\;0\le x \le 1,\\ 0,&\;\text{otherwise}. \end{cases} \end{split}\]

Here is how you can make this random variable in scipy.stats:

import scipy.stats as st
X = st.uniform()

You can evaluate the PDF anywhere like this:

X.pdf(0.5)
1.0
X.pdf(-0.1)
0.0

Here is the plot of the PDF:

import numpy as np
x = np.linspace(-0.1, 1.1, 100)
plt.plot(x, X.pdf(x))
plt.xlabel('$x$')
plt.ylabel('$f_X(x)$')
plt.title('Probability density function of $U([0,1])$')
sns.despine(trim=True);
../_images/aed1af4c40947b2ffd8eda6475cd49b228db1a6b40353f57dac1998466dc5177.svg

The CDF of the uniform is for \(x\) in \([0,1]\):

\[ F(x) = p(X \le x) = \int_0^x f_X(u) du = \int_0^x du = x. \]

Obviously, we have \(F(x) = 0\) for \(x < 0\) and \(F(x) = 1\) for \(x > 1\).

If you have a scipy random variable, you can evaluate the CDF like this:

X.cdf(0.5)
0.5

And here is the plot of the CDF:

fig, ax = plt.subplots()
ax.plot(x, X.cdf(x))
ax.set_xlabel("$x$")
ax.set_ylabel("$F(x)$")
sns.despine(trim=True);
../_images/9d48b89a51ead4bfcb3e724629565b2ae4f0bb1834d1c717aef15f3bfb5433e3.svg

The probability that \(X\) takes values in \([a,b]\) for \(a < b\) in \([0,1]\) is:

\[ p(a \le X \le b) = F(b) - F(a) = b - a. \]

The expectation of the uniform is:

\[ \mathbb{E}[X] = \int_0^1 xdx = \frac{1}{2}. \]

The variance of the uniform is:

\[ \mathbb{V}[X] = \mathbb{E}[X^2] - \left(\mathbb{E}[X]\right)^2 = \frac{1}{3} - \frac{1}{4} = \frac{1}{12}. \]

You can get the expectation from scipy like this:

print(f"E[X] = {X.expect():.2f}")
E[X] = 0.50

Similarly for the variance:

# The variance is:
print(f"V[X] = {X.var():.2f}")
V[X] = 0.08

Here is how you can sample from the uniform one hundred times:

X.rvs(size=100)
Hide code cell output
array([0.76729017, 0.36952278, 0.10087681, 0.07862868, 0.60059355,
       0.67411476, 0.52692837, 0.74775941, 0.32725188, 0.09251002,
       0.84754023, 0.48967682, 0.53803498, 0.63307813, 0.31388762,
       0.81414139, 0.71660186, 0.40905378, 0.46989262, 0.16505625,
       0.8165627 , 0.58280347, 0.84913497, 0.65398629, 0.92523862,
       0.55441257, 0.15409921, 0.75380523, 0.81245847, 0.15343603,
       0.93335657, 0.85958455, 0.57108753, 0.17125569, 0.24601956,
       0.25005015, 0.89133982, 0.10301823, 0.64911844, 0.22709183,
       0.01927219, 0.95021723, 0.6476825 , 0.01218941, 0.89630753,
       0.59988331, 0.33984252, 0.66573216, 0.69288164, 0.62555955,
       0.27574074, 0.5206354 , 0.61875597, 0.41581619, 0.4513762 ,
       0.92981875, 0.227476  , 0.43180579, 0.21190154, 0.49153453,
       0.49341273, 0.61105989, 0.07411594, 0.1829612 , 0.01207508,
       0.24722949, 0.55046647, 0.04399492, 0.09769202, 0.43167262,
       0.70403407, 0.07128094, 0.04293587, 0.70561647, 0.17386921,
       0.36427349, 0.05374578, 0.19023514, 0.64702886, 0.17007486,
       0.71964167, 0.06407958, 0.76850661, 0.929924  , 0.89864387,
       0.23800754, 0.89793791, 0.97326305, 0.47128844, 0.75338281,
       0.59765009, 0.44245882, 0.76754975, 0.25625771, 0.97200478,
       0.09419354, 0.29462606, 0.47367611, 0.41960741, 0.76614393])

An alternative way is to use the functionality of numpy:

np.random.rand(100)
Hide code cell output
array([0.93093137, 0.16267519, 0.98072917, 0.55046935, 0.61283248,
       0.74779252, 0.22316264, 0.56462595, 0.92195484, 0.76353306,
       0.4694509 , 0.78917924, 0.04214299, 0.75893337, 0.25455189,
       0.9561007 , 0.62399571, 0.6791297 , 0.72893628, 0.05441865,
       0.85144579, 0.97866923, 0.85819365, 0.64255414, 0.28482819,
       0.11235003, 0.27351465, 0.19731848, 0.48427637, 0.67619509,
       0.34797569, 0.70134629, 0.04246567, 0.08694933, 0.93984669,
       0.87682069, 0.41592074, 0.35655285, 0.13357834, 0.01134986,
       0.80493372, 0.07183044, 0.51270814, 0.76724534, 0.22777905,
       0.93031754, 0.40681666, 0.72016314, 0.31751413, 0.359763  ,
       0.89388459, 0.1223253 , 0.84354193, 0.17038232, 0.30717771,
       0.65363139, 0.98618411, 0.46358422, 0.25264433, 0.79273715,
       0.9239096 , 0.017061  , 0.58550893, 0.73638448, 0.57617304,
       0.45232555, 0.20714044, 0.8360547 , 0.7700278 , 0.8926971 ,
       0.74378973, 0.62038716, 0.83780607, 0.02930129, 0.16029063,
       0.42704029, 0.75474664, 0.59888824, 0.84668931, 0.70842054,
       0.72007957, 0.29441483, 0.46133129, 0.99088283, 0.33129073,
       0.30146832, 0.79466372, 0.89290712, 0.74968618, 0.32788356,
       0.16177775, 0.3871587 , 0.10573329, 0.46312394, 0.65462353,
       0.37192501, 0.85224172, 0.61887343, 0.99525838, 0.85706736])

Finally, let’s find the probability that X is between two numbers. In particular, we will find \(p(-1 \le X \le 0.3)\):

a = -1.0
b = 0.3
prob_X_is_in_ab = X.cdf(b) - X.cdf(a)
print(f"p({a:.2f} <= X <= {b:.2f}) = {prob_X_is_in_ab:.2f}")
p(-1.00 <= X <= 0.30) = 0.30

The uniform distribution over an arbitrary interval \([a, b]\)#

One can define a uniform distribution over an arbitrary interval \([a,b]\). We write:

\[ X \sim U([a, b]). \]

The PDF of this random variable is:

\[\begin{split} f_X(x) = \begin{cases} c,&\;x\in[a,b],\\ 0,&\;\text{otherwise}, \end{cases} \end{split}\]

where \(c\) is a positive constant. The formula tells us that the probability density of finding \(X\) in \([a,b]\) is positive and that the probability density of finding outside is zero. We can determine the positive constant \(c\) by imposing the normalization condition:

\[ \int_{-\infty}^{+\infty}f_X(x)dx = 1. \]

Carrying out the integral:

\[ 1 = \int_{-\infty}^{+\infty}p(x)dx = \int_a^bc dx = c \int_a^bdx = c (b-a). \]

Therefore:

\[ c = \frac{1}{b - a}, \]

and we can now write:

\[\begin{split} f_X(x) = \begin{cases} \frac{1}{b-a},&x \in [a, b],\\ 0,&\;\text{otherwise}, \end{cases} \end{split}\]

From the PDF, we can now find the CDF for \(x \in [a,b]\):

\[ F(x) = p(X\le x) = \int_{-\infty}^x f_X(u)du = \int_a^x \frac{1}{b-a}du = \frac{1}{b-a}\int_a^xdu = \frac{x-a}{b-a}. \]

The expectation is:

\[ \mathbb{E}[X] = \frac{1}{2}(a+b), \]

and the variance is:

\[ \mathbb{V}[X] = \frac{1}{12}(b-a)^2. \]

Here is how you can do this using scipy.stats for \(a=-2\) and \(b=5\):

a = -2.0
b = 5.0
X = st.uniform(loc=a, scale=(b-a))

The PDF is:

Hide code cell source
fig, ax = plt.subplots()
xs = np.linspace(a - 0.1, b + 0.1, 100)
ax.plot(xs, X.pdf(xs))
ax.set_xlabel("$x$")
ax.set_ylabel("$f_X(x)$")
sns.despine(trim=True);
../_images/51786b3e1f5a21a482ce919c92f988652319fc470cbf77631fcee21f755dc476.svg

The CDF is:

Hide code cell source
fig, ax = plt.subplots()
xs = np.linspace(a - 0.1, b + 0.1, 100)
ax.plot(xs, X.cdf(xs))
ax.set_xlabel("$x$")
ax.set_ylabel("$F(x)$")
sns.despine(trim=True);
../_images/94679b612e8efbf02741baff611af91ad46a078bc199955aff62a933e8a53807.svg

The expectation is:

print(f"E[X] = {X.expect():.2f}")
E[X] = 1.50

And the variance:

# The variance is:
print(f"V[X] = {X.var():.2f}")
V[X] = 4.08

And here are a few random samples:

X.rvs(size=100)
Hide code cell output
array([ 3.16665541, -0.18104791,  1.96342543,  4.18870164,  3.23764465,
       -0.82096326,  4.98052135, -1.38981321, -0.50330442,  3.04544003,
        3.57291963, -0.0246068 , -0.75965116,  2.95439047, -1.12582801,
       -0.21330492, -0.06041608,  1.05717779,  3.74259905,  1.22815532,
        4.46087461,  4.65834513,  1.98313002, -0.3457419 ,  0.52873396,
        2.19216614,  0.58014778,  3.48916931,  2.58836805, -0.09261791,
        2.6469498 ,  1.44946877,  2.43532653,  0.58238562, -0.49375135,
        1.56556198,  2.15663798,  4.02352609,  0.50995412, -1.3694415 ,
        3.63864776,  4.20909284,  0.21081806,  0.9119826 ,  0.39183794,
        3.40977852,  0.4506317 , -0.81889945,  0.43709493,  2.72611971,
        2.79490825,  0.28771322,  1.04592226,  2.35724624,  0.23814042,
        3.04293161,  3.45193204,  4.64232531, -1.44629507,  2.22385568,
       -0.14967713,  2.57377918, -1.03224389,  1.61921102, -0.97853337,
        0.29126199,  2.56444605,  2.32616012,  0.57708777, -0.61447082,
        4.63412622, -1.87452257,  3.49904636, -1.52254057,  2.02114143,
       -0.25032501, -1.00256198,  0.12084568,  2.75575215, -0.33447655,
        4.87899017, -1.61458953,  4.94087464, -0.45925774,  3.66770739,
        3.72933382,  2.56221919, -0.81862425, -0.19654772,  4.28578981,
        0.87723842,  2.8401387 , -0.29426586, -1.43155218, -1.83854561,
       -0.54418838,  2.13432151, -0.13853689,  0.9280982 ,  2.39894963])

Alternative way to get \(U([a,b])\)#

There is another way to obtain samples from \(U([a,b])\) that uses only samples from \(U([0,1])\). Here is how. Let \(Z\) be a standard uniform random variable:

\[ Z\sim U([0,1]). \]

Then define the random variable:

\[ X = a + (b-a) Z. \]

Then, \(X\sim U([a,b])\). Why? Well, let’s just show that the CDF of \(X\) has the right form:

\[ p(X \le x) = p(a + (b-a)Z \le x) = p((b-a)Z \le x - a) = p\left(Z \le \frac{x-a}{b-a}\right) = \frac{x-a}{b-a}, \]

where the last step follows from the fact that the CDF of \(Z\) is simply: \(p(Z \le z) = z\). Equipped with this result, we can sample \(X\) by sampling \(Z\) and then scaling it appropriately (by the way, this is what scipy.stats is doing internally). Here it is using numpy.random.rand to sample in \([0,1]\):

x_samples = a + (b - a) * np.random.rand(1000)
print(x_samples)
Hide code cell output
[ 1.69239621  0.9757347   2.01930877  0.97124803 -1.78383998  3.50721793
  3.85687481  1.34565458  2.65133676  2.73446522  3.58543036  0.03099628
  3.00420889 -1.49087007  3.98757702  0.62403014  4.06576063  3.34915879
  0.7844617  -0.01357119  3.79397164 -0.41609199 -0.1838001   3.73428323
  1.74670737  3.28665485  2.66908929  0.38709258  2.62388955  0.14602571
  1.12803562 -1.07811501  0.4938018   0.87187493 -0.17122994  3.63108869
 -1.62355923  3.09161146  3.95091639  2.52920018  0.41394578  4.83210917
  1.01172741 -0.0238804   2.69182884 -1.92775519  2.73843321 -1.93005121
 -1.05087812  2.36430478  3.4843959   1.50841697  0.48301237  1.11976217
  1.0858566   2.74410851  4.10218827  2.31230585  1.86022678 -0.43176399
  2.19977026  2.61475477  0.44763574 -1.68342423  1.29116325  0.41739773
  4.68193632 -0.58008792  0.24255565  4.16571672  3.09296445  1.63990842
 -1.40279869 -0.74046118  2.84061453 -0.39366519  4.02480577 -0.24199919
  3.95768273  4.57654153  0.89029425  3.94460064 -0.21364654 -0.93256834
 -1.67810641  4.20794745  3.37431458  0.46095725 -0.2828538   2.43185576
  4.70701524  3.52363113  3.12029176  0.49645088 -1.59392279  4.30275722
 -0.81028296  4.91802452  1.16206686 -0.06496322 -0.46547529  3.17572741
  0.22555509 -1.96911046 -1.49935839 -1.37990789  2.21387946  2.64317858
  3.54794944  2.60540981  2.65537739  4.91447408  2.55111586 -0.16833468
  3.23147335  2.82929278  2.45067751  4.07499066  2.64126431  4.79485783
  3.04951659  2.19951283  3.225053   -1.119928    2.67309166  3.59512495
  2.92063932  4.8301531   3.27312004  2.60848257  3.94279962  4.39631093
  1.16245675  2.88604704  0.19413883 -1.0123574   3.37618291  0.54166036
  1.0599277   0.90956041 -1.66188155  2.4950896  -0.16982471 -1.70007416
 -0.32933826  4.9849717   3.21771311  0.01113271  3.74848347 -0.39398217
  2.6328304   3.17993177  4.86731973  4.65143931  2.47186087  0.53639529
  3.21089198  4.39799823 -0.74483947  3.6630386  -1.25694502  3.18646792
  0.66148662 -1.37783143  4.97880851  4.91220526 -0.44270476 -0.20860791
 -0.14577594  2.46205913  2.57880247  3.82608033  0.93399996  3.21421695
  2.53183075 -0.01712106 -0.91560922 -0.15606679  4.60644124  3.29311881
  1.21777326 -1.72354653  2.05485699  0.55114528 -1.98667878  4.56782795
 -1.18856038  4.14512619 -0.67809452  0.13775855  1.69631917  3.78898137
  4.96428696 -0.25391899  1.36279535 -0.58343045  4.67717957 -0.53539081
 -1.98048713 -0.63206958 -0.59228126  0.11487649  1.64527795  3.30415489
  0.09505727 -1.90547527 -1.32037148 -0.21860963  4.01717372  1.84711659
  1.5600234   1.83159288 -1.81988983  4.60295725 -1.14387323  4.16421847
  0.30588997  2.84105631  2.23526888  1.029097    4.21632116 -0.93070824
  1.55172418  2.77141873  1.70069941  2.42137239  1.19168579  3.95831433
  2.62718735 -1.99134272  4.44446191  3.39083447  2.64457541  1.07821767
  4.98927454  4.04287803 -0.5281115  -0.0570207  -0.41891663 -0.84142561
  1.67248892  3.09397812 -0.82391329  1.60651842 -1.9785378   4.6243466
  0.62137735  4.25026859 -1.72434718  2.06274063 -1.16040357 -0.31558658
  2.46518921  4.58086644  0.75467885  4.04254744  0.5907563   0.33755603
  0.25467695 -0.8754782   1.01793792 -0.78259359  0.55439143  2.21404879
 -1.65950501  1.5276296   4.35576097 -1.81216882  0.27136631 -1.88569699
 -0.89926001  1.15200213  0.80084609  3.70797987  1.27811039  4.69874356
  0.33821269 -0.86602172  0.48264791  3.74547627  1.02413839 -0.19060522
  2.99081473  0.50275256  1.99213164 -0.96873555 -1.39009264 -1.16157027
  3.26919111  2.51838299 -1.98687465 -1.96483707 -0.22442162  3.40690677
  0.3964887   0.54432659 -1.93964358  1.90535547  3.56722818 -0.05389979
  1.03552764 -0.90048828  4.18692911 -1.59212372  1.01009508  2.51508675
  4.46567906  2.97204386  1.24453106  3.99364745  0.63258402  2.88593814
 -1.16735446 -0.97149864  3.73928129  1.29209307  2.78808252  2.12876421
  1.85431108  1.38935528  0.67304959  2.53895831  0.08333362  1.21630742
 -1.23277459  3.64512489  4.7405222   4.01098079  4.73471929  2.68865283
  0.1512616   3.59087967  1.40710095  3.68301795  4.58440582 -0.28747618
 -0.43237625  3.37270069  3.78695699  4.93706603 -1.90999428 -0.2368028
 -1.97446711  1.34503382  1.48080322  2.96080208  4.99341139  1.26885021
  4.86979571 -1.06358222  1.47921356  2.06268878 -1.53684037  1.56656437
  1.83161779  0.38207411 -1.34681487  4.23978764  3.48028991  4.29588141
 -0.34872137 -1.45194009 -0.2047992   1.05363221  2.58544432  1.20998416
  4.52983916 -1.87403626 -0.46682638  1.8040523  -0.2474103  -1.88775563
  4.88119462 -0.87977156  1.68328372 -1.93164725  1.13732868  4.39450803
  4.72514294 -1.60757219  1.52973059  0.26575866  4.23161035  1.07549971
 -0.55979883  1.80901393  4.43283738 -1.44719744  1.21653104  0.69518783
  3.65968577 -0.45175112 -1.8315917   1.79873147 -0.4955834  -1.79687415
  3.58181323  4.39017682  1.80138449 -0.79645392 -1.9562498  -0.80539048
  1.74945652  0.96689893  4.74743232  3.95486104 -1.28051136  1.52309636
  2.07624929  3.91723876 -1.35042832  1.1666016  -0.53164564  3.46860343
  1.21712726  4.80957348 -1.94680537  4.79934605 -0.14450988  2.13270591
  2.31695315  0.61281806  3.79091824 -1.84823256  2.47080525  0.61989889
  3.33301575  2.48732878 -0.40946618  4.48969634  2.90030514  2.03896491
  4.57192511  2.46973188  0.14598982  2.3617642   0.98914687  3.82049531
  3.32187279  1.59527795  0.0908875  -1.42845533  3.50486909  2.327694
  0.48261385  0.53057875 -1.99241133  2.15756853  2.09120998 -1.75890102
  2.01172868 -1.43162256 -1.64598936 -0.23424108  2.91236198  3.13213538
  4.76046789 -1.20449605  3.78999222  0.46582812  3.9511495   0.33707975
  1.05886191  1.08572497  2.82631533  1.59258346  2.05099986 -1.89746347
  0.53163366  1.59025144  0.76051826 -0.32937722  2.09531956 -1.7258793
  4.34027437 -0.59794967  0.25724681  3.79366945  0.07851849  1.20861127
  2.14794399  0.76740788  4.45622009  3.6130357   3.71769349  0.10603926
  3.46581567  1.71346018 -1.87585504  3.35764717  0.69351719  0.91050356
  2.13180111  0.41378184  4.78194892  4.85807424 -0.60416673  2.61027463
  1.25359187  2.53032124  3.05545283 -0.71971405  0.80718205  1.94633004
  2.43334301  3.21115768  0.70713623  1.83685248  3.71165665  1.84755502
 -1.65971321  1.04389404  0.95394517  2.06591894  4.63900519  0.87614458
 -1.53386128  4.33411796  2.98404542 -0.40842029  3.93504893  4.00907909
 -0.36484097  0.01315169  4.14903174  2.02352308 -1.40255963 -1.80385046
 -1.88186704  3.71422808  4.62692106  4.68813691 -0.48094642 -0.73922472
 -0.21588873  1.15005933  0.0557341   2.06101754 -0.57809339 -1.26214081
  4.84263842  0.54970281  0.34218415  0.46832135 -1.36943396  1.62037258
 -1.43970649  4.67262787 -1.61345085 -0.08838376  4.64322813  1.14822634
 -0.09211443 -1.22283262 -0.56920481  1.0848626   1.02331957  3.50200884
  2.02017679 -1.74523647 -0.14237785  3.92525846 -1.82702803  4.29742575
  2.29928318  0.35623798  2.66715427  1.60323599  0.91722768 -0.79009352
  4.91553128  2.0429254   1.22208707  3.58691363 -1.90612295  1.00852031
 -1.70494278  0.92446523 -0.6948281   2.19448497  4.32988567  2.70326521
  3.96164716  1.4958179   2.88587828  1.16858165  4.79659016 -1.97864233
  4.84586809  4.24694067  2.00139779  3.17212935  1.06436898  4.85570917
  4.78291199  2.68908821 -1.34594529 -1.05477144  0.27671221 -1.97444381
  4.20873216  3.48144499 -0.69175126  4.62572056  4.02138583  1.44168369
 -0.86836232  3.61625529  2.81563957  4.68047022  4.23158331  0.52801663
  4.99899405  0.87361555  4.07654918  4.00690442  0.8619838  -1.79755956
  3.15474018  4.78351268  4.59627569  1.41523868 -1.30263102 -1.32664273
 -1.57853089 -0.96799864  0.69112193  3.73054697  4.76201102 -0.49532385
  1.91984693  2.36517057  4.55742796  4.11024979  2.20757356  3.27480039
  2.38710533  3.62891955  1.32878537  3.46955615  3.56498385  2.18828122
  1.26383394  1.50652839  3.96035421  2.59328895  1.04808865 -0.17834341
  0.50685341 -0.03212137  3.42845032  3.45504671  1.43816364 -0.6583519
 -1.01501844 -1.80520758  4.49904223  4.71196037  0.55808086 -0.75117941
 -0.87014337  2.79035926  1.81668751  2.25770628 -1.60064612 -0.13411938
 -0.05322514  2.21176558  0.13117128  2.29040804 -1.80544006  3.62807994
  2.26358001 -1.52490154  0.94455705 -0.99976016  2.11089303 -0.14611034
  2.41564413  4.29263333 -0.20933148  0.12238578  0.75143096  2.81486562
  0.16755794  2.24242409 -1.31260079  4.00047316  1.94384089  1.99416364
  2.02302687  1.8381222   2.02530926  2.89049564 -0.47009939  0.71196963
 -0.64041028  2.27243232 -0.89437014  1.94817949 -1.84346263  0.26498987
  1.71383783  1.5738496  -0.1193343   1.09176536 -0.47072034  4.9401139
  2.57747024  2.11953695  1.59149625  4.91430433  2.20692505  2.70389622
  1.83373338  0.47941019  0.56907701  3.52925573  2.0218887   3.87330914
  1.51191817  4.06211985  2.69039052 -1.81002958  4.47040545 -1.60187394
  4.95448672  1.59541684  2.81336148  4.45220741 -1.51104356  0.98592701
  0.65749285  3.46960425  0.73341651  1.00479131  1.86081246  1.48066621
 -1.44022155  1.57964859  3.73502511 -0.06713501  3.95673961  0.05820612
  0.3218133   2.44509826  2.98315361  2.09921255  0.80915583  3.9666444
  2.52456182 -1.0387735   3.95438364  3.606256    0.33385115  0.46061771
  1.02821248 -1.9151517  -1.82400198  2.40394105  1.30846442  3.90852784
  1.62061093  1.45585064  1.65903396 -0.41524384  3.03320568  3.34160657
  3.08352592 -0.36609274  0.60525001  1.51404797  1.98023019  0.74225401
  4.85121594  3.19506274  2.41255602 -1.1676906   3.92525688  0.2045407
  1.28738112  4.42209625  4.35753633  2.52687736 -1.2786669   2.35141043
  3.37084498  4.02607462  1.91154069  4.16980131 -0.59167696  3.98453861
 -0.26572306  0.4604129   1.59300907 -0.2322581  -0.89182593  3.34575429
  0.62841105 -1.43536613 -0.3775873   0.02847658  0.34724231  4.20093122
  1.78709366  3.83945749  2.70473437  2.34109668 -1.982536    4.55006968
  2.32487668 -1.91812651  4.21769505 -1.2437192  -1.20781333 -1.67073473
  0.40846688  0.94627468  1.61377492  3.50924512  3.32501051 -0.80899006
  4.38871083  4.66051007  1.15018701  3.60467976  3.12660407  4.22538408
 -1.81379209  2.1627863  -1.88243428  1.30135871  1.10013978  2.50071349
  3.15812902  0.4785657   0.0362454  -0.77122499  2.39061806  4.21093057
  0.58792104 -1.81421365  3.90948953  0.75237699  2.10455222 -0.91724553
  3.62957224 -0.62426159  3.84193227  3.50501826  4.28065837  0.31423015
  1.37705043 -1.04345964 -1.26729065  3.82397924  0.28575911 -0.47583741
  1.21588883  1.07536132  2.28175763  1.55259446  2.32416492 -0.32716985
  3.02367369  3.56221802  3.26923544  3.47930336  0.0976459   3.48298969
  4.19733479  1.56584909 -0.74777953 -0.09606613  0.5185026   1.54149989
  1.68527384  3.57982406  4.13073086  4.62447488  1.02610429  0.56559754
  3.89733787 -1.52106627  4.30711104  3.49974279  0.86564778 -1.80419916
  4.43919857 -0.66795268 -1.22432278  2.85398578 -0.40576424  4.58006509
  0.23018428  4.24748741 -1.26509809  0.46309073 -1.17496722  3.48716149
  0.3602364  -0.49728289  3.52434707 -1.28620106  1.35889229  4.63890606
  4.50283818  4.96301463  0.39531205 -0.45425934  2.93744824 -1.89127113
  0.62448163  3.33437749  2.21693327 -0.64715955  1.41895322 -1.63199063
  3.12572361  4.43606788  1.94361289  1.45225518  1.48518027  0.02269611
  3.82761504  4.14563797 -1.77861794 -1.46884692  3.943601    4.16677748
  0.09777172  3.55218736 -1.8303875   0.06112043 -1.81745405  2.15887488
  4.99052199  1.22982198  2.39185509  0.21063392  2.47744229  4.58019045
 -0.98660092 -0.38313283 -0.88218342  2.34506504 -1.49154776  0.75598947
 -0.98470803  2.9782695   3.14501455  3.95744167  3.53920458  1.73530938
  2.70345577 -0.24751947  1.6885114   3.17292182  0.74009202  0.56904162
  4.35556513  1.34339051  2.94701763  0.15565139  0.21815829  0.07142765
  1.44353185  1.36499665 -0.61759184  4.59530984  4.81138286 -0.89572365
 -0.61896139  1.99650076 -1.54438496  0.48564188 -0.31570249 -1.73648016
 -0.69569907  0.83521097  0.6328362   4.44640203 -0.40579678  0.86578475
  4.41269431  0.21280814  2.43268396  4.53336063 -1.67359811  3.22033222
  0.02746641  0.2575197   4.17974066  0.79949443  3.82636379  4.64571801
 -1.24549326  0.65756483 -1.74377722  3.48519197]

Let’s also do the histogram of x_samples to make sure they are distributed the right way:

fig, ax = plt.subplots()
ax.plot(xs, X.pdf(xs), label="True PDF")
ax.hist(x_samples, density=True, alpha=0.25, label="Histogram",
        color=sns.color_palette()[0])
ax.set_xlabel("$x$")
ax.set_ylabel("$f_X(x)$")
plt.legend(loc="best")
sns.despine(trim=True);
../_images/fc9bb923c7c42617fead170b5718d7153af19a8dc7d46ad81cf1832aa721a075.svg

Questions#

  • Rerun the code above so that the random variable is \(U([1, 10])\).