diff --git a/doc/Programs/JupyterFiles/Design_matrix.ipynb b/doc/Programs/JupyterFiles/Design_matrix.ipynb new file mode 100644 index 000000000..679cef377 --- /dev/null +++ b/doc/Programs/JupyterFiles/Design_matrix.ipynb @@ -0,0 +1,271 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1. 0. 0. ... 0. 0. 0. ]\n", + " [1. 0. 0.05 ... 0. 0. 0. ]\n", + " [1. 0. 0.1 ... 0. 0. 0. ]\n", + " ...\n", + " [1. 0.95 0.85 ... 0.61945344 0.55424781 0.49590594]\n", + " [1. 0.95 0.9 ... 0.69447375 0.6579225 0.623295 ]\n", + " [1. 0.95 0.95 ... 0.77378094 0.77378094 0.77378094]]\n" + ] + }, + { + "data": { + "text/plain": [ + "400" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"\"\"\n", + "Importations\n", + "\"\"\"\n", + "\n", + "from mpl_toolkits.mplot3d import Axes3D\n", + "import matplotlib.pyplot as plt\n", + "from matplotlib import cm\n", + "from matplotlib.ticker import LinearLocator, FormatStrFormatter\n", + "\n", + "import pandas as pd\n", + "import numpy as np\n", + "from random import random, seed\n", + "\n", + "#plt.close()\n", + "#fig = plt.figure()\n", + "#ax = fig.gca(projection=\"3d\")\n", + "\n", + "# Make data\n", + "\n", + "x0 = np.arange(0, 1, 0.05)\n", + "y0 = np.arange(0, 1, 0.05)\n", + "\n", + "x, y = np.meshgrid(x0,y0)\n", + "\n", + "def FrankeFunction(x,y):\n", + "\tterm1 = 0.75*np.exp(-(0.25*(9*x-2)**2) - 0.25*((9*y-2)**2))\n", + "\tterm2 = 0.75*np.exp(-((9*x+1)**2)/49.0 - 0.1*(9*y+1))\n", + "\tterm3 = 0.5*np.exp(-(9*x-7)**2/4.0 - 0.25*((9*y-3)**2))\n", + "\tterm4 = -0.2*np.exp(-(9*x-4)**2 - (9*y-7)**2)\n", + "\n", + "\treturn term1 + term2 + term3 + term4\n", + "\n", + "z = FrankeFunction(x, y)\n", + "\n", + "matrix = np.zeros((len(y0)**2,21))\n", + "\n", + "# First column\n", + "\n", + "matrix[:,0] = 1\n", + "\n", + "# Second column\n", + "\n", + "X0 = []\n", + "for k in range(len(x0)):\n", + "\tfor i in range(len(x0)) :\n", + "\t\txk = x0[k]\n", + "\t\tX0.append(xk)\n", + "\n", + "matrix[:,1] = X0\n", + "\n", + "# Third column\n", + "\n", + "Y0 = []\n", + "for k in range(len(y0)) :\n", + "\tfor i in range(len(y0)) :\n", + "\t\tyk = x0[i]\n", + "\t\tY0.append(yk)\n", + "\n", + "matrix[:,2] = Y0\n", + "\n", + "# Fourth column\n", + "\n", + "X02 = []\n", + "for k in range(len(x0)**2):\n", + "\tX02.append(X0[k]**2)\n", + "\n", + "matrix[:,3] = X02\n", + "\n", + "# Fifth column\n", + "\n", + "Y02 = []\n", + "for k in range(len(y0)**2):\n", + "\tY02.append(Y0[k]**2)\n", + "\t\n", + "matrix[:,4] = Y02\n", + "\n", + "# sixth column\n", + "\n", + "X0Y0 = []\n", + "for k in range(len(x0)*len(y0)):\n", + "\tX0Y0.append(X0[k]*Y0[k])\n", + "\t\n", + "matrix[:,5] = X0Y0\n", + "\n", + "#7th column\n", + "\n", + "X03 = []\n", + "for k in range(len(x0)*len(y0)):\n", + "\tX03.append(X0[k]*X0[k]*X0[k])\n", + "\n", + "matrix[:,6] = X03\n", + "\n", + "\n", + "#8th column\n", + "\n", + "Y03 = []\n", + "for k in range(len(x0)*len(y0)):\n", + "\tY03.append(Y0[k]*Y0[k]*Y0[k])\n", + "\n", + "matrix[:,7] = Y03\n", + "\n", + "#9th column\n", + "\n", + "X02Y0 = []\n", + "for k in range(len(x0)*len(y0)):\n", + "\tX02Y0.append(X0[k]*X0[k]*Y0[k]) \n", + "\t\n", + "matrix[:,8] = X02Y0\n", + "\t\n", + "#10th column \t\n", + " \n", + "X0Y02 = []\n", + "for k in range(len(x0)*len(y0)):\n", + "\tX0Y02.append(X0[k]*Y0[k]*Y0[k])\n", + "\t\n", + "matrix[:,9] = X0Y02\n", + "\n", + "#11th column\n", + "\n", + "X04 = []\n", + "for k in range(len(x0)*len(y0)):\n", + "\tX04.append(X0[k]*X0[k]*X0[k]*X0[k])\n", + "\t\n", + "matrix[:,10] = X04\n", + "\n", + "#12th column\n", + "\n", + "Y04 = []\n", + "for k in range(len(x0)*len(y0)):\n", + "\tY04.append(Y0[k]*Y0[k]*Y0[k]*Y0[k])\n", + "\t\n", + "matrix[:,11] = Y04\n", + "\n", + "#13th column\n", + "\n", + "X03Y0 =[]\n", + "for k in range(len(x0)*len(y0)):\n", + "\tX03Y0.append(X0[k]*X0[k]*X0[k]*Y0[k])\n", + "\t\n", + "matrix[:,12] = X03Y0\n", + "\n", + "#14th column\n", + "\n", + "X02Y02 = []\n", + "for k in range(len(x0)*len(y0)):\n", + "\tX02Y02.append(X0[k]*X0[k]*Y0[k]*Y0[k])\n", + "\t\n", + "matrix[:,13] = X02Y02\n", + "\n", + "#15th column\n", + "\n", + "X0Y03 = []\n", + "for k in range(len(x0)*len(y0)):\n", + "\tX0Y03.append(X0[k]*Y0[k]*Y0[k]*Y0[k])\n", + "\t\n", + "matrix[:,14] = X0Y03\n", + "\n", + "#16th column\n", + "\n", + "X05 = []\n", + "for k in range(len(x0)*len(y0)):\n", + "\tX05.append(X0[k]*X0[k]*X0[k]*X0[k]*X0[k])\n", + "\t\n", + "matrix[:,15] = X05\n", + "\n", + "#17th column\n", + "\n", + "Y05 = []\n", + "for k in range(len(x0)*len(y0)):\n", + "\tY05.append(Y0[k]*Y0[k]*Y0[k]*Y0[k]*Y0[k])\n", + "\t\n", + "matrix[:,16] = Y05\n", + "\n", + "#18th column\n", + "\n", + "X04Y0 = []\n", + "for k in range(len(x0)*len(y0)):\n", + "\tX04Y0.append(X0[k]*X0[k]*X0[k]*X0[k]*Y0[k])\n", + "\t\n", + "matrix[:,17] = X04Y0\n", + "\n", + "#19th column\n", + "\n", + "X03Y02 = []\n", + "for k in range(len(x0)*len(y0)):\n", + "\tX03Y02.append(X0[k]*X0[k]*X0[k]*Y0[k]*Y0[k])\n", + "\t\n", + "matrix[:,18] = X03Y02\n", + "\n", + "#20th column\n", + "\n", + "X02Y03 = []\n", + "for k in range(len(x0)*len(y0)):\n", + "\tX02Y03.append(X0[k]*X0[k]*Y0[k]*Y0[k]*Y0[k])\n", + "\t\n", + "matrix[:,19] = X02Y03\n", + "\n", + "#21th column\n", + "\n", + "X0Y04 = []\n", + "for k in range(len(x0)*len(y0)):\n", + "\tX0Y04.append(X0[k]*Y0[k]*Y0[k]*Y0[k]*Y0[k])\n", + "\t\n", + "matrix[:,20] = X0Y04\n", + "\n", + "print(matrix)\n", + "len(matrix)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "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.9.12" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/doc/pub/week39/ipynb/week39.ipynb b/doc/pub/week39/ipynb/week39.ipynb index c4906056b..9cbd5e3ac 100644 --- a/doc/pub/week39/ipynb/week39.ipynb +++ b/doc/pub/week39/ipynb/week39.ipynb @@ -4235,10 +4235,18 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 1, "id": "e8fdc7be", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0.25 0.19661197 0.10499357]\n" + ] + } + ], "source": [ "import jax.numpy as jnp\n", "from jax import grad, jit, vmap\n", @@ -4250,6 +4258,14 @@ "derivative_fn = grad(sum_logistic)\n", "print(derivative_fn(x_small))" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1a930655", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { diff --git a/doc/src/week39/jax.py b/doc/src/week39/jax.py new file mode 100644 index 000000000..a5d76a774 --- /dev/null +++ b/doc/src/week39/jax.py @@ -0,0 +1,9 @@ +import jax.numpy as jnp +from jax import grad, jit, vmap + +def sum_logistic(x): + return jnp.sum(1.0 / (1.0 + jnp.exp(-x))) + +x_small = jnp.arange(3.) +derivative_fn = grad(sum_logistic) +print(derivative_fn(x_small))