Example 1
Let X equal the length of life of a 60-watt light bulb marketed by a certain manufacturer. Assume that the distribution of X is $N(\mu, 1296)$. If a random sample of n = 27 bulbs is tested until they burn out, yielding a sample mean of x = 1478 hours, find 95% confidence interval for $\mu$.
Solution:
Given
$\sigma^2 = 1296 \therefore \sigma = 36$,
$x = 1478$, $1-\alpha = 0.95$,
$z_{\frac{\alpha}{2}} = z_{0.025} = 1.96$, $n = 27 \geq 5$
%load_ext tikzmagic
preamble = '''
\pgfmathdeclarefunction{gauss}{3}{%
\pgfmathparse{1/(#3*sqrt(2*pi))*exp(-((#1-#2)^2)/(2*#3^2))}%
}
'''
%%tikz -p pgfplots -x $preamble
% had to be this size to have a normal size in latex
\begin{axis}[
no markers,
domain=0:6,
samples=100,
ymin=0,
axis lines*=left,
xlabel=$x$,
ylabel=$f(x)$,
height=5cm,
width=12cm,
xtick=\empty,
ytick=\empty,
enlargelimits=false,
clip=false,
axis on top,
grid = major,
axis lines = middle
]
\def\mean{3}
\def\sd{1}
\def\cilow{\mean - 1.96*\sd}
\def\cihigh{\mean + 1.96*\sd}
\addplot [draw=none, fill=yellow!25, domain=\cilow:\cihigh] {gauss(x, \mean, \sd)} \closedcycle;
\addplot [very thick,cyan!50!black] {gauss(x, 3, 1)};
\pgfmathsetmacro\valueA{gauss(1,\mean,\sd)}
\draw [gray] (axis cs:\cilow,0) -- (axis cs:\cilow,\valueA)
(axis cs:\cihigh,0) -- (axis cs:\cihigh,\valueA);
\draw [yshift=0.3cm, latex-latex](axis cs:\cilow, 0) -- node [above] {Area = $0.95$} (axis cs:\cihigh, 0);
\node[below] at (axis cs:\cilow, 0) {$\mu - 1.96\sigma$};
\node[below] at (axis cs:\mean, 0) {$\mu$};
\node[below] at (axis cs:\cihigh, 0) {$\mu + 1.96\sigma$};
\end{axis}
Thus the 95% CI intervals are $[1464.42,1491.58]$. This does not mean, $\mu$ is inside this interval 95% of the time. But simply, if we are to take many such samples and their CIs, 95% of those CIs would contain $\mu$. We do not know what those CIs would be because we do not know the real $\mu$.
just testing if matplotlib output is reduced as well in size in latex..
%matplotlib inline
import matplotlib.pyplot as plt
X = [1,2,3,4,5]
Y = [2,4,6,8,2]
plt.plot(X,Y)
plt.show()