{ "cells": [ { "cell_type": "markdown", "id": "3b8ddf8d-902e-4462-aba9-4c468600854f", "metadata": {}, "source": [ "## Data Analysis III\n", "\n", "### February 6, 2024\n", "\n", "Goals for Today\n", "* Loading data from text files\n", "* Computing weighted averages\n", "* Plotting with errorbars\n", "* Compute the Chi-Squared" ] }, { "cell_type": "code", "execution_count": 7, "id": "11ebb3bb-a7da-4b5d-ba66-40705bcdb8c8", "metadata": {}, "outputs": [], "source": [ "#Download the file 'hdata.dat' from the course website. Open it in a text editor to get a sense of what it contains" ] }, { "cell_type": "markdown", "id": "03c6ef7b-053c-43e5-836e-9672c71c199c", "metadata": {}, "source": [ "Look at the syntax of np.loadtxt, and use it to open the file.\n", "\n", "Before you run any code, answer the following questions\n", "* What kind of delimiter is used? Is it space, comma, tab?\n", "* How many header lines are used? Headers often provide relevant information such as units, instrument settings used for the data included. \n", "* When running np.loadtext, you have to specify optional arguments for delimiter, as well as number of rows to skip\n", "\n" ] }, { "cell_type": "code", "execution_count": 8, "id": "a9b4ba3c-5753-484e-9f5f-0708b642f7d7", "metadata": {}, "outputs": [], "source": [ "#run data = np.loadtext(...) to store the contents of the file into an array called data\n", "#split up data into two arrays: yvalues and yunc. You may find it useful to use the transpose operation (np.transpose())" ] }, { "cell_type": "code", "execution_count": null, "id": "d50ad8b8-d8c8-46e2-9b18-f627a014d962", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "a830ad0b-4fc5-4223-a39e-f1355bccfd2e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 9, "id": "11c3f3c0-86a3-4da0-8244-a6a0bb884023", "metadata": {}, "outputs": [], "source": [ "#Make a plot of Measurement number vs value with properly labeled axes\n", "#Calculate the mean of the values using np.mean() or some other method you like\n" ] }, { "cell_type": "markdown", "id": "c0098102-e3b8-434e-a02a-2c04bc25272c", "metadata": {}, "source": [ "### Recall from last week: \n", "Average: $\\bar{x} = \\frac{1}{N}\\Sigma_ix_i$\n", "\n", "Weighted average: $\\bar{x}_{weighted} = \\frac{\\Sigma_iw_ix_i}{\\Sigma_iw_i}$, where the weights are given by $w_i = \\frac{1}{\\alpha_i^2}$ \n", "\n", "In this notation, $\\alpha_i$ represents the uncertainty of data point $i$." ] }, { "cell_type": "markdown", "id": "b16ec743-e96c-44e0-b19a-cecab2b2f7cc", "metadata": {}, "source": [ "calculate the weighted mean. (Hint: a dot product makes the numerator easy. you can take the dot product of two matrices a and b either with numpy.dot -- i.e., np.dot(a,b) -- or by using the @ sign -- i.e., a @ b.) \n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "bb6d5f21-291c-4ee0-9688-c98b27581be6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "8730b24a-77da-47bc-92dc-6ce4c44b3aaf", "metadata": {}, "source": [ "Make a plot of Measurement number vs value with properly labeled axes\n", "\n", "Use ```np.hlines``` to draw horizontal lines for the mean, and the weighted means.\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "a10ce75a-e7bd-4369-9001-c141a21e91e2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "af7d19ad-2b56-4d76-bd36-62c0ae60c573", "metadata": {}, "source": [ "Look up the syntax of the function ```np.errorbar()```\n", "\n", "For example, if given three arrrs, xval,yval,yerr, then the following code snippet plots errorbar plots with circular indicators\n", "``` errorbarplot(xval,yval,yerr,fmt = 'o') ```" ] }, { "cell_type": "code", "execution_count": null, "id": "ce313557-e670-48b4-81f4-562a307c6eca", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "f1e49038-e815-4f69-a079-27621726a0a2", "metadata": {}, "source": [ "Next, plot the residuals $x_i - \\bar{x}$" ] }, { "cell_type": "code", "execution_count": null, "id": "af054489-2b0f-4f83-88a3-beea137f2e5d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "f998c383-3ad9-470a-ba0a-ddf4b944cd13", "metadata": {}, "source": [ "And then plot the normalized residuals ($(x_i - \\bar{x})/\\alpha_i$)" ] }, { "cell_type": "code", "execution_count": null, "id": "4a910725-b422-450e-bb28-84feb22fc49b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "2e5445e9-676f-4093-909b-b551de631d4d", "metadata": {}, "source": [ "Now square the residuals and add them up. This gives the chi-squared." ] }, { "cell_type": "code", "execution_count": null, "id": "802ee0fa-10db-4e8c-97ce-c190dceabd83", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "3cc71de6-84ae-4a70-a8ae-8fc17fb04166", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "65870b83-6498-4f6a-9290-8aa03cd284db", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "6037f52c-74ff-4141-aec4-31a0d6404427", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.9" } }, "nbformat": 4, "nbformat_minor": 5 }