Variance (as an expectation)

import numpy as np
import matplotlib.pyplot as plt
def bernoulli_density(x, p = 0.5):
    return p ** x * (1 - p) ** (1 - x)
bernoulli_density(0, p = 0.27)
0.73
bernoulli_density(1, p = 0.27)
0.27
rng = np.random.default_rng()
x = rng.binomial(1, 0.27, size = 10)
bernoulli_density(x, p = 0.27)
array([0.73, 0.73, 0.73, 0.27, 0.27, 0.73, 0.27, 0.73, 0.73, 0.73])
x = np.array([0, 1])
p = 0.33
y = bernoulli_density(x, p = p)
plt.scatter(x, y)

N = 100000
R = 10100
x = rng.binomial(1, p, size = (R, N))
ndx = np.arange(1, N + 1)
cm = np.cumsum(x, axis = 1) / ndx
plt.plot(cm.T);
plt.axhline(p, color = "black", linestyle = "--");
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
Cell In[8], line 1
----> 1 plt.plot(cm.T);
      2 plt.axhline(p, color = "black", linestyle = "--");

File ~/venvs/py3/lib/python3.12/site-packages/matplotlib/pyplot.py:3838, in plot(scalex, scaley, data, *args, **kwargs)
   3830 @_copy_docstring_and_deprecators(Axes.plot)
   3831 def plot(
   3832     *args: float | ArrayLike | str,
   (...)
   3836     **kwargs,
   3837 ) -> list[Line2D]:
-> 3838     return gca().plot(
   3839         *args,
   3840         scalex=scalex,
   3841         scaley=scaley,
   3842         **({"data": data} if data is not None else {}),
   3843         **kwargs,
   3844     )

File ~/venvs/py3/lib/python3.12/site-packages/matplotlib/axes/_axes.py:1779, in Axes.plot(self, scalex, scaley, data, *args, **kwargs)
   1777 lines = [*self._get_lines(self, *args, data=data, **kwargs)]
   1778 for line in lines:
-> 1779     self.add_line(line)
   1780 if scalex:
   1781     self._request_autoscale_view("x")

File ~/venvs/py3/lib/python3.12/site-packages/matplotlib/axes/_base.py:2417, in _AxesBase.add_line(self, line)
   2414 if line.get_clip_path() is None:
   2415     line.set_clip_path(self.patch)
-> 2417 self._update_line_limits(line)
   2418 if not line.get_label():
   2419     line.set_label(f'_child{len(self._children)}')

File ~/venvs/py3/lib/python3.12/site-packages/matplotlib/axes/_base.py:2440, in _AxesBase._update_line_limits(self, line)
   2436 def _update_line_limits(self, line):
   2437     """
   2438     Figures out the data limit of the given line, updating `.Axes.dataLim`.
   2439     """
-> 2440     path = line.get_path()
   2441     if path.vertices.size == 0:
   2442         return

File ~/venvs/py3/lib/python3.12/site-packages/matplotlib/lines.py:1052, in Line2D.get_path(self)
   1050 """Return the `~matplotlib.path.Path` associated with this line."""
   1051 if self._invalidy or self._invalidx:
-> 1052     self.recache()
   1053 return self._path

File ~/venvs/py3/lib/python3.12/site-packages/matplotlib/lines.py:698, in Line2D.recache(self, always)
    695 else:
    696     y = self._y
--> 698 self._xy = np.column_stack(np.broadcast_arrays(x, y)).astype(float)
    699 self._x, self._y = self._xy.T  # views
    701 self._subslice = False

File ~/venvs/py3/lib/python3.12/site-packages/numpy/lib/shape_base.py:652, in column_stack(tup)
    650         arr = array(arr, copy=False, subok=True, ndmin=2).T
    651     arrays.append(arr)
--> 652 return _nx.concatenate(arrays, 1)

