|
68 | 68 | "\\chi^2(m,c;\\{x_k\\},\\{y_k\\},\\{\\alpha_k\\}) = \\sum_{k=1}^N\\frac{[y_k - (mx_k + c)]^2}{\\alpha_k^2} \\label{eq:lsq}\\tag{1}\n", |
69 | 69 | "$$\n", |
70 | 70 | "\n", |
71 | | - "with respect to the parameters $m$ and $c$. We use a semicolon to emphasize the distinction between the *model parameters*, $m$ and $c$, and the *measurements*, $\\{x_k\\},\\{y_k\\}$, and $\\{\\alpha_k\\}$. Typically we are interested only in the functional dependence of $\\chi^2$ on the model parameters, holding the measurement variables fixed." |
| 71 | + "with respect to the parameters $m$ and $c$. We use a semicolon to emphasize the distinction between the *model parameters*, $m$ and $c$, and the *measurements*, $\\{x_k\\},\\{y_k\\}$, and $\\{\\alpha_k\\}$. Typically, we are interested only in the functional dependence of $\\chi^2$ on the model parameters, holding the measurement variables fixed." |
72 | 72 | ] |
73 | 73 | }, |
74 | 74 | { |
|
119 | 119 | "cAlpha = np.sqrt(pCov[1, 1])\n", |
120 | 120 | "\n", |
121 | 121 | "# Display formatted results\n", |
122 | | - "print(\"Model slope (mV/Hz): {0:.2f} ± {1:.2f}\".format(mHat, mAlpha))\n", |
123 | | - "print(\"Model intercept (mV): {0:.0f} ± {1:.0f}\".format(cHat, cAlpha))\n", |
| 122 | + "print(f\"Model slope (mV/Hz): {mHat:.2f} ± {mAlpha:.2f}\")\n", |
| 123 | + "print(f\"Model intercept (mV): {cHat:.0f} ± {cAlpha:.0f}\")\n", |
124 | 124 | "print()\n", |
125 | 125 | "\n", |
126 | 126 | "# Evaluate the chi-squared function at the minimum and define contour levels\n", |
|
231 | 231 | "print(\"Covariance matrix:\")\n", |
232 | 232 | "print(np.array_str(pCov, precision=4, suppress_small=True))\n", |
233 | 233 | "print()\n", |
234 | | - "print(\"Correlation coefficient: {0:.3f}\".format(rho_mc))\n", |
| 234 | + "print(f\"Correlation coefficient: {rho_mc:.3f}\")\n", |
235 | 235 | "print()" |
236 | 236 | ] |
237 | 237 | }, |
|
251 | 251 | "outputs": [], |
252 | 252 | "source": [ |
253 | 253 | "trial = np.arange(1,77)\n", |
254 | | - "noiseScale = 0.1;\n", |
| 254 | + "noiseScale = 0.1\n", |
255 | 255 | "\n", |
256 | | - "mRange = [0.925, 1.075];\n", |
257 | | - "cRange = [-0.35, 0.35];\n", |
258 | | - "m = np.linspace(mRange[0], mRange[1], Nm);\n", |
259 | | - "c = np.linspace(cRange[0], cRange[1], Nc);\n", |
| 256 | + "mRange = [0.925, 1.075]\n", |
| 257 | + "cRange = [-0.35, 0.35]\n", |
| 258 | + "m = np.linspace(mRange[0], mRange[1], Nm)\n", |
| 259 | + "c = np.linspace(cRange[0], cRange[1], Nc)\n", |
260 | 260 | "\n", |
261 | | - "chi2grid_trial = np.zeros([Nc, Nm]);\n", |
| 261 | + "chi2grid_trial = np.zeros([Nc, Nm])\n", |
262 | 262 | "\n", |
263 | 263 | "fig, ax = plt.subplots(5, 5, sharex=True, sharey=True, figsize=[12.8, 9.6])\n", |
264 | 264 | "\n", |
|
293 | 293 | " linewidths=1, linestyles='dotted')\n", |
294 | 294 | " ax[i][j].vlines(mHat_trial + mAlpha_trial*np.array([-1, 1]), c[0], c[-1], \n", |
295 | 295 | " linewidths=1, linestyles='dotted')\n", |
296 | | - " ax[i][j].text(1.05, 0.25, \"({0:d})\".format(5*i+j+1))\n", |
| 296 | + " ax[i][j].text(1.05, 0.25, f\"({5*i+j+1:d})\")\n", |
297 | 297 | "\n", |
298 | 298 | "fig.text(0.5, 0.04, 'Gradient (trial/mean)', ha='center')\n", |
299 | 299 | "fig.text(0.04, 0.5, 'Intercept (trial)', va='center', rotation='vertical')\n", |
|
351 | 351 | "source": [ |
352 | 352 | "N_sim = 500\n", |
353 | 353 | "trial = np.arange(1,77)\n", |
354 | | - "noiseScale = 0.1;\n", |
| 354 | + "noiseScale = 0.1\n", |
355 | 355 | "\n", |
356 | 356 | "mHat_sim = np.zeros(N_sim)\n", |
357 | 357 | "cHat_sim = np.zeros(N_sim)\n", |
|
367 | 367 | "Nm = 50\n", |
368 | 368 | "Nc = 50\n", |
369 | 369 | "\n", |
370 | | - "mRange = [0.95, 1.06];\n", |
371 | | - "cRange = [-0.35, 0.35];\n", |
372 | | - "m = np.linspace(mRange[0], mRange[1], Nm);\n", |
373 | | - "c = np.linspace(cRange[0], cRange[1], Nc);\n", |
| 370 | + "mRange = [0.95, 1.06]\n", |
| 371 | + "cRange = [-0.35, 0.35]\n", |
| 372 | + "m = np.linspace(mRange[0], mRange[1], Nm)\n", |
| 373 | + "c = np.linspace(cRange[0], cRange[1], Nc)\n", |
374 | 374 | "\n", |
375 | 375 | "# Define the chi-squared function for m and c, given the data\n", |
376 | 376 | "def chi2fun(m, c):\n", |
377 | 377 | " normres = (meanVal - (m*trial + c))/(noiseScale*trial)\n", |
378 | 378 | " return np.sum(normres**2)\n", |
379 | 379 | "\n", |
380 | | - "chi2grid_sim = np.zeros([Nc, Nm]);\n", |
| 380 | + "chi2grid_sim = np.zeros([Nc, Nm])\n", |
381 | 381 | "for i in range(Nc):\n", |
382 | 382 | " for j in range(Nm):\n", |
383 | 383 | " chi2grid_sim[i, j] = chi2fun(m[j], c[i])\n", |
|
0 commit comments