Skip to content

Commit e9c4260

Browse files
committed
Update NB 8.0 with f-strings
1 parent a3ee9c3 commit e9c4260

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

notebooks/8.0-Hypothesis-testing.ipynb

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@
160160
"cell_type": "markdown",
161161
"metadata": {},
162162
"source": [
163-
"Now define a model function based on Ohm's law and choose an initial guess for the resistance. The voltage uncertainty is too small to be appear on the `errorbar` plot, so we switch to the conventional `plot` command."
163+
"Now define a model function based on Ohm's law and choose an initial guess for the resistance. The voltage uncertainty is too small to appear on the `errorbar` plot, so we switch to the conventional `plot` command."
164164
]
165165
},
166166
{
@@ -170,14 +170,14 @@
170170
"outputs": [],
171171
"source": [
172172
"# Define model\n",
173-
"def vOhm(current, resistance):\n",
173+
"def v_ohm(current, resistance):\n",
174174
" return current*resistance\n",
175175
"\n",
176176
"# Assign initial guess for resistance and\n",
177177
"RInit = 100\n",
178178
"Imodel_mA = np.linspace(0,100)\n",
179179
"Imodel = 1e-3*Imodel_mA\n",
180-
"Vmodel = vOhm(Imodel, RInit)\n",
180+
"Vmodel = v_ohm(Imodel, RInit)\n",
181181
"plt.plot(I_mA, Vm, 'o')\n",
182182
"plt.plot(Imodel_mA, Vmodel, '-')\n",
183183
"plt.xlabel(\"Current (mA)\")\n",
@@ -197,10 +197,10 @@
197197
"metadata": {},
198198
"outputs": [],
199199
"source": [
200-
"R_fit, R_fit_cov = curve_fit(vOhm, I_mA*1e-3, Vm, p0=[RInit], \n",
200+
"R_fit, R_fit_cov = curve_fit(v_ohm, I_mA*1e-3, Vm, p0=[RInit], \n",
201201
" sigma=alphaV*np.ones(np.size(Vm)), absolute_sigma=True)\n",
202202
"alpha_R = np.sqrt(R_fit_cov[0,0])\n",
203-
"print(\"R = {0:.3f} ± {1:.3f} ohms\".format(R_fit[0], alpha_R))"
203+
"print(f\"R = {R_fit[0]:.3f} ± {alpha_R:.3f} ohms\")"
204204
]
205205
},
206206
{
@@ -217,10 +217,10 @@
217217
"outputs": [],
218218
"source": [
219219
"# Compute and display chi-squared\n",
220-
"res = Vm - vOhm(I_mA*1e-3, R_fit)\n",
220+
"res = Vm - v_ohm(I_mA*1e-3, R_fit)\n",
221221
"normres = res/alphaV\n",
222222
"chisq = np.sum(normres**2)\n",
223-
"print(\"chisq = {0:.1f}\".format(chisq))"
223+
"print(f\"chisq = {chisq:.1f}\")"
224224
]
225225
},
226226
{
@@ -241,8 +241,8 @@
241241
"# Compute the probability of getting this fit result\n",
242242
"dof = 10\n",
243243
"cdf = chi2.cdf(chisq, dof)\n",
244-
"print(\"Cumulative probability = {0:.6f}\".format(cdf))\n",
245-
"print(\"Significance: {0:.6f}\".format(1-cdf))"
244+
"print(f\"Cumulative probability = {cdf:.6f}\")\n",
245+
"print(f\"Significance: {1-cdf:.6f}\")"
246246
]
247247
},
248248
{
@@ -279,18 +279,18 @@
279279
"outputs": [],
280280
"source": [
281281
"# Define new model\n",
282-
"def vOhm_offset(current, resistance, offset):\n",
282+
"def v_ohm_offset(current, resistance, offset):\n",
283283
" return current*resistance + offset\n",
284284
"\n",
285285
"VoffInit = 0\n",
286-
"p_fit, p_fit_cov = curve_fit(vOhm_offset, I_mA*1e-3, Vm, p0=[RInit,VoffInit], \n",
286+
"p_fit, p_fit_cov = curve_fit(v_ohm_offset, I_mA*1e-3, Vm, p0=[RInit,VoffInit], \n",
287287
" sigma=alphaV*np.ones(np.size(Vm)), absolute_sigma=True)\n",
288288
"R_fit = p_fit[0]\n",
289289
"Voff_fit = p_fit[1]\n",
290290
"alpha_R = np.sqrt(p_fit_cov[0,0])\n",
291291
"alpha_Voff = np.sqrt(p_fit_cov[1,1])\n",
292-
"print(\"R = {0:.3f} ± {1:.3f} ohms\".format(R_fit, alpha_R))\n",
293-
"print(\"Voff = {0:.3f} ± {1:.3f} V\".format(Voff_fit, alpha_Voff))"
292+
"print(f\"R = {R_fit:.3f} ± {alpha_R:.3f} ohms\")\n",
293+
"print(f\"Voff = {Voff_fit:.3f} ± {alpha_Voff:.3f} V\")"
294294
]
295295
},
296296
{
@@ -307,16 +307,16 @@
307307
"outputs": [],
308308
"source": [
309309
"# Compute and display chi-squared\n",
310-
"res_offset = Vm - vOhm_offset(I_mA*1e-3, R_fit, Voff_fit)\n",
310+
"res_offset = Vm - v_ohm_offset(I_mA*1e-3, R_fit, Voff_fit)\n",
311311
"normres_offset = res_offset/alphaV\n",
312312
"chisq_offset = np.sum(normres_offset**2)\n",
313-
"print(\"chisq_offset = {0:.1f}\".format(chisq_offset))\n",
313+
"print(f\"chisq_offset = {chisq_offset:.1f}\")\n",
314314
"\n",
315315
"# Compute the probability of getting this fit result\n",
316316
"dof_offset = 9\n",
317317
"cdf_offset = chi2.cdf(chisq_offset, dof_offset)\n",
318-
"print(\"Cumulative probability = {0:.6f}\".format(cdf_offset))\n",
319-
"print(\"Significance: {0:.6f}\".format(1-cdf_offset))"
318+
"print(f\"Cumulative probability = {cdf_offset:.6f}\")\n",
319+
"print(f\"Significance: {1-cdf_offset:.6f}\")"
320320
]
321321
},
322322
{
@@ -466,7 +466,7 @@
466466
"g_fit, g_fit_cov = curve_fit(t_fall, d_cm*1e-2, tm, p0=[gInit], \n",
467467
" sigma=alpha_t_est*np.ones(np.size(tm)), absolute_sigma=True)\n",
468468
"alpha_g = np.sqrt(g_fit_cov[0,0])\n",
469-
"print(\"g = {0:.2f} ± {1:.2f} m/s^2\".format(g_fit[0], alpha_g))"
469+
"print(f\"g = {g_fit[0]:.2f} ± {alpha_g:.2f} m/s^2\")"
470470
]
471471
},
472472
{
@@ -486,7 +486,7 @@
486486
"res = tm - t_fall(d_cm*1e-2, g_fit)\n",
487487
"normres = res/alpha_t_est\n",
488488
"chisq = np.sum(normres**2)\n",
489-
"print(\"chisq = {0:.1f}\".format(chisq))"
489+
"print(f\"chisq = {chisq:.1f}\")"
490490
]
491491
},
492492
{
@@ -508,9 +508,9 @@
508508
"dof = 9\n",
509509
"cdf = chi2.cdf(chisq, dof)\n",
510510
"sig = chi2.sf(chisq, dof)\n",
511-
"print(\"Cumulative probability = {0:.6f}\".format(cdf))\n",
512-
"print(\"Significance (1-cdf method): {0:.6f}\".format(1 - cdf))\n",
513-
"print(\"Significance (sf method): {0:.6e}\".format(sig))"
511+
"print(f\"Cumulative probability = {cdf:.6f}\")\n",
512+
"print(f\"Significance (1-cdf method): {1 - cdf:.6f}\")\n",
513+
"print(f\"Significance (sf method): {sig:.6e}\")"
514514
]
515515
},
516516
{
@@ -543,7 +543,7 @@
543543
"$$\n",
544544
"S = \\sqrt{\\frac{\\chi^2_\\text{min}}{\\nu}} = \\sqrt{\\chi^2_\\nu},\n",
545545
"$$\n",
546-
"so that the resulting $\\chi'^2_\\text{min} = S\\chi^2_\\text{min}$ matches the number of degrees of freedom exactly. Of course you forfeit the ability to use $\\chi'^2_\\text{min}$ as evidence of fit quality when you do this, so it is usually preferable to just go back and do a more careful measurement of the noise. This is definitely the case in this example, where $\\chi^2_\\text{min}$ indicates such a poor fit.\n",
546+
"so that the resulting $\\chi'^2_\\text{min} = S\\chi^2_\\text{min}$ matches the number of degrees of freedom exactly. Of course, you forfeit the ability to use $\\chi'^2_\\text{min}$ as evidence of fit quality when you do this, so it is usually preferable to just go back and do a more careful measurement of the noise. This is definitely the case in this example, where $\\chi^2_\\text{min}$ indicates such a poor fit.\n",
547547
"\n",
548548
"When $S\\approx 1$, though, it may be advantageous to scale the residuals to improve the accuracy of the *parameter uncertainty,* since the parameter uncertainty estimate given by `curve_fit` depends on $\\chi^2_\\text{min}$. The following code cell computes the scale factor for our example, then uses this to revise $\\alpha_g$. Another way to achieve the same result is to simply set `absolute_sigma=False` in the `curve_fit` algorithm—this will compute the fit by treating the values assigned to `sigma` as *relative uncertainties*, with an overall scale factor given by $S$ above. The defaults are `absolute_sigma=False` and `sigma` set to a vector of ones with the same size as the `y` data, so that when both `sigma` and `absolute_sigma` are omitted, `curve_fit` minimizes\n",
549549
"\n",
@@ -560,27 +560,27 @@
560560
"source": [
561561
"# Determine the scaling factor\n",
562562
"S = np.sqrt(chisq/dof)\n",
563-
"print(\"Scale factor: {0:.3f}\".format(S))\n",
563+
"print(f\"Scale factor: {S:.3f}\")\n",
564564
"\n",
565565
"# Scale measurement uncertainty by S\n",
566-
"print(\"S*alpha_t_est = {0:.2f} ms\".format(1e3*S*alpha_t_est))\n",
566+
"print(f\"S*alpha_t_est = {1e3*S*alpha_t_est:.2f} ms\")\n",
567567
"\n",
568568
"# Compare with true measurement uncertainty\n",
569-
"print(\"True alpha_t = {0:.2f}\".format(1e3*alpha_t))\n",
569+
"print(f\"True alpha_t = {1e3*alpha_t:.2f}\")\n",
570570
"\n",
571571
"# Scale parameter uncertainty by S\n",
572-
"print(\"g = {0:.2f} ± {1:.2f} m/s^2 (scaled uncertainty)\".format(g_fit[0], S*alpha_g))\n",
572+
"print(f\"g = ({g_fit[0]:.2f} ± {S*alpha_g:.2f}) m/s^2 (scaled uncertainty)\")\n",
573573
"\n",
574574
"# Repeat fit with absolute_sigma=False\n",
575575
"g_fit_scaled, g_fit_cov_scaled = curve_fit(t_fall, d_cm*1e-2, tm, p0=[gInit], \n",
576576
" sigma=alpha_t_est*np.ones(np.size(tm)), absolute_sigma=False)\n",
577577
"alpha_g_scaled = np.sqrt(g_fit_cov_scaled[0,0])\n",
578-
"print(\"g = {0:.2f} ± {1:.2f} m/s^2 (absolute_sigma=False)\".format(g_fit_scaled[0], alpha_g_scaled))\n",
578+
"print(f\"g = ({g_fit_scaled[0]:.2f} ± {alpha_g_scaled:.2f}) m/s^2 (absolute_sigma=False)\")\n",
579579
"\n",
580580
"# Repeat fit without sigma or absolute_sigma keywords\n",
581581
"g_fit_nosig, g_fit_cov_nosig = curve_fit(t_fall, d_cm*1e-2, tm, p0=[gInit])\n",
582582
"alpha_g_nosig = np.sqrt(g_fit_cov_nosig[0,0])\n",
583-
"print(\"g = {0:.2f} ± {1:.2f} m/s^2 (no sigma)\".format(g_fit_nosig[0], alpha_g_nosig))"
583+
"print(f\"g = ({g_fit_nosig[0]:.2f} ± {alpha_g_nosig:.2f}) m/s^2 (no sigma)\")"
584584
]
585585
},
586586
{

0 commit comments

Comments
 (0)