Linear Regrssion

Load Necessary Libraries

Load dataframe and data cleaning

Converting Height from inch to cm (1 inch = 2.54 cm) and lbs to kg (1 lb = 0.45359237 kg)

Converting the Weighth and Height Columns to lists

Converting the weight_list and height_list to numpy arraies

Plotting outputs

1) Best linear predictions using our linear regression formulas as the first method

fig24.png

fig25.png

fig26.png

fig27.png

fig28.png

fig29.png

Predictions based on the first method

2) Best linear predictions using our linear regression formulas as the first method

Creating Linear Regression Object

Train the Model Using the Trainig Sets

Predictions based on the second method

fig30.png

Statistical Functions

Mean, standard deviation, and percentiles of arrays:

$average \equiv avg = \frac{\Sigma_{i = 1}^Nx_i}{N}$

$\sigma = std = \sqrt{\frac{\Sigma_{i = 1}^N(x_i -\mu)^2}{N}},~~where~~\mu \equiv avg$

What is the 75 percentile?

The answer is 43, meaning that 75% of the people are 43 or younger.

Interpolation

Curve Fitting

We would like to fit the data to the curve $y = ax^2 + b$.

The main goal here is to determine the values of $a$ and $b$

The equation for spring motion is $y(t) = A\cos(\omega t+\phi)$. We aim to find the natural frequency of oscillation $\omega$ for the spring.

Suppose that we have collected the following data:

$\omega = 2 \pi f$, $f=1/T$, and $T \approx 2$ seconds.

Thus good initial guess can be:

The estimated error on the parameters

The Goal of Curve Fitting

Purpose of interpolation

Given some $x_{\text{data}}=[...]$ and $y_{\text{data}}=[...]$ and a model function $f$ that depends on unknown parameters $\beta$ the goal is to find the optimal set of parameters $\beta$ such that the function $y=f(x,\beta)$ best resembles the data.

Here is a model where $\beta = (a,b,c)$ and

$$f(x, \beta) = f(x, a, b, c) = a(x-b)^2 + c$$

We want to find the optimal values of $a$, $b$, and $c$ that fit the data above

  1. pcov: the covariance matrix, which gives an estimate of the "error" of the parameters (based on how sensitive the "goodness-of-fit" is to changing them) and the relation the parameters have to eachother (are two seperate parameters really needed?)

Two things are returned here:

1. popt, which gives the optimal parameters for the model_f given the data

2. pcov: the covariance matrix, which gives an estimate of the "error" of the parameters (based on how sensitive the "goodness-of-fit" is to changing them) and the relation the parameters have to eachother (are two seperate parameters really needed?)

pcov is difficult to interpret, but we can do so as follows.

Lets look at an example below

Lets look at a color plot of the pcov matrix:

Examples

Example 1: Fitting Gaussian Curves

Open some data

Make a plot

Gaussian curve as the model with 3 parameters $A$, $\mu$, and $\sigma$

$$f(x, A, \mu, \sigma) = Ae^{-(x-\mu)^2/\sigma^2}$$

Selecting the main peak of the curve and neglecting other parts of the data

Replot the subregion of the curve

Now we fit to find the optimal parameters of the Gaussian curve

Estimation of the error of the above Gaussian problem can be found by looking at the square root of the diagonal elements of pcov $\sqrt{\text{pcov}_{\text{diag}}}$

We can get the maximum likelihood estimator by including sigma=yerr_data in the curve_fit function

which are very close to the last prediction withouth sigma=yerr_data argument:

Example 2

The Lennard-Jones potential:

$$V(r) = 4\epsilon \left[ \left(\frac{\sigma}{r}\right)^{12}-\left(\frac{\sigma}{r}\right)^{6}\right]$$

Parameters are: $\sigma$ and $\epsilon$

First its probably a good idea to standardize our units. We can convert back after:

In order to fit this data to the curve, we need to identify some key points for out initial parameter guess. From wikipedia:

Obtaining initial guesses

Getting the optimal parameters

Plotting the model