跳至内容
Oxford PhysicsNotebook

DA / 大一

DA01 exercise1 (1)

静态 Notebook 阅读页:展示保存的代码和输出,未重新运行。

Finding Hubble's constant

Introduction

In this exercise we will introduce ourselves to the Python notebook, import some data, plot it and then fit a linear relationship.

The following lines demonstrate some examples of different types of formatting which can be added to a notebook cell.

Note we have also grouped cells into sections. This is the Introduction section.

Overview of steps coming up (note: this is an example of a formatted list!):

  • import data
  • examine data
  • plot data
  • fit data

Importing data

import the 'pandas' library and use it to import CSV style files.

Note: To save having to write 'pandas' all the time we load it into a variable called 'pd' instead

import pandas as pd
  • From the pandas module, we use the 'read_csv' function to load our data (filename in brackets).
  • we assign our imported data to a variable we call 'df' (short for dataframe).
df = pd.read_csv('hubble.csv')

We can look at a sample of our data by using the 'head' function. This is a way we can see the names of our columns (which we will use later).

df.head()

Note that the data frame will refer to the first column, in this case 'radial distance (Mparsec)' as column 0, the next column would be column 1 etc.

There are other functions which provide alternate summaries of the data. Below is the 'describe' function (others to try are: info() and columns).

df.describe()

Plotting data

  • We first specify which columns are the x and y axes.
  • The names are those found when using the 'head' or 'describe' functions (above).

We first need to extract the columns of data we are interested in. We'll call them 'x' and 'y' and we access elements of a list with square brackets.

There are a number of different methods to extract one column. Here we show 2 of the easiest examples:

  1. we ask for the column of values given the column name
  2. we ask for the column of values specifying the column number
x = df["Distance (M-Parsecs)"].values
#x = df[df.columns[0]].values

# this was an example of the 2 different methods.
# We'll choose to use the first option for the y values and y errors (the second is 'commented out')

y = df["Speed (km/sec)"].values
yerr = df["error in v (km/sec)"].values

Note: in the cell of code above, a # symbol is used in Python to represent 'comments' in the code, or lines of code which we don't want to be executed.

Now we would like to plot our data. We do this by importing the 'matplotlib pyplot' module.

  • First import the module (the line following is a bit of technical detail, it ensures our plots are displayed correctly in the notebook, it's good to include this)

  • We specify a size for the plot (the 'figsize' option)

  • The 'errorbar' function produces a plot with error bars. For this function, we specify the x, y and the y errorbars.

  • We use the 'errorbar' plot to produce a plot with error bars. We specify to use our 'x' and 'y' data and 'yerr' uncertainty values for the errorbars.

  • Note: the other commands we specify are some formatting options (fmt='.', capsize=5 change what the data-points look like)

  • We specify the axes labels, and a title and ensure there is a legend.

import matplotlib.pyplot as plt
%matplotlib inline

plt.figure(figsize=(5,4), dpi=200)
plt.errorbar(x, y, yerr, label='data', fmt='.', capsize=5)
plt.title('Speed vs Distance')
plt.xlabel('Distance (M-Parsecs)')
plt.ylabel('Speed (km/sec)')
plt.legend()
plt.show()

Fitting using SciPy

Now we would like to fit our data. To do so we need to import a statistics package, of which there are many choices, we will use a common one here. A module which makes this easy is the 'scipy.optimize' module and the 'curve_fit' function.

from scipy.optimize import curve_fit

The 'curve_fit' function requires us to define the type of fit. We first define a linear fit here.

def linear(x, a, b):
    return a * x + b

We then perform the fit with the (linear) function we have defined.

  • In the 'curve_fit' function we specify our 'x' and 'y' data and also the uncertainties on our y-data (we called this variable 'yerr')
  • We specify the function to fit, which we just named 'linear'.
  • We must specify the 'absolute_sigma=True' option. This option changes the way the fitting function works based on the type of uncertainties we have given it. When we have uncertainties based on measurements which are the same units as our data (which will be the case for most of your lab work) we specify this option to be True.

The 'curve_fit' function returns two things:

  1. The fitted set of parameters (here this contains our fitted 'a' and 'b' values).
  2. The covariance matrix. (This is a matrix and gives information about how well the parameters are affected by each other. Discussed in 2nd year stats!)
parameters, covariant_matrix = curve_fit(linear, x, y, sigma=yerr, absolute_sigma=True)
slope = parameters[0] #extract slope, it is parameter #0
intercept = parameters[1] #extract intercept, it is parameter #1

To find the errors on the parameters, we have to take the square root of the diagonal elements of the variance (importing the numpy library for the square root).

import numpy as np
parameter_uncertainties = np.sqrt(covariant_matrix.diagonal())
slope_error = parameter_uncertainties[0]
intercept_error = parameter_uncertainties[1]

print('Intercept: {:.0f} Error on intercept: {:.0f}'.format(intercept, intercept_error))
print('Slope: {:.2f} Error on slope: {:.2f}'.format(slope, slope_error))

Combining plots

Now we:

  • plot both the data and the fitted line and,
  • save our plot as a file using the 'savefig' function (note: must be executed from this cell)

The saved image file, 'hubble_fit.png' should be saved in the same folder as where this Python script is.

fig = plt.figure(figsize=(5,4), dpi=200)
plt.errorbar(x, y, yerr, label='Data', fmt='.', capsize=5)
plt.plot(x, linear(x, slope, intercept), 'r', label='Fitted line') # note: we can write any formula into this plotting function
plt.title('Speed vs Distance')
plt.xlabel('Distance (M-Parsecs)')
plt.ylabel('Speed (km/sec)')
plt.legend()

plt.savefig('hubble_fit.png', dpi=200)
plt.show()

Data re-visited

We could equally swap the data in the plot around and plot distance vs speed. To do this we:

  • re-set our x, y and yerr variables to the appriate data.
  • re-calculate the fit and extract the slope and intercept
x = df["Speed (km/sec)"].values
y = df["Distance (M-Parsecs)"].values
yerr = df["error in D (M-Parsecs)"].values

parameters, covariant_matrix = curve_fit(linear, x, y, sigma=yerr, absolute_sigma=True)
slope = parameters[0] #slope is parameter #0
intercept = parameters[1] #intercept is parameter #1

parameter_uncertainties = np.sqrt(covariant_matrix.diagonal())
slope_error = parameter_uncertainties[0]
intercept_error = parameter_uncertainties[1]

print('Intercept: {:.2f} Error on intercept: {:.2f}'.format(intercept, intercept_error))
print('Slope: {:.5f} Error on slope: {:.5f}'.format(slope, slope_error))

Since we swapped the axes, we invert the slope to compare with the previous value. Note, for the uncertainty, we take the fractional uncertainty and multiply by the new value (i.e. the inverted value)

print('1/Slope: {:.1f} Error on slope: {:.1f}'.format(1/slope, (slope_error/slope)*1/slope))

End of exercise 1