diff --git a/doc/pub/week37/html/._week37-bs000.html b/doc/pub/week37/html/._week37-bs000.html index a875d46ec..f984145ed 100644 --- a/doc/pub/week37/html/._week37-bs000.html +++ b/doc/pub/week37/html/._week37-bs000.html @@ -302,7 +302,7 @@ MathJax.Hub.Config({
[2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

-

Sep 16, 2021

+

Sep 17, 2021


diff --git a/doc/pub/week37/html/._week37-bs035.html b/doc/pub/week37/html/._week37-bs035.html index e462a453f..e57db43f3 100644 --- a/doc/pub/week37/html/._week37-bs035.html +++ b/doc/pub/week37/html/._week37-bs035.html @@ -302,37 +302,32 @@ theorem.

-

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)
 

We see that our new variance and from that the standard deviation, agrees with the central limit theorem. diff --git a/doc/pub/week37/html/._week37-bs036.html b/doc/pub/week37/html/._week37-bs036.html index dc3ca707e..8fd1c6a99 100644 --- a/doc/pub/week37/html/._week37-bs036.html +++ b/doc/pub/week37/html/._week37-bs036.html @@ -287,17 +287,14 @@ MathJax.Hub.Config({

-

# 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()
 

diff --git a/doc/pub/week37/html/week37-bs.html b/doc/pub/week37/html/week37-bs.html index a875d46ec..f984145ed 100644 --- a/doc/pub/week37/html/week37-bs.html +++ b/doc/pub/week37/html/week37-bs.html @@ -302,7 +302,7 @@ MathJax.Hub.Config({

[2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

-

Sep 16, 2021

+

Sep 17, 2021


diff --git a/doc/pub/week37/html/week37-reveal.html b/doc/pub/week37/html/week37-reveal.html index f48a11faa..3000dce08 100644 --- a/doc/pub/week37/html/week37-reveal.html +++ b/doc/pub/week37/html/week37-reveal.html @@ -148,7 +148,7 @@ MathJax.Hub.Config({

[2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

 
-

Sep 16, 2021

+

Sep 17, 2021


@@ -1225,37 +1225,32 @@ theorem.

-

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)
 

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

-

# 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()
 
diff --git a/doc/pub/week37/html/week37-solarized.html b/doc/pub/week37/html/week37-solarized.html index 2178da4cf..92297bafd 100644 --- a/doc/pub/week37/html/week37-solarized.html +++ b/doc/pub/week37/html/week37-solarized.html @@ -243,7 +243,7 @@ MathJax.Hub.Config({
[2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

-

Sep 16, 2021

+

Sep 17, 2021












@@ -1243,37 +1243,32 @@ theorem.

-

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)
 

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

-

# 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()
 

diff --git a/doc/pub/week37/html/week37.html b/doc/pub/week37/html/week37.html index 9c67887f6..5e7a20f6f 100644 --- a/doc/pub/week37/html/week37.html +++ b/doc/pub/week37/html/week37.html @@ -248,7 +248,7 @@ MathJax.Hub.Config({

[2] Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University

-

Sep 16, 2021

+

Sep 17, 2021












@@ -1248,37 +1248,32 @@ theorem.

-

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)
 

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

-

# 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()
 

diff --git a/doc/pub/week37/ipynb/ipynb-week37-src.tar.gz b/doc/pub/week37/ipynb/ipynb-week37-src.tar.gz index 5f1c144d1..9a14287c6 100644 Binary files a/doc/pub/week37/ipynb/ipynb-week37-src.tar.gz and b/doc/pub/week37/ipynb/ipynb-week37-src.tar.gz differ diff --git a/doc/pub/week37/ipynb/week37.ipynb b/doc/pub/week37/ipynb/week37.ipynb index 65168ab5c..ea7af2c3b 100644 --- a/doc/pub/week37/ipynb/week37.ipynb +++ b/doc/pub/week37/ipynb/week37.ipynb @@ -10,7 +10,7 @@ " \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()" ] }, diff --git a/doc/src/week37/week37.do.txt b/doc/src/week37/week37.do.txt index ab8b7e895..e3b9e3bd1 100644 --- a/doc/src/week37/week37.do.txt +++ b/doc/src/week37/week37.do.txt @@ -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