diff --git a/Visualization.ipynb b/Visualization.ipynb index 978a759..6bdc4e4 100644 --- a/Visualization.ipynb +++ b/Visualization.ipynb @@ -817,11 +817,128 @@ "# More Advanced Visualization\n", "There's tons more \"out there\" for helping you visualize your data. In the folder you downloaded, there is another `ipython` notebook called `Skyfit.ipynb` which shows you an example of using one such package: `Bokeh`. We encourage you to have a look at it, as it may give you ideas for handling your own summer research project. The math is pretty dense, but the real point is to see how data can be manipulated, fit, and visualized." ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's first import all the packages we will need." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from __future__ import division\n", + "import numpy as np\n", + "import matplotlib.pyplot as plt\n", + "import pandas as pd\n", + "from scipy.optimize import curve_fit" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Then let's load the supernova catalog." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "Data = pd.read_csv('data/SNIa_DM.dat',delim_whitespace=True, skiprows=4)\n", + "\n", + "zcmb = Data.zcmb.values\n", + "DM = Data.DM.values\n", + "DM_err = Data['+/-'].values\n", + "survey = Data.survey.values" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Next, we define the function specified at the beginning of this section." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def DM_model(logz,H0):\n", + " return 5*(np.log10(3*10**5/H0)+logz)+25" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we are ready to fit for the Hubble parameter! We will fit $H_0$ for survey 1 and survey 2 seperately." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "idx1_valid = np.where((DM_err>0)&(survey==1))[0]\n", + "popt1, pcov1 = curve_fit(DM_model,np.log10(zcmb[idx1_valid]),DM[idx1_valid],sigma=DM_err[idx1_valid])\n", + "print('H0 =',popt1[0],'+/-',np.sqrt(pcov1)[0][0],'[km/s/Mpc] for survey 1')\n", + "\n", + "idx2_valid = np.where((DM_err>0)&(survey==2))[0]\n", + "popt2, pcov2 = curve_fit(DM_model,np.log10(zcmb[idx2_valid]),DM[idx2_valid],sigma=DM_err[idx2_valid])\n", + "print('H0 =',popt2[0],'+/-',np.sqrt(pcov2)[0][0],'[km/s/Mpc] for survey 2')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's plot the fitting results." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "fontsize = 15\n", + "plt.figure(1)\n", + "plt.errorbar(np.log10(zcmb[idx1_valid]),DM[idx1_valid],yerr=DM_err[idx1_valid],linestyle='none',ecolor='k',markeredgecolor='k',markerfacecolor='k',\n", + " marker='o',ms=2,capsize=2)\n", + "plt.plot(np.log10(zcmb),DM_model(np.log10(zcmb),popt1[0]),'r',label='best fit model')\n", + "plt.xlabel(r'$\\log(z)$',fontsize=fontsize)\n", + "plt.ylabel(r'DM',fontsize=fontsize)\n", + "plt.xticks(fontsize = fontsize)\n", + "plt.yticks(fontsize = fontsize)\n", + "plt.legend(frameon=False,fontsize=fontsize)\n", + "plt.title('Survey 1',fontsize=fontsize)\n", + "\n", + "plt.figure(2)\n", + "plt.errorbar(np.log10(zcmb[idx2_valid]),DM[idx2_valid],yerr=DM_err[idx2_valid],linestyle='none',ecolor='k',markeredgecolor='k',markerfacecolor='k',\n", + " marker='o',ms=2,capsize=2)\n", + "plt.plot(np.log10(zcmb[idx2_valid]),DM_model(np.log10(zcmb[idx2_valid]),popt2[0]),'r',label='best fit model')\n", + "plt.xlabel(r'$\\log(z)$',fontsize=fontsize)\n", + "plt.ylabel(r'DM',fontsize=fontsize)\n", + "plt.xticks(fontsize = fontsize)\n", + "plt.yticks(fontsize = fontsize)\n", + "plt.legend(frameon=False,fontsize=fontsize)\n", + "plt.title('Survey 2',fontsize=fontsize)" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -835,7 +952,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.9" + "version": "3.7.6" } }, "nbformat": 4,