KeyboardInterrupt: 
Error in callback <function _draw_all_if_interactive at 0x107b70e00> (for post_execute), with arguments args (),kwargs {}:
Exception ignored in: <bound method IPythonKernel._clean_thread_parent_frames of <ipykernel.ipkernel.IPythonKernel object at 0x103f89df0>>
Traceback (most recent call last):
  File "/Users/edward/venvs/py3/lib/python3.12/site-packages/ipykernel/ipkernel.py", line 770, in _clean_thread_parent_frames
KeyboardInterrupt: 

KeyboardInterrupt
Error in callback <function flush_figures at 0x107b8b920> (for post_execute), with arguments args (),kwargs {}:
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
File ~/venvs/py3/lib/python3.12/site-packages/matplotlib_inline/backend_inline.py:126, in flush_figures()
    123 if InlineBackend.instance().close_figures:
    124     # ignore the tracking, just draw and close all figures
    125     try:
--> 126         return show(True)
    127     except Exception as e:
    128         # safely show traceback if in IPython, else raise
    129         ip = get_ipython()

File ~/venvs/py3/lib/python3.12/site-packages/matplotlib_inline/backend_inline.py:90, in show(close, block)
     88 try:
     89     for figure_manager in Gcf.get_all_fig_managers():
---> 90         display(
     91             figure_manager.canvas.figure,
     92             metadata=_fetch_figure_metadata(figure_manager.canvas.figure)
     93         )
     94 finally:
     95     show._to_draw = []

File ~/venvs/py3/lib/python3.12/site-packages/IPython/core/display_functions.py:298, in display(include, exclude, metadata, transient, display_id, raw, clear, *objs, **kwargs)
    296     publish_display_data(data=obj, metadata=metadata, **kwargs)
    297 else:
--> 298     format_dict, md_dict = format(obj, include=include, exclude=exclude)
    299     if not format_dict:
    300         # nothing to display (e.g. _ipython_display_ took over)
    301         continue

File ~/venvs/py3/lib/python3.12/site-packages/IPython/core/formatters.py:182, in DisplayFormatter.format(self, obj, include, exclude)
    180 md = None
    181 try:
--> 182     data = formatter(obj)
    183 except:
    184     # FIXME: log the exception
    185     raise

File ~/venvs/py3/lib/python3.12/site-packages/decorator.py:232, in decorate.<locals>.fun(*args, **kw)
    230 if not kwsyntax:
    231     args, kw = fix(args, kw, sig)
--> 232 return caller(func, *(extras + args), **kw)

File ~/venvs/py3/lib/python3.12/site-packages/IPython/core/formatters.py:226, in catch_format_error(method, self, *args, **kwargs)
    224 """show traceback on failed format call"""
    225 try:
--> 226     r = method(self, *args, **kwargs)
    227 except NotImplementedError:
    228     # don't warn on NotImplementedErrors
    229     return self._check_return(None, args[0])

File ~/venvs/py3/lib/python3.12/site-packages/IPython/core/formatters.py:343, in BaseFormatter.__call__(self, obj)
    341     pass
    342 else:
--> 343     return printer(obj)
    344 # Finally look for special method names
    345 method = get_real_method(obj, self.print_method)

File ~/venvs/py3/lib/python3.12/site-packages/IPython/core/pylabtools.py:152, in print_figure(fig, fmt, bbox_inches, base64, **kwargs)
    149     from matplotlib.backend_bases import FigureCanvasBase
    150     FigureCanvasBase(fig)
--> 152 fig.canvas.print_figure(bytes_io, **kw)
    153 data = bytes_io.getvalue()
    154 if fmt == 'svg':

File ~/venvs/py3/lib/python3.12/site-packages/matplotlib/backend_bases.py:2157, in FigureCanvasBase.print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, pad_inches, bbox_extra_artists, backend, **kwargs)
   2154     # we do this instead of `self.figure.draw_without_rendering`
   2155     # so that we can inject the orientation
   2156     with getattr(renderer, "_draw_disabled", nullcontext)():
-> 2157         self.figure.draw(renderer)
   2158 if bbox_inches:
   2159     if bbox_inches == "tight":

File ~/venvs/py3/lib/python3.12/site-packages/matplotlib/artist.py:94, in _finalize_rasterization.<locals>.draw_wrapper(artist, renderer, *args, **kwargs)
     92 @wraps(draw)
     93 def draw_wrapper(artist, renderer, *args, **kwargs):
---> 94     result = draw(artist, renderer, *args, **kwargs)
     95     if renderer._rasterizing:
     96         renderer.stop_rasterizing()

File ~/venvs/py3/lib/python3.12/site-packages/matplotlib/artist.py:71, in allow_rasterization.<locals>.draw_wrapper(artist, renderer)
     68     if artist.get_agg_filter() is not None:
     69         renderer.start_filter()
---> 71     return draw(artist, renderer)
     72 finally:
     73     if artist.get_agg_filter() is not None:

File ~/venvs/py3/lib/python3.12/site-packages/matplotlib/figure.py:3257, in Figure.draw(self, renderer)
   3254             # ValueError can occur when resizing a window.
   3256     self.patch.draw(renderer)
-> 3257     mimage._draw_list_compositing_images(
   3258         renderer, self, artists, self.suppressComposite)
   3260     renderer.close_group('figure')
   3261 finally:

File ~/venvs/py3/lib/python3.12/site-packages/matplotlib/image.py:134, in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
    132 if not_composite or not has_images:
    133     for a in artists:
--> 134         a.draw(renderer)
    135 else:
    136     # Composite any adjacent images together
    137     image_group = []

File ~/venvs/py3/lib/python3.12/site-packages/matplotlib/artist.py:71, in allow_rasterization.<locals>.draw_wrapper(artist, renderer)
     68     if artist.get_agg_filter() is not None:
     69         renderer.start_filter()
---> 71     return draw(artist, renderer)
     72 finally:
     73     if artist.get_agg_filter() is not None:

File ~/venvs/py3/lib/python3.12/site-packages/matplotlib/axes/_base.py:3226, in _AxesBase.draw(self, renderer)
   3223 if artists_rasterized:
   3224     _draw_rasterized(self.get_figure(root=True), artists_rasterized, renderer)
-> 3226 mimage._draw_list_compositing_images(
   3227     renderer, self, artists, self.get_figure(root=True).suppressComposite)
   3229 renderer.close_group('axes')
   3230 self.stale = False

File ~/venvs/py3/lib/python3.12/site-packages/matplotlib/image.py:134, in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
    132 if not_composite or not has_images:
    133     for a in artists:
--> 134         a.draw(renderer)
    135 else:
    136     # Composite any adjacent images together
    137     image_group = []

File ~/venvs/py3/lib/python3.12/site-packages/matplotlib/artist.py:71, in allow_rasterization.<locals>.draw_wrapper(artist, renderer)
     68     if artist.get_agg_filter() is not None:
     69         renderer.start_filter()
---> 71     return draw(artist, renderer)
     72 finally:
     73     if artist.get_agg_filter() is not None:

File ~/venvs/py3/lib/python3.12/site-packages/matplotlib/lines.py:770, in Line2D.draw(self, renderer)
    768 if self._subslice and self.axes:
    769     x0, x1 = self.axes.get_xbound()
--> 770     i0 = self._x_filled.searchsorted(x0, 'left')
    771     i1 = self._x_filled.searchsorted(x1, 'right')
    772     subslice = slice(max(i0 - 1, 0), i1 + 1)

KeyboardInterrupt: 
np.shape(cm)
plt.hist(cm[:, 800]);
rng = np.random.default_rng()
N = 1000
p = 0.11
x = rng.binomial(1, p, size = N)
phat = np.mean(x) # close to p
v = p * (1 - p) # variance of distribution
np.mean((x - phat) ** 2) # variance of data
0.09711900000000001
y = (x - phat) ** 2
np.mean(y)
0.09711900000000001
ndy = np.arange(1, N + 1)
cm = np.cumsum(y) / ndy
plt.plot(ndy, cm);
plt.axhline(v, color = "black", linestyle = "--");