Skip to content

Commit

Permalink
deploy: 497b551
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-brett committed Sep 14, 2023
1 parent 532e1e3 commit 524c518
Show file tree
Hide file tree
Showing 42 changed files with 563 additions and 351 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
111 changes: 94 additions & 17 deletions _sources/glm_intro.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,33 @@ plt.xlabel('Clamminess of handshake')
plt.ylabel('Psychopathy score')
```

Let's immediately rename the values we are *predicting* to `y`, and the values we are *predicting with* to `x`:

```{python}
y = psychopathy
x = clammy
```


You have already seen a preliminary guess we made at a straight line that links
the `clammy` scores to the `psychopathy` scores. Our guess was:

```{python}
guessed_slope = 0.9
guessed_intercept = 10
b = 0.9
c = 10
```

We use this line to *predict* the `psychopathy` scores from the `clammy` scores.

```{python}
predicted_psychopathy = guessed_slope * clammy + guessed_intercept
predicted_psychopathy
predicted = b * clammy + c
predicted
```

```{python}
# Plot the data and the predictions
plt.plot(clammy, psychopathy, '+', label='Actual data')
plt.plot(clammy, predicted_psychopathy, 'ro', label='Predictions')
plt.plot(x, y, '+', label='Actual data')
plt.plot(x, predicted, 'ro', label='Predictions')
plt.xlabel('Clamminess of handshake')
plt.ylabel('Psychopathy score')
plt.title('Clammy vs psychopathy with guessed line')
Expand Down Expand Up @@ -125,7 +133,7 @@ sy_y_ind = IndexedBase("y")
vector_indices = range(1, n + 1)
sy_y_val = Matrix([sy_y_ind[i] for i in vector_indices])
Eq(sy_y, Eq(sy_y_val, Matrix(psychopathy)), evaluate=False)
Eq(sy_y, Eq(sy_y_val, Matrix(y)), evaluate=False)
```

`x` (the `clammy` score) is a *predictor*. Lets call the clammy scores $\xvec$.
Expand All @@ -137,7 +145,7 @@ student (= 0.389) and $x_i$ is the value for student $i$ where $i \in 1 .. 12$.
sy_x = symbols(r'\vec{x}')
sy_x_ind = IndexedBase("x")
sy_x_val = Matrix([sy_x_ind[i] for i in vector_indices])
Eq(sy_x, Eq(sy_x_val, Matrix(clammy)), evaluate=False)
Eq(sy_x, Eq(sy_x_val, Matrix(x)), evaluate=False)
```

In [regression notation](regression_notation.Rmd), we wrote our straight line
Expand All @@ -159,6 +167,14 @@ $\evec$ is the vector of as-yet-unknown errors $e_1, e_2, ... e_{12}$. The
values of the errors depend on the predicted values, and therefore, on our
slope $b$ and intercept $c$.

We calculate the errors as:

```{python}
e = y - predicted
```

We write the errors as an error vector:

```{python tags=c("hide-input")}
# Ignore this cell.
sy_e = symbols(r'\vec{\varepsilon}')
Expand All @@ -181,6 +197,13 @@ rhs = MatAdd(MatMul(sy_b, sy_x_val), sy_c_mat, sy_e_val)
Eq(sy_y_val, rhs, evaluate=False)
```

Confirm this is true when we calculate on our particular values:

```{python}
# Confirm that y is close as dammit to b * x + c + e
assert np.allclose(y, b * x + c + e)
```

Bear with with us for a little trick. We define $\vec{o}$ as a vector of ones,
like this:

Expand All @@ -191,6 +214,12 @@ sy_o_val = Matrix([1 for i in vector_indices])
Eq(sy_o, sy_o_val, evaluate=False)
```

In code, in our case:

```{python}
o = np.ones(n)
```

Now we can rewrite the formula as:

$$
Expand All @@ -212,13 +241,27 @@ This evaluates to the result we intend:
Eq(sy_y_val, sy_c * sy_o_val + sy_b * sy_x_val + sy_e_val, evaluate=False)
```

```{python}
# Confirm that y is close as dammit to b * x + c * o + e
assert np.allclose(y, b * x + c * o + e)
```

$\newcommand{Xmat}{\boldsymbol X} \newcommand{\bvec}{\vec{\beta}}$

We can now rephrase the calculation in terms of matrix multiplication.

Call $\Xmat$ the matrix of two columns, where the first column is $\xvec$ and
the second column is the column of ones ($\vec{o}$ above). Call $\bvec$ the
column vector:
the second column is the column of ones ($\vec{o}$ above).


In code, for our case:

```{python}
X = np.stack([x, o], axis=1)
X
```

Call $\bvec$ the column vector:

$$
\left[
Expand All @@ -229,6 +272,13 @@ c \\
\right]
$$

In code:

```{python}
B = np.array([b, c])
B
```

This gives us exactly the same calculation as $\yvec = b \xvec + c + \evec$
above, but in terms of matrix multiplication:

Expand All @@ -245,7 +295,6 @@ $$
\yvec = \Xmat \bvec + \evec
$$


Because of the way that matrix multiplication works, this again gives us the
intended result:

Expand All @@ -254,8 +303,15 @@ intended result:
Eq(sy_y_val, sy_xmat_val @ sy_beta_val + sy_e_val, evaluate=False)
```

We still haven’t found our best fitting line. But before we go further,
it might be obvious that we can easily add a new predictor here.
We write *matrix multiplication* in Numpy as `@`:

```{python}
# Confirm that y is close as dammit to X @ B + e
assert np.allclose(y, X @ B + e)
```

We still haven’t found our best fitting line. But before we go further, it
might be clear that we can easily add a new predictor here.


## Multiple regression
Expand All @@ -272,9 +328,22 @@ age = np.array(
```

Now rename the `clammy` predictor vector from $\xvec$ to
$\xvec_1$. Of course $\xvec_1$ has 12 values $x_{1, 1}..x_{1,
12}$. Call the `age` predictor vector $\xvec_2$. Call the slope for
`clammy` $b_1$ (slope for $\xvec_1$). Call the slope for age
$\xvec_1$.

```{python}
# clammy (our previous x) is the first column we will use to predict.
x_1 = clammy
```

Of course $\xvec_1$ has 12 values $x_{1, 1}..x_{1, 12}$. Call the `age`
predictor vector $\xvec_2$.

```{python}
# age is the second column we use to predict
x_2 = age
```

Call the slope for `clammy` $b_1$ (slope for $\xvec_1$). Call the slope for age
$b_2$ (slope for $\xvec_2$). Our model is:

$$
Expand Down Expand Up @@ -323,7 +392,15 @@ sy_Xmat = symbols(r'\boldsymbol{X}')
Eq(sy_Xmat, sy_xmat3_val, evaluate=False)
```

and therefore:
In code in our case:

```{python}
# Design X in values
X_3_cols = np.stack([x_1, x_2, o], axis=1)
X_3_cols
```

In symbols:

```{python tags=c("hide-input")}
# Ignore this cell.
Expand Down
20 changes: 10 additions & 10 deletions arrays_and_images.html
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ <h1>Arrays as images, images as arrays<a class="headerlink" href="#arrays-as-ima
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;matplotlib.image.AxesImage at 0x7f9634ba9060&gt;
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;matplotlib.image.AxesImage at 0x7f7961b11480&gt;
</pre></div>
</div>
<img alt="_images/d3bdaf09ab4de248ada9006c6f3f6aa396b99a66bfa57157f6ea8e325e463d84.png" src="_images/d3bdaf09ab4de248ada9006c6f3f6aa396b99a66bfa57157f6ea8e325e463d84.png" />
Expand All @@ -625,7 +625,7 @@ <h1>Arrays as images, images as arrays<a class="headerlink" href="#arrays-as-ima
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;matplotlib.colorbar.Colorbar at 0x7f9634b1e500&gt;
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;matplotlib.colorbar.Colorbar at 0x7f7961a73850&gt;
</pre></div>
</div>
<img alt="_images/7f2041d66705ba898f072957a1e419ad4221effecb638c2f99721a6a701413a8.png" src="_images/7f2041d66705ba898f072957a1e419ad4221effecb638c2f99721a6a701413a8.png" />
Expand All @@ -641,7 +641,7 @@ <h1>Arrays as images, images as arrays<a class="headerlink" href="#arrays-as-ima
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;matplotlib.colorbar.Colorbar at 0x7f96349ef190&gt;
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;matplotlib.colorbar.Colorbar at 0x7f796194a020&gt;
</pre></div>
</div>
<img alt="_images/fe16d4e426399645442aeb8f95a9539a2cd871326863a43b068580fae0825035.png" src="_images/fe16d4e426399645442aeb8f95a9539a2cd871326863a43b068580fae0825035.png" />
Expand All @@ -668,7 +668,7 @@ <h1>Arrays as images, images as arrays<a class="headerlink" href="#arrays-as-ima
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;matplotlib.colorbar.Colorbar at 0x7f96348aa110&gt;
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;matplotlib.colorbar.Colorbar at 0x7f7961806e30&gt;
</pre></div>
</div>
<img alt="_images/fe16d4e426399645442aeb8f95a9539a2cd871326863a43b068580fae0825035.png" src="_images/fe16d4e426399645442aeb8f95a9539a2cd871326863a43b068580fae0825035.png" />
Expand Down Expand Up @@ -731,7 +731,7 @@ <h1>Arrays as images, images as arrays<a class="headerlink" href="#arrays-as-ima
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>[&lt;matplotlib.lines.Line2D at 0x7f963496aa40&gt;]
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>[&lt;matplotlib.lines.Line2D at 0x7f79618cb6d0&gt;]
</pre></div>
</div>
<img alt="_images/2b8bb81f4e9577b5fd6a61124fb022b604d30f0a9c02dc7bc38db52b09b63e1d.png" src="_images/2b8bb81f4e9577b5fd6a61124fb022b604d30f0a9c02dc7bc38db52b09b63e1d.png" />
Expand All @@ -748,7 +748,7 @@ <h1>Arrays as images, images as arrays<a class="headerlink" href="#arrays-as-ima
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>[&lt;matplotlib.lines.Line2D at 0x7f96347f9420&gt;]
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>[&lt;matplotlib.lines.Line2D at 0x7f79617581f0&gt;]
</pre></div>
</div>
<img alt="_images/2b8bb81f4e9577b5fd6a61124fb022b604d30f0a9c02dc7bc38db52b09b63e1d.png" src="_images/2b8bb81f4e9577b5fd6a61124fb022b604d30f0a9c02dc7bc38db52b09b63e1d.png" />
Expand Down Expand Up @@ -785,7 +785,7 @@ <h1>Arrays as images, images as arrays<a class="headerlink" href="#arrays-as-ima
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;matplotlib.image.AxesImage at 0x7f9634c2d960&gt;
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;matplotlib.image.AxesImage at 0x7f79617ab280&gt;
</pre></div>
</div>
<img alt="_images/9bcb92f79177b8fb8f11c684a06580360455b68ab7128fbc898cbd03dae4c906.png" src="_images/9bcb92f79177b8fb8f11c684a06580360455b68ab7128fbc898cbd03dae4c906.png" />
Expand Down Expand Up @@ -932,7 +932,7 @@ <h1>Arrays as images, images as arrays<a class="headerlink" href="#arrays-as-ima
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;matplotlib.image.AxesImage at 0x7f96346aab60&gt;
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;matplotlib.image.AxesImage at 0x7f7961734760&gt;
</pre></div>
</div>
<img alt="_images/18c626fa333ccff444a9c11a6cc744f5e6d415ebd17dcb03858b11315228feb1.png" src="_images/18c626fa333ccff444a9c11a6cc744f5e6d415ebd17dcb03858b11315228feb1.png" />
Expand Down Expand Up @@ -1003,7 +1003,7 @@ <h1>Arrays as images, images as arrays<a class="headerlink" href="#arrays-as-ima
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;matplotlib.image.AxesImage at 0x7f96344c3eb0&gt;
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;matplotlib.image.AxesImage at 0x7f7961445120&gt;
</pre></div>
</div>
<img alt="_images/61f258561c288035a850ceefdaec326ed1f81c4ed252d89a938ef76cdfb3161d.png" src="_images/61f258561c288035a850ceefdaec326ed1f81c4ed252d89a938ef76cdfb3161d.png" />
Expand All @@ -1021,7 +1021,7 @@ <h1>Arrays as images, images as arrays<a class="headerlink" href="#arrays-as-ima
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;matplotlib.image.AxesImage at 0x7f963454d4e0&gt;
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;matplotlib.image.AxesImage at 0x7f79614ae500&gt;
</pre></div>
</div>
<img alt="_images/18ccb330c61b39ab784fa743c5ee5421b6ac6715a76520699ab647c2394864e6.png" src="_images/18ccb330c61b39ab784fa743c5ee5421b6ac6715a76520699ab647c2394864e6.png" />
Expand Down
8 changes: 4 additions & 4 deletions brisk_python.html
Original file line number Diff line number Diff line change
Expand Up @@ -1783,9 +1783,6 @@ <h2>Slicing<a class="headerlink" href="#slicing" title="Permalink to this headin
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>[9, 999, 0, 8, 20]
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>
</pre></div>
</div>
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>[20, 8, 0]
</pre></div>
</div>
Expand Down Expand Up @@ -3190,7 +3187,10 @@ <h2>Files<a class="headerlink" href="#files" title="Permalink to this heading">#
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Line is: MATLAB is good for matrices
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Line is:
</pre></div>
</div>
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> MATLAB is good for matrices

Line is: Python is good for coding
</pre></div>
Expand Down
6 changes: 3 additions & 3 deletions convolution_background.html
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ <h2>Using scipy<a class="headerlink" href="#using-scipy" title="Permalink to thi
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;matplotlib.legend.Legend at 0x7f251de33070&gt;
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;matplotlib.legend.Legend at 0x7feae1be61a0&gt;
</pre></div>
</div>
<img alt="_images/0170cc68185e9ab7419932303ccc53ec987d9657652ed654d94c7c59fdedf026.png" src="_images/0170cc68185e9ab7419932303ccc53ec987d9657652ed654d94c7c59fdedf026.png" />
Expand Down Expand Up @@ -757,7 +757,7 @@ <h2>Constructing a hemodynamic response function<a class="headerlink" href="#con
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>[&lt;matplotlib.lines.Line2D at 0x7f251b484250&gt;]
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>[&lt;matplotlib.lines.Line2D at 0x7feadf1f0f10&gt;]
</pre></div>
</div>
<img alt="_images/0f940e22064def4586fdfd195e230b51e64ed860ca059c7d39444abad42eb14c.png" src="_images/0f940e22064def4586fdfd195e230b51e64ed860ca059c7d39444abad42eb14c.png" />
Expand Down Expand Up @@ -804,7 +804,7 @@ <h2>Constructing a hemodynamic response function<a class="headerlink" href="#con
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>[&lt;matplotlib.lines.Line2D at 0x7f251b4f8dc0&gt;]
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>[&lt;matplotlib.lines.Line2D at 0x7feadf265d20&gt;]
</pre></div>
</div>
<img alt="_images/629b49544f5ba93c203e0964e249c16bbb9e63db3662a26eb0745e3e9e52700e.png" src="_images/629b49544f5ba93c203e0964e249c16bbb9e63db3662a26eb0745e3e9e52700e.png" />
Expand Down
4 changes: 2 additions & 2 deletions convolution_matrices.html
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ <h2>Neural and hemodynamic prediction<a class="headerlink" href="#neural-and-hem
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;matplotlib.image.AxesImage at 0x7fcca9e16320&gt;
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;matplotlib.image.AxesImage at 0x7f5b04d7ec80&gt;
</pre></div>
</div>
<img alt="_images/83782de8fe090067602d303f892ad20da65dc9707a25ce4eb7bace5bc8274eb2.png" src="_images/83782de8fe090067602d303f892ad20da65dc9707a25ce4eb7bace5bc8274eb2.png" />
Expand Down Expand Up @@ -824,7 +824,7 @@ <h2>Convolution is like cross-correlation with the reversed HRF<a class="headerl
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>[&lt;matplotlib.lines.Line2D at 0x7fcca9c24970&gt;]
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>[&lt;matplotlib.lines.Line2D at 0x7f5b04a28af0&gt;]
</pre></div>
</div>
<img alt="_images/13004ecc6c938b84ee6c94a4db468e22957e9ae1a3c46a5b60f9d23ce477601d.png" src="_images/13004ecc6c938b84ee6c94a4db468e22957e9ae1a3c46a5b60f9d23ce477601d.png" />
Expand Down
Loading

0 comments on commit 524c518

Please sign in to comment.