update on week 37
This commit is contained in:
@@ -302,7 +302,7 @@ MathJax.Hub.Config({
|
||||
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
|
||||
<br>
|
||||
<p>
|
||||
<center><h4>Sep 16, 2021</h4></center> <!-- date -->
|
||||
<center><h4>Sep 17, 2021</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
|
||||
|
||||
@@ -302,37 +302,32 @@ theorem.
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%;"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">import</span> <span style="color: #666666">*</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">numpy.random</span> <span style="color: #008000; font-weight: bold">import</span> randint, randn
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%;"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">time</span> <span style="color: #008000; font-weight: bold">import</span> time
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.mlab</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">mlab</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">scipy.stats</span> <span style="color: #008000; font-weight: bold">import</span> norm
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Returns mean of bootstrap samples # Alternatively, we can run it using Scikit-Learn's function resample # See the examples below</span>
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">statistics</span>(data):
|
||||
<span style="color: #008000; font-weight: bold">return</span> mean(data)
|
||||
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Returns mean of bootstrap samples </span>
|
||||
<span style="color: #408080; font-style: italic"># Bootstrap algorithm</span>
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">bootstrap</span>(data, statistic, R):
|
||||
t <span style="color: #666666">=</span> zeros(R); n <span style="color: #666666">=</span> <span style="color: #008000">len</span>(data); inds <span style="color: #666666">=</span> arange(n); t0 <span style="color: #666666">=</span> time()
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">bootstrap</span>(data, datapoints):
|
||||
t <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros(datapoints)
|
||||
n <span style="color: #666666">=</span> <span style="color: #008000">len</span>(data)
|
||||
<span style="color: #408080; font-style: italic"># non-parametric bootstrap </span>
|
||||
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(R):
|
||||
t[i] <span style="color: #666666">=</span> statistic(data[randint(<span style="color: #666666">0</span>,n,n)])
|
||||
|
||||
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(datapoints):
|
||||
t[i] <span style="color: #666666">=</span> np<span style="color: #666666">.</span>mean(data[np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>randint(<span style="color: #666666">0</span>,n,n)])
|
||||
<span style="color: #408080; font-style: italic"># analysis </span>
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Runtime: </span><span style="color: #BB6688; font-weight: bold">%g</span><span style="color: #BA2121"> sec"</span> <span style="color: #666666">%</span> (time()<span style="color: #666666">-</span>t0)); <span style="color: #008000">print</span>(<span style="color: #BA2121">"Bootstrap Statistics :"</span>)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Bootstrap Statistics :"</span>)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"original bias std. error"</span>)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"</span><span style="color: #BB6688; font-weight: bold">%8g</span><span style="color: #BA2121"> </span><span style="color: #BB6688; font-weight: bold">%8g</span><span style="color: #BA2121"> </span><span style="color: #BB6688; font-weight: bold">%14g</span><span style="color: #BA2121"> </span><span style="color: #BB6688; font-weight: bold">%15g</span><span style="color: #BA2121">"</span> <span style="color: #666666">%</span> (statistic(data), std(data),mean(t),std(t)))
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"</span><span style="color: #BB6688; font-weight: bold">%8g</span><span style="color: #BA2121"> </span><span style="color: #BB6688; font-weight: bold">%8g</span><span style="color: #BA2121"> </span><span style="color: #BB6688; font-weight: bold">%14g</span><span style="color: #BA2121"> </span><span style="color: #BB6688; font-weight: bold">%15g</span><span style="color: #BA2121">"</span> <span style="color: #666666">%</span> (np<span style="color: #666666">.</span>mean(data), np<span style="color: #666666">.</span>std(data),np<span style="color: #666666">.</span>mean(t),np<span style="color: #666666">.</span>std(t)))
|
||||
<span style="color: #008000; font-weight: bold">return</span> t
|
||||
|
||||
|
||||
<span style="color: #408080; font-style: italic"># We set the mean value to 100 and the standard deviation to 15</span>
|
||||
mu, sigma <span style="color: #666666">=</span> <span style="color: #666666">100</span>, <span style="color: #666666">15</span>
|
||||
datapoints <span style="color: #666666">=</span> <span style="color: #666666">10000</span>
|
||||
x <span style="color: #666666">=</span> mu <span style="color: #666666">+</span> sigma<span style="color: #666666">*</span>random<span style="color: #666666">.</span>randn(datapoints)
|
||||
<span style="color: #408080; font-style: italic"># We generate random numbers according to the normal distribution</span>
|
||||
x <span style="color: #666666">=</span> mu <span style="color: #666666">+</span> sigma<span style="color: #666666">*</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>randn(datapoints)
|
||||
<span style="color: #408080; font-style: italic"># bootstrap returns the data sample </span>
|
||||
t <span style="color: #666666">=</span> bootstrap(x, statistics, datapoints)
|
||||
t <span style="color: #666666">=</span> bootstrap(x, datapoints)
|
||||
</pre></div>
|
||||
<p>
|
||||
We see that our new variance and from that the standard deviation, agrees with the central limit theorem.
|
||||
|
||||
@@ -287,17 +287,14 @@ MathJax.Hub.Config({
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%;"><span></span><span style="color: #408080; font-style: italic"># the histogram of the bootstrapped data </span>
|
||||
n, binsboot, patches <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>hist(t, <span style="color: #666666">50</span>, normed<span style="color: #666666">=1</span>, facecolor<span style="color: #666666">=</span><span style="color: #BA2121">'red'</span>, alpha<span style="color: #666666">=0.75</span>)
|
||||
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%;"><span></span><span style="color: #408080; font-style: italic"># the histogram of the bootstrapped data (normalized data if density = True)</span>
|
||||
n, binsboot, patches <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>hist(t, <span style="color: #666666">50</span>, density<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>, facecolor<span style="color: #666666">=</span><span style="color: #BA2121">'red'</span>, alpha<span style="color: #666666">=0.75</span>)
|
||||
<span style="color: #408080; font-style: italic"># add a 'best fit' line </span>
|
||||
y <span style="color: #666666">=</span> mlab<span style="color: #666666">.</span>normpdf( binsboot, mean(t), std(t))
|
||||
lt <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>plot(binsboot, y, <span style="color: #BA2121">'r--'</span>, linewidth<span style="color: #666666">=1</span>)
|
||||
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">'Smarts'</span>)
|
||||
y <span style="color: #666666">=</span> norm<span style="color: #666666">.</span>pdf(binsboot, np<span style="color: #666666">.</span>mean(t), np<span style="color: #666666">.</span>std(t))
|
||||
lt <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>plot(binsboot, y, <span style="color: #BA2121">'b'</span>, linewidth<span style="color: #666666">=1</span>)
|
||||
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">'x'</span>)
|
||||
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">'Probability'</span>)
|
||||
plt<span style="color: #666666">.</span>axis([<span style="color: #666666">99.5</span>, <span style="color: #666666">100.6</span>, <span style="color: #666666">0</span>, <span style="color: #666666">3.0</span>])
|
||||
plt<span style="color: #666666">.</span>grid(<span style="color: #008000; font-weight: bold">True</span>)
|
||||
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
</pre></div>
|
||||
<p>
|
||||
|
||||
@@ -302,7 +302,7 @@ MathJax.Hub.Config({
|
||||
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
|
||||
<br>
|
||||
<p>
|
||||
<center><h4>Sep 16, 2021</h4></center> <!-- date -->
|
||||
<center><h4>Sep 17, 2021</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
|
||||
|
||||
@@ -148,7 +148,7 @@ MathJax.Hub.Config({
|
||||
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
|
||||
<br>
|
||||
<p> <br>
|
||||
<center><h4>Sep 16, 2021</h4></center> <!-- date -->
|
||||
<center><h4>Sep 17, 2021</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
|
||||
@@ -1225,37 +1225,32 @@ theorem.
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%;"><span></span><span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">import</span> *
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">numpy.random</span> <span style="color: #8B008B; font-weight: bold">import</span> randint, randn
|
||||
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%;"><span></span><span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">time</span> <span style="color: #8B008B; font-weight: bold">import</span> time
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.mlab</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">mlab</span>
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">scipy.stats</span> <span style="color: #8B008B; font-weight: bold">import</span> norm
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
|
||||
|
||||
<span style="color: #228B22"># Returns mean of bootstrap samples # Alternatively, we can run it using Scikit-Learn's function resample # See the examples below</span>
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">statistics</span>(data):
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> mean(data)
|
||||
|
||||
|
||||
<span style="color: #228B22"># Returns mean of bootstrap samples </span>
|
||||
<span style="color: #228B22"># Bootstrap algorithm</span>
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">bootstrap</span>(data, statistic, R):
|
||||
t = zeros(R); n = <span style="color: #658b00">len</span>(data); inds = arange(n); t0 = time()
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">bootstrap</span>(data, datapoints):
|
||||
t = np.zeros(datapoints)
|
||||
n = <span style="color: #658b00">len</span>(data)
|
||||
<span style="color: #228B22"># non-parametric bootstrap </span>
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(R):
|
||||
t[i] = statistic(data[randint(<span style="color: #B452CD">0</span>,n,n)])
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(datapoints):
|
||||
t[i] = np.mean(data[np.random.randint(<span style="color: #B452CD">0</span>,n,n)])
|
||||
<span style="color: #228B22"># analysis </span>
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Runtime: %g sec"</span> % (time()-t0)); <span style="color: #658b00">print</span>(<span style="color: #CD5555">"Bootstrap Statistics :"</span>)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Bootstrap Statistics :"</span>)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"original bias std. error"</span>)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"%8g %8g %14g %15g"</span> % (statistic(data), std(data),mean(t),std(t)))
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"%8g %8g %14g %15g"</span> % (np.mean(data), np.std(data),np.mean(t),np.std(t)))
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> t
|
||||
|
||||
|
||||
<span style="color: #228B22"># We set the mean value to 100 and the standard deviation to 15</span>
|
||||
mu, sigma = <span style="color: #B452CD">100</span>, <span style="color: #B452CD">15</span>
|
||||
datapoints = <span style="color: #B452CD">10000</span>
|
||||
x = mu + sigma*random.randn(datapoints)
|
||||
<span style="color: #228B22"># We generate random numbers according to the normal distribution</span>
|
||||
x = mu + sigma*np.random.randn(datapoints)
|
||||
<span style="color: #228B22"># bootstrap returns the data sample </span>
|
||||
t = bootstrap(x, statistics, datapoints)
|
||||
t = bootstrap(x, datapoints)
|
||||
</pre></div>
|
||||
<p>
|
||||
We see that our new variance and from that the standard deviation, agrees with the central limit theorem.
|
||||
@@ -1267,17 +1262,14 @@ We see that our new variance and from that the standard deviation, agrees with t
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%;"><span></span><span style="color: #228B22"># the histogram of the bootstrapped data </span>
|
||||
n, binsboot, patches = plt.hist(t, <span style="color: #B452CD">50</span>, normed=<span style="color: #B452CD">1</span>, facecolor=<span style="color: #CD5555">'red'</span>, alpha=<span style="color: #B452CD">0.75</span>)
|
||||
|
||||
<div class="highlight" style="background: #eeeedd"><pre style="font-size: 80%; line-height: 125%;"><span></span><span style="color: #228B22"># the histogram of the bootstrapped data (normalized data if density = True)</span>
|
||||
n, binsboot, patches = plt.hist(t, <span style="color: #B452CD">50</span>, density=<span style="color: #8B008B; font-weight: bold">True</span>, facecolor=<span style="color: #CD5555">'red'</span>, alpha=<span style="color: #B452CD">0.75</span>)
|
||||
<span style="color: #228B22"># add a 'best fit' line </span>
|
||||
y = mlab.normpdf( binsboot, mean(t), std(t))
|
||||
lt = plt.plot(binsboot, y, <span style="color: #CD5555">'r--'</span>, linewidth=<span style="color: #B452CD">1</span>)
|
||||
plt.xlabel(<span style="color: #CD5555">'Smarts'</span>)
|
||||
y = norm.pdf(binsboot, np.mean(t), np.std(t))
|
||||
lt = plt.plot(binsboot, y, <span style="color: #CD5555">'b'</span>, linewidth=<span style="color: #B452CD">1</span>)
|
||||
plt.xlabel(<span style="color: #CD5555">'x'</span>)
|
||||
plt.ylabel(<span style="color: #CD5555">'Probability'</span>)
|
||||
plt.axis([<span style="color: #B452CD">99.5</span>, <span style="color: #B452CD">100.6</span>, <span style="color: #B452CD">0</span>, <span style="color: #B452CD">3.0</span>])
|
||||
plt.grid(<span style="color: #8B008B; font-weight: bold">True</span>)
|
||||
|
||||
plt.show()
|
||||
</pre></div>
|
||||
</section>
|
||||
|
||||
@@ -243,7 +243,7 @@ MathJax.Hub.Config({
|
||||
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
|
||||
<br>
|
||||
<p>
|
||||
<center><h4>Sep 16, 2021</h4></center> <!-- date -->
|
||||
<center><h4>Sep 17, 2021</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
@@ -1243,37 +1243,32 @@ theorem.
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%;"><span></span><span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">import</span> *
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">numpy.random</span> <span style="color: #8B008B; font-weight: bold">import</span> randint, randn
|
||||
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%;"><span></span><span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">numpy</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">np</span>
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">time</span> <span style="color: #8B008B; font-weight: bold">import</span> time
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.mlab</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">mlab</span>
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">scipy.stats</span> <span style="color: #8B008B; font-weight: bold">import</span> norm
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">matplotlib.pyplot</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">plt</span>
|
||||
|
||||
<span style="color: #228B22"># Returns mean of bootstrap samples # Alternatively, we can run it using Scikit-Learn's function resample # See the examples below</span>
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">statistics</span>(data):
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> mean(data)
|
||||
|
||||
|
||||
<span style="color: #228B22"># Returns mean of bootstrap samples </span>
|
||||
<span style="color: #228B22"># Bootstrap algorithm</span>
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">bootstrap</span>(data, statistic, R):
|
||||
t = zeros(R); n = <span style="color: #658b00">len</span>(data); inds = arange(n); t0 = time()
|
||||
<span style="color: #8B008B; font-weight: bold">def</span> <span style="color: #008b45">bootstrap</span>(data, datapoints):
|
||||
t = np.zeros(datapoints)
|
||||
n = <span style="color: #658b00">len</span>(data)
|
||||
<span style="color: #228B22"># non-parametric bootstrap </span>
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(R):
|
||||
t[i] = statistic(data[randint(<span style="color: #B452CD">0</span>,n,n)])
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(datapoints):
|
||||
t[i] = np.mean(data[np.random.randint(<span style="color: #B452CD">0</span>,n,n)])
|
||||
<span style="color: #228B22"># analysis </span>
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Runtime: %g sec"</span> % (time()-t0)); <span style="color: #658b00">print</span>(<span style="color: #CD5555">"Bootstrap Statistics :"</span>)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"Bootstrap Statistics :"</span>)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"original bias std. error"</span>)
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"%8g %8g %14g %15g"</span> % (statistic(data), std(data),mean(t),std(t)))
|
||||
<span style="color: #658b00">print</span>(<span style="color: #CD5555">"%8g %8g %14g %15g"</span> % (np.mean(data), np.std(data),np.mean(t),np.std(t)))
|
||||
<span style="color: #8B008B; font-weight: bold">return</span> t
|
||||
|
||||
|
||||
<span style="color: #228B22"># We set the mean value to 100 and the standard deviation to 15</span>
|
||||
mu, sigma = <span style="color: #B452CD">100</span>, <span style="color: #B452CD">15</span>
|
||||
datapoints = <span style="color: #B452CD">10000</span>
|
||||
x = mu + sigma*random.randn(datapoints)
|
||||
<span style="color: #228B22"># We generate random numbers according to the normal distribution</span>
|
||||
x = mu + sigma*np.random.randn(datapoints)
|
||||
<span style="color: #228B22"># bootstrap returns the data sample </span>
|
||||
t = bootstrap(x, statistics, datapoints)
|
||||
t = bootstrap(x, datapoints)
|
||||
</pre></div>
|
||||
<p>
|
||||
We see that our new variance and from that the standard deviation, agrees with the central limit theorem.
|
||||
@@ -1285,17 +1280,14 @@ We see that our new variance and from that the standard deviation, agrees with t
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%;"><span></span><span style="color: #228B22"># the histogram of the bootstrapped data </span>
|
||||
n, binsboot, patches = plt.hist(t, <span style="color: #B452CD">50</span>, normed=<span style="color: #B452CD">1</span>, facecolor=<span style="color: #CD5555">'red'</span>, alpha=<span style="color: #B452CD">0.75</span>)
|
||||
|
||||
<div class="highlight" style="background: #eeeedd"><pre style="line-height: 125%;"><span></span><span style="color: #228B22"># the histogram of the bootstrapped data (normalized data if density = True)</span>
|
||||
n, binsboot, patches = plt.hist(t, <span style="color: #B452CD">50</span>, density=<span style="color: #8B008B; font-weight: bold">True</span>, facecolor=<span style="color: #CD5555">'red'</span>, alpha=<span style="color: #B452CD">0.75</span>)
|
||||
<span style="color: #228B22"># add a 'best fit' line </span>
|
||||
y = mlab.normpdf( binsboot, mean(t), std(t))
|
||||
lt = plt.plot(binsboot, y, <span style="color: #CD5555">'r--'</span>, linewidth=<span style="color: #B452CD">1</span>)
|
||||
plt.xlabel(<span style="color: #CD5555">'Smarts'</span>)
|
||||
y = norm.pdf(binsboot, np.mean(t), np.std(t))
|
||||
lt = plt.plot(binsboot, y, <span style="color: #CD5555">'b'</span>, linewidth=<span style="color: #B452CD">1</span>)
|
||||
plt.xlabel(<span style="color: #CD5555">'x'</span>)
|
||||
plt.ylabel(<span style="color: #CD5555">'Probability'</span>)
|
||||
plt.axis([<span style="color: #B452CD">99.5</span>, <span style="color: #B452CD">100.6</span>, <span style="color: #B452CD">0</span>, <span style="color: #B452CD">3.0</span>])
|
||||
plt.grid(<span style="color: #8B008B; font-weight: bold">True</span>)
|
||||
|
||||
plt.show()
|
||||
</pre></div>
|
||||
<p>
|
||||
|
||||
@@ -248,7 +248,7 @@ MathJax.Hub.Config({
|
||||
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
|
||||
<br>
|
||||
<p>
|
||||
<center><h4>Sep 16, 2021</h4></center> <!-- date -->
|
||||
<center><h4>Sep 17, 2021</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
@@ -1248,37 +1248,32 @@ theorem.
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%;"><span></span><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">import</span> <span style="color: #666666">*</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">numpy.random</span> <span style="color: #008000; font-weight: bold">import</span> randint, randn
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%;"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">time</span> <span style="color: #008000; font-weight: bold">import</span> time
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.mlab</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">mlab</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">scipy.stats</span> <span style="color: #008000; font-weight: bold">import</span> norm
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">matplotlib.pyplot</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">plt</span>
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Returns mean of bootstrap samples # Alternatively, we can run it using Scikit-Learn's function resample # See the examples below</span>
|
||||
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">statistics</span>(data):
|
||||
<span style="color: #008000; font-weight: bold">return</span> mean(data)
|
||||
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Returns mean of bootstrap samples </span>
|
||||
<span style="color: #408080; font-style: italic"># Bootstrap algorithm</span>
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">bootstrap</span>(data, statistic, R):
|
||||
t <span style="color: #666666">=</span> zeros(R); n <span style="color: #666666">=</span> <span style="color: #008000">len</span>(data); inds <span style="color: #666666">=</span> arange(n); t0 <span style="color: #666666">=</span> time()
|
||||
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">bootstrap</span>(data, datapoints):
|
||||
t <span style="color: #666666">=</span> np<span style="color: #666666">.</span>zeros(datapoints)
|
||||
n <span style="color: #666666">=</span> <span style="color: #008000">len</span>(data)
|
||||
<span style="color: #408080; font-style: italic"># non-parametric bootstrap </span>
|
||||
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(R):
|
||||
t[i] <span style="color: #666666">=</span> statistic(data[randint(<span style="color: #666666">0</span>,n,n)])
|
||||
|
||||
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(datapoints):
|
||||
t[i] <span style="color: #666666">=</span> np<span style="color: #666666">.</span>mean(data[np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>randint(<span style="color: #666666">0</span>,n,n)])
|
||||
<span style="color: #408080; font-style: italic"># analysis </span>
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Runtime: </span><span style="color: #BB6688; font-weight: bold">%g</span><span style="color: #BA2121"> sec"</span> <span style="color: #666666">%</span> (time()<span style="color: #666666">-</span>t0)); <span style="color: #008000">print</span>(<span style="color: #BA2121">"Bootstrap Statistics :"</span>)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"Bootstrap Statistics :"</span>)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"original bias std. error"</span>)
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"</span><span style="color: #BB6688; font-weight: bold">%8g</span><span style="color: #BA2121"> </span><span style="color: #BB6688; font-weight: bold">%8g</span><span style="color: #BA2121"> </span><span style="color: #BB6688; font-weight: bold">%14g</span><span style="color: #BA2121"> </span><span style="color: #BB6688; font-weight: bold">%15g</span><span style="color: #BA2121">"</span> <span style="color: #666666">%</span> (statistic(data), std(data),mean(t),std(t)))
|
||||
<span style="color: #008000">print</span>(<span style="color: #BA2121">"</span><span style="color: #BB6688; font-weight: bold">%8g</span><span style="color: #BA2121"> </span><span style="color: #BB6688; font-weight: bold">%8g</span><span style="color: #BA2121"> </span><span style="color: #BB6688; font-weight: bold">%14g</span><span style="color: #BA2121"> </span><span style="color: #BB6688; font-weight: bold">%15g</span><span style="color: #BA2121">"</span> <span style="color: #666666">%</span> (np<span style="color: #666666">.</span>mean(data), np<span style="color: #666666">.</span>std(data),np<span style="color: #666666">.</span>mean(t),np<span style="color: #666666">.</span>std(t)))
|
||||
<span style="color: #008000; font-weight: bold">return</span> t
|
||||
|
||||
|
||||
<span style="color: #408080; font-style: italic"># We set the mean value to 100 and the standard deviation to 15</span>
|
||||
mu, sigma <span style="color: #666666">=</span> <span style="color: #666666">100</span>, <span style="color: #666666">15</span>
|
||||
datapoints <span style="color: #666666">=</span> <span style="color: #666666">10000</span>
|
||||
x <span style="color: #666666">=</span> mu <span style="color: #666666">+</span> sigma<span style="color: #666666">*</span>random<span style="color: #666666">.</span>randn(datapoints)
|
||||
<span style="color: #408080; font-style: italic"># We generate random numbers according to the normal distribution</span>
|
||||
x <span style="color: #666666">=</span> mu <span style="color: #666666">+</span> sigma<span style="color: #666666">*</span>np<span style="color: #666666">.</span>random<span style="color: #666666">.</span>randn(datapoints)
|
||||
<span style="color: #408080; font-style: italic"># bootstrap returns the data sample </span>
|
||||
t <span style="color: #666666">=</span> bootstrap(x, statistics, datapoints)
|
||||
t <span style="color: #666666">=</span> bootstrap(x, datapoints)
|
||||
</pre></div>
|
||||
<p>
|
||||
We see that our new variance and from that the standard deviation, agrees with the central limit theorem.
|
||||
@@ -1290,17 +1285,14 @@ We see that our new variance and from that the standard deviation, agrees with t
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%;"><span></span><span style="color: #408080; font-style: italic"># the histogram of the bootstrapped data </span>
|
||||
n, binsboot, patches <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>hist(t, <span style="color: #666666">50</span>, normed<span style="color: #666666">=1</span>, facecolor<span style="color: #666666">=</span><span style="color: #BA2121">'red'</span>, alpha<span style="color: #666666">=0.75</span>)
|
||||
|
||||
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%;"><span></span><span style="color: #408080; font-style: italic"># the histogram of the bootstrapped data (normalized data if density = True)</span>
|
||||
n, binsboot, patches <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>hist(t, <span style="color: #666666">50</span>, density<span style="color: #666666">=</span><span style="color: #008000; font-weight: bold">True</span>, facecolor<span style="color: #666666">=</span><span style="color: #BA2121">'red'</span>, alpha<span style="color: #666666">=0.75</span>)
|
||||
<span style="color: #408080; font-style: italic"># add a 'best fit' line </span>
|
||||
y <span style="color: #666666">=</span> mlab<span style="color: #666666">.</span>normpdf( binsboot, mean(t), std(t))
|
||||
lt <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>plot(binsboot, y, <span style="color: #BA2121">'r--'</span>, linewidth<span style="color: #666666">=1</span>)
|
||||
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">'Smarts'</span>)
|
||||
y <span style="color: #666666">=</span> norm<span style="color: #666666">.</span>pdf(binsboot, np<span style="color: #666666">.</span>mean(t), np<span style="color: #666666">.</span>std(t))
|
||||
lt <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>plot(binsboot, y, <span style="color: #BA2121">'b'</span>, linewidth<span style="color: #666666">=1</span>)
|
||||
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">'x'</span>)
|
||||
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">'Probability'</span>)
|
||||
plt<span style="color: #666666">.</span>axis([<span style="color: #666666">99.5</span>, <span style="color: #666666">100.6</span>, <span style="color: #666666">0</span>, <span style="color: #666666">3.0</span>])
|
||||
plt<span style="color: #666666">.</span>grid(<span style="color: #008000; font-weight: bold">True</span>)
|
||||
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
</pre></div>
|
||||
<p>
|
||||
|
||||
Binary file not shown.
@@ -10,7 +10,7 @@
|
||||
"<!-- Author: --> \n",
|
||||
"**Morten Hjorth-Jensen**, Department of Physics, University of Oslo and Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University\n",
|
||||
"\n",
|
||||
"Date: **Sep 16, 2021**\n",
|
||||
"Date: **Sep 17, 2021**\n",
|
||||
"\n",
|
||||
"Copyright 1999-2021, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license\n",
|
||||
"\n",
|
||||
@@ -1258,37 +1258,32 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from numpy import *\n",
|
||||
"from numpy.random import randint, randn\n",
|
||||
"import numpy as np\n",
|
||||
"from time import time\n",
|
||||
"import matplotlib.mlab as mlab\n",
|
||||
"from scipy.stats import norm\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"\n",
|
||||
"# Returns mean of bootstrap samples # Alternatively, we can run it using Scikit-Learn's function resample # See the examples below\n",
|
||||
"\n",
|
||||
"def statistics(data):\n",
|
||||
" return mean(data)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Returns mean of bootstrap samples \n",
|
||||
"# Bootstrap algorithm\n",
|
||||
"def bootstrap(data, statistic, R):\n",
|
||||
" t = zeros(R); n = len(data); inds = arange(n); t0 = time()\n",
|
||||
"def bootstrap(data, datapoints):\n",
|
||||
" t = np.zeros(datapoints)\n",
|
||||
" n = len(data)\n",
|
||||
" # non-parametric bootstrap \n",
|
||||
" for i in range(R):\n",
|
||||
" t[i] = statistic(data[randint(0,n,n)])\n",
|
||||
"\n",
|
||||
" for i in range(datapoints):\n",
|
||||
" t[i] = np.mean(data[np.random.randint(0,n,n)])\n",
|
||||
" # analysis \n",
|
||||
" print(\"Runtime: %g sec\" % (time()-t0)); print(\"Bootstrap Statistics :\")\n",
|
||||
" print(\"Bootstrap Statistics :\")\n",
|
||||
" print(\"original bias std. error\")\n",
|
||||
" print(\"%8g %8g %14g %15g\" % (statistic(data), std(data),mean(t),std(t)))\n",
|
||||
" print(\"%8g %8g %14g %15g\" % (np.mean(data), np.std(data),np.mean(t),np.std(t)))\n",
|
||||
" return t\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# We set the mean value to 100 and the standard deviation to 15\n",
|
||||
"mu, sigma = 100, 15\n",
|
||||
"datapoints = 10000\n",
|
||||
"x = mu + sigma*random.randn(datapoints)\n",
|
||||
"# We generate random numbers according to the normal distribution\n",
|
||||
"x = mu + sigma*np.random.randn(datapoints)\n",
|
||||
"# bootstrap returns the data sample \n",
|
||||
"t = bootstrap(x, statistics, datapoints)"
|
||||
"t = bootstrap(x, datapoints)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1309,17 +1304,14 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# the histogram of the bootstrapped data \n",
|
||||
"n, binsboot, patches = plt.hist(t, 50, normed=1, facecolor='red', alpha=0.75)\n",
|
||||
"\n",
|
||||
"# the histogram of the bootstrapped data (normalized data if density = True)\n",
|
||||
"n, binsboot, patches = plt.hist(t, 50, density=True, facecolor='red', alpha=0.75)\n",
|
||||
"# add a 'best fit' line \n",
|
||||
"y = mlab.normpdf( binsboot, mean(t), std(t))\n",
|
||||
"lt = plt.plot(binsboot, y, 'r--', linewidth=1)\n",
|
||||
"plt.xlabel('Smarts')\n",
|
||||
"y = norm.pdf(binsboot, np.mean(t), np.std(t))\n",
|
||||
"lt = plt.plot(binsboot, y, 'b', linewidth=1)\n",
|
||||
"plt.xlabel('x')\n",
|
||||
"plt.ylabel('Probability')\n",
|
||||
"plt.axis([99.5, 100.6, 0, 3.0])\n",
|
||||
"plt.grid(True)\n",
|
||||
"\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -910,56 +910,47 @@ theorem.
|
||||
|
||||
|
||||
!bc pycod
|
||||
from numpy import *
|
||||
from numpy.random import randint, randn
|
||||
import numpy as np
|
||||
from time import time
|
||||
import matplotlib.mlab as mlab
|
||||
from scipy.stats import norm
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
# Returns mean of bootstrap samples # Alternatively, we can run it using Scikit-Learn's function resample # See the examples below
|
||||
|
||||
def statistics(data):
|
||||
return mean(data)
|
||||
|
||||
|
||||
# Returns mean of bootstrap samples
|
||||
# Bootstrap algorithm
|
||||
def bootstrap(data, statistic, R):
|
||||
t = zeros(R); n = len(data); inds = arange(n); t0 = time()
|
||||
def bootstrap(data, datapoints):
|
||||
t = np.zeros(datapoints)
|
||||
n = len(data)
|
||||
# non-parametric bootstrap
|
||||
for i in range(R):
|
||||
t[i] = statistic(data[randint(0,n,n)])
|
||||
|
||||
for i in range(datapoints):
|
||||
t[i] = np.mean(data[np.random.randint(0,n,n)])
|
||||
# analysis
|
||||
print("Runtime: %g sec" % (time()-t0)); print("Bootstrap Statistics :")
|
||||
print("Bootstrap Statistics :")
|
||||
print("original bias std. error")
|
||||
print("%8g %8g %14g %15g" % (statistic(data), std(data),mean(t),std(t)))
|
||||
print("%8g %8g %14g %15g" % (np.mean(data), np.std(data),np.mean(t),np.std(t)))
|
||||
return t
|
||||
|
||||
|
||||
# We set the mean value to 100 and the standard deviation to 15
|
||||
mu, sigma = 100, 15
|
||||
datapoints = 10000
|
||||
x = mu + sigma*random.randn(datapoints)
|
||||
# We generate random numbers according to the normal distribution
|
||||
x = mu + sigma*np.random.randn(datapoints)
|
||||
# bootstrap returns the data sample
|
||||
t = bootstrap(x, statistics, datapoints)
|
||||
t = bootstrap(x, datapoints)
|
||||
!ec
|
||||
We see that our new variance and from that the standard deviation, agrees with the central limit theorem.
|
||||
|
||||
!split
|
||||
===== Plotting the Histogram =====
|
||||
!bc pycod
|
||||
# the histogram of the bootstrapped data
|
||||
n, binsboot, patches = plt.hist(t, 50, normed=1, facecolor='red', alpha=0.75)
|
||||
|
||||
# the histogram of the bootstrapped data (normalized data if density = True)
|
||||
n, binsboot, patches = plt.hist(t, 50, density=True, facecolor='red', alpha=0.75)
|
||||
# add a 'best fit' line
|
||||
y = mlab.normpdf( binsboot, mean(t), std(t))
|
||||
lt = plt.plot(binsboot, y, 'r--', linewidth=1)
|
||||
plt.xlabel('Smarts')
|
||||
y = norm.pdf(binsboot, np.mean(t), np.std(t))
|
||||
lt = plt.plot(binsboot, y, 'b', linewidth=1)
|
||||
plt.xlabel('x')
|
||||
plt.ylabel('Probability')
|
||||
plt.axis([99.5, 100.6, 0, 3.0])
|
||||
plt.grid(True)
|
||||
|
||||
plt.show()
|
||||
|
||||
!ec
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user