{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Numerical examples for \"A bit-by-bit guide to the equations governing differentiable neural computers\"\n", "\n", "See the [guide](dnc.html) for more details. You can modify the notebook by downloading its [source file](dnc-notebook.ipynb)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Auxiliary functions\n", "\n", "Next code is probably not the code you are looking for. It contains auxiliary functions that are mainly intended for pretty printing in the previous cells. You can safely skip this section." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Auxiliary functions:\n", "\n", "def _render_table (j=0,u=0,gamma=0,prod=0,a=0,state=\"row\"):\n", " if state==\"start\":\n", " _render_table.s= ''.join([\"$$\\\\newcommand{\\\\b}[1]{\\\\boldsymbol{#1}}$$\",\n", " \"\"\n", " \"\"\n", " \"\",\n", " \"\",\n", " \"\",\n", " \"\",\n", " \"\",\n", " \"\",\n", " \"\",\n", " \"\",\n", " \"\"])\n", " elif state==\"end\":\n", " _render_table.s+= \"
$j$$\\\\b{u}_t[j]$$\\\\gamma : \\\\b{\\\\phi}_t[\\\\gamma] = j$$1-\\\\b{u}_t[j]$$\\prod_{i=1}^{\\\\gamma-1} \\\\b{u}_t[\\\\b{\\\\phi}_t[i]]$$\\\\b{a}_t[j]$
\"\n", " return _render_table.s\n", " elif state==\"row\":\n", " _render_table.s+= ''.join([\"\",\n", " \"\"+str(j)+\"\",\n", " \"\"+str(u)+\"\",\n", " \"\"+str(gamma)+\"\",\n", " \"\"+str(round(1-u,4))+\"\",\n", " \"\"+str(round(prod,4))+\"\",\n", " \"\"+str(round(a,4))+\"\",\n", " \"\"])\n", " \n", " \n", "def _matrix_to_latex (A):\n", " s= \"\\\\begin{bmatrix}\"\n", " s+= \" \\\\\\\\\\n\".join([\" & \".join(map(str,line)) for line in A])\n", " s+= \"\\\\end{bmatrix}\"\n", " return s\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example of a dualistic read operation\n", "\n", "$$\n", "\\newcommand{\\b}[1]{\\boldsymbol{#1}}\n", "\\newcommand{\\r}[1]{\\mathrm{#1}}\n", "\\newcommand{\\c}[1]{\\mathcal{#1}}\n", "$$\n", "A position in memory can be read with the operation $\\b{M}^\\top_t \\b{w}^\\r{r}_t$.\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "Input:" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "$$\\b{M}_t=\\begin{bmatrix}-0.5 & 0.01 & 3.1 \\\\\n", "0.2 & 0.6 & 1.2 \\\\\n", "0.0 & 0.0 & 0.0 \\\\\n", "-0.1 & -0.05 & 0.0\\end{bmatrix}\\quad \\b{w}^\\r{r}_t=\\begin{bmatrix}0 \\\\\n", "1 \\\\\n", "0 \\\\\n", "0\\end{bmatrix}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "Vector retrieved from memory:" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "$$\\b{r}_t=\\begin{bmatrix}0.2 \\\\\n", "0.6 \\\\\n", "1.2\\end{bmatrix}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import numpy as np\n", "from IPython.display import display, Markdown\n", "\n", "# Memory at time t-1:\n", "M= np.array([[-0.5, 0.01, 3.1],\n", " [ 0.2, 0.6, 1.2],\n", " [ 0, 0, 0 ],\n", " [-0.1, -0.05, 0 ]])\n", "# Read weighting:\n", "w= np.array([0,1,0,0]).reshape(-1,1)\n", "\n", "# Operation to retrieve memory contents using the read weighting:\n", "r= M.T @ w\n", "\n", "display(Markdown(\"Input:\"))\n", "display(Markdown(\"$$\\\\b{M}_t=\"+_matrix_to_latex(M)+\"\\\\quad \\\\b{w}^\\\\r{r}_t=\"+_matrix_to_latex(w)+\"$$\"))\n", "display(Markdown(\"Vector retrieved from memory:\"))\n", "display(Markdown(\"$$\\\\b{r}_t=\"+_matrix_to_latex(r)+\"$$\"))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example of a non-dualistic read operation\n", "\n", "A position in memory can be read with the operation $\\b{M}^\\top_t \\b{w}^\\r{r}_t$.\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "Input:" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "$$\\b{M}_t=\\begin{bmatrix}-0.5 & 0.01 & 3.1 \\\\\n", "0.2 & 0.6 & 1.2 \\\\\n", "0.0 & 0.0 & 0.0 \\\\\n", "-0.1 & -0.05 & 0.0\\end{bmatrix}\\quad \\b{w}^\\r{r}_t=\\begin{bmatrix}0.0 \\\\\n", "0.8 \\\\\n", "0.1 \\\\\n", "0.1\\end{bmatrix}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "Vector retrieved from memory:" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "$$\\b{r}_t=\\begin{bmatrix}0.15 \\\\\n", "0.475 \\\\\n", "0.96\\end{bmatrix}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Memory at time t-1:\n", "M= np.array([[-0.5, 0.01, 3.1],\n", " [ 0.2, 0.6, 1.2],\n", " [ 0, 0, 0 ],\n", " [-0.1, -0.05, 0 ]])\n", "# Read weighting:\n", "w= np.array([0,0.8,0.1,0.1]).reshape(-1,1)\n", "\n", "# Operation to retrieve memory contents using the read weighting:\n", "r= M.T @ w\n", "\n", "display(Markdown(\"Input:\"))\n", "display(Markdown(\"$$\\\\b{M}_t=\"+_matrix_to_latex(M)+\"\\\\quad \\\\b{w}^\\\\r{r}_t=\"+_matrix_to_latex(w)+\"$$\"))\n", "display(Markdown(\"Vector retrieved from memory:\"))\n", "display(Markdown(\"$$\\\\b{r}_t=\"+_matrix_to_latex(r)+\"$$\"))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example of a dualistic write operation\n", "\n", "The equation that determines the new content of the memory is:\n", "\n", "$$\n", "\\b{M}_t = \\b{M}_{t-1} \\circ \\left( \\b{E} - \\b{w}^\\r{w}_t \\b{e}^\\top_t \\right) + \\b{w}^\\r{w}_t \\b{v}^\\top_t\n", "$$\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/markdown": [ "Input:" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "$$\\b{M}_{t-1}=\\begin{bmatrix}-0.5 & 0.01 & 3.1 \\\\\n", "0.2 & 0.6 & 1.2 \\\\\n", "0.0 & 0.0 & 0.0 \\\\\n", "-0.1 & -0.05 & 0.0\\end{bmatrix}\\quad \\b{w}^\\r{w}_t=\\begin{bmatrix}0 \\\\\n", "1 \\\\\n", "0 \\\\\n", "0\\end{bmatrix}\\quad \\b{v}_t=\\begin{bmatrix}-1.5 \\\\\n", "-1.3 \\\\\n", "-1.1\\end{bmatrix}\\quad \\b{e}_t=\\begin{bmatrix}1 \\\\\n", "1 \\\\\n", "1\\end{bmatrix}\\quad \\b{E}_t=\\begin{bmatrix}1.0 & 1.0 & 1.0 \\\\\n", "1.0 & 1.0 & 1.0 \\\\\n", "1.0 & 1.0 & 1.0 \\\\\n", "1.0 & 1.0 & 1.0\\end{bmatrix}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "New memory matrix:" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "$$\\b{M}_t=\\begin{bmatrix}-0.5 & 0.01 & 3.1 \\\\\n", "-1.5 & -1.3 & -1.1 \\\\\n", "0.0 & 0.0 & 0.0 \\\\\n", "-0.1 & -0.05 & 0.0\\end{bmatrix}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import numpy as np\n", "\n", "# Memory at time t-1:\n", "M1= np.array([[-0.5, 0.01, 3.1],\n", " [ 0.2, 0.6, 1.2],\n", " [ 0, 0, 0 ],\n", " [-0.1, -0.05, 0]])\n", "# Write weighting:\n", "w= np.array([0,1,0,0]).reshape(-1,1)\n", "# Write vector:\n", "v= np.array([-1.5,-1.3,-1.1]).reshape(-1,1)\n", "# Erase vector:\n", "e= np.array([1,1,1]).reshape(-1,1)\n", "# Matrix of ones:\n", "E= np.ones((4,3))\n", "\n", "# Operation to obtain the memory at time t:\n", "M= M1 * (E - w @ e.T) + w @ v.T\n", "\n", "display(Markdown(\"Input:\"))\n", "display(Markdown(\"$$\\\\b{M}_{t-1}=\"+_matrix_to_latex(M1)+\"\\\\quad \\\\b{w}^\\\\r{w}_t=\"+_matrix_to_latex(w)+\n", " \"\\\\quad \\\\b{v}_t=\"+_matrix_to_latex(v)+\"\\\\quad \\\\b{e}_t=\"+_matrix_to_latex(e)+\n", " \"\\\\quad \\\\b{E}_t=\"+_matrix_to_latex(E)+\"$$\"))\n", "display(Markdown(\"New memory matrix:\"))\n", "display(Markdown(\"$$\\\\b{M}_t=\"+_matrix_to_latex(M)+\"$$\"))\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example of a non-dualistic write operation\n", "\n", "The equation that determines the new content of the memory is:\n", "\n", "$$\n", "\\b{M}_t = \\b{M}_{t-1} \\circ \\left( \\b{E} - \\b{w}^\\r{w}_t \\b{e}^\\top_t \\right) + \\b{w}^\\r{w}_t \\b{v}^\\top_t\n", "$$\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/markdown": [ "Input:" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "$$\\b{M}_{t-1}=\\begin{bmatrix}-0.5 & 0.01 & 3.1 \\\\\n", "0.2 & 0.6 & 1.2 \\\\\n", "0.0 & 0.0 & 0.0 \\\\\n", "-0.1 & -0.05 & 0.0\\end{bmatrix}\\quad \\b{w}^\\r{w}_t=\\begin{bmatrix}0.0 \\\\\n", "0.8 \\\\\n", "0.1 \\\\\n", "0.1\\end{bmatrix}\\quad \\b{v}_t=\\begin{bmatrix}-1.5 \\\\\n", "-1.3 \\\\\n", "-1.1\\end{bmatrix}\\quad \\b{e}_t=\\begin{bmatrix}1.0 \\\\\n", "0.5 \\\\\n", "0.0\\end{bmatrix}\\quad \\b{E}_t=\\begin{bmatrix}1.0 & 1.0 & 1.0 \\\\\n", "1.0 & 1.0 & 1.0 \\\\\n", "1.0 & 1.0 & 1.0 \\\\\n", "1.0 & 1.0 & 1.0\\end{bmatrix}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "New memory matrix:" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "$$\\b{M}_t=\\begin{bmatrix}-0.5 & 0.01 & 3.1 \\\\\n", "-1.16 & -0.68 & 0.32 \\\\\n", "-0.15 & -0.13 & -0.11 \\\\\n", "-0.24 & -0.1775 & -0.11\\end{bmatrix}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import numpy as np\n", "\n", "# Memory at time t-1:\n", "M1= np.array([[-0.5, 0.01, 3.1],\n", " [ 0.2, 0.6, 1.2],\n", " [ 0, 0, 0 ],\n", " [-0.1, -0.05, 0]])\n", "# Write weighting:\n", "w= np.array([0,0.8,0.1,0.1]).reshape(-1,1)\n", "# Write vector:\n", "v= np.array([-1.5,-1.3,-1.1]).reshape(-1,1)\n", "# Erase vector:\n", "e= np.array([1,0.5,0]).reshape(-1,1)\n", "# Matrix of ones:\n", "E= np.ones((4,3))\n", "\n", "# Operation to obtain the memory at time t:\n", "M= M1 * (E - w @ e.T) + w @ v.T\n", "\n", "display(Markdown(\"Input:\"))\n", "display(Markdown(\"$$\\\\b{M}_{t-1}=\"+_matrix_to_latex(M1)+\"\\\\quad \\\\b{w}^\\\\r{w}_t=\"+_matrix_to_latex(w)+\n", " \"\\\\quad \\\\b{v}_t=\"+_matrix_to_latex(v)+\"\\\\quad \\\\b{e}_t=\"+_matrix_to_latex(e)+\n", " \"\\\\quad \\\\b{E}_t=\"+_matrix_to_latex(E)+\"$$\"))\n", "display(Markdown(\"New memory matrix:\"))\n", "display(Markdown(\"$$\\\\b{M}_t=\"+_matrix_to_latex(M)+\"$$\"))\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Content-based addressing\n", "\n", "The $i$-th element of $\\c{C}$ is obtained as follows:\n", "\n", "$$\n", "\\c{C}(\\b{M},\\b{k},\\beta)[i] = \\frac{\n", " \\r{exp}\\left(\\c{D}(\\b{k},\\b{M}[i,\\cdot])\\right)^\\beta\n", "}{\n", " \\sum_{j=1}^N \\r{exp}\\left(\\c{D}(\\b{k},\\b{M}[j,\\cdot])\\right)^\\beta\n", "}\n", "$$\n", "\n", "The cosine similarity $\\c{D}$ is defined as:\n", "\n", "$$\n", "\\c{D}(\\b{u},\\b{v}) = \\frac{\\b{u} \\cdot \\b{v}}{\\lVert\\b{u}\\rVert \\, \\lVert\\b{v}\\rVert}\n", "$$\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "Input:" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "$$\\b{M}=\\begin{bmatrix}-0.5 & 0.01 & 3.1 \\\\\n", "0.2 & 0.6 & 1.2\\end{bmatrix}\\quad \\b{k}=\\begin{bmatrix}0.3 \\\\\n", "0.5 \\\\\n", "1.0\\end{bmatrix}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "Cosine similarities:" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "$$\\quad\\c{D}(\\b{k},\\b{M}[1,\\cdot])=0.812951106053$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "$$\\quad\\c{D}(\\b{k},\\b{M}[2,\\cdot])=0.99348951412$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "Content-based weighting (with $\\beta$=1):" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "$$\\quad\\c{C}(\\b{M},\\b{k},\\beta=1)=\\begin{bmatrix}0.454987593255 \\\\\n", "0.545012406745\\end{bmatrix}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "Content-based weighting (with $\\beta$=10):" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "$$\\quad\\c{C}(\\b{M},\\b{k},\\beta=10)=\\begin{bmatrix}0.141196927269 \\\\\n", "0.858803072731\\end{bmatrix}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "def cosine_sim(u,v):\n", " return np.dot(u,v) / (np.sqrt(np.dot(u,u)) * np.sqrt(np.dot(v,v)))\n", "\n", "def softmax(x,beta):\n", " return np.exp(x*beta) / np.sum(np.exp(x*beta), axis=0)\n", "\n", "# Memory:\n", "M= np.array([[-0.5, 0.01, 3.1],\n", " [ 0.2, 0.6, 1.2]])\n", "# Lookup vector (represented as a row vector):\n", "k= np.array([0.3,0.5,1])\n", "# Cosine similarities:\n", "D= np.apply_along_axis(cosine_sim, 1, M, k)\n", "\n", "display(Markdown(\"Input:\"))\n", "display(Markdown(\"$$\\\\b{M}=\"+_matrix_to_latex(M)\n", " +\"\\\\quad \\\\b{k}=\"+_matrix_to_latex(k.reshape(-1,1))+\"$$\"))\n", "display(Markdown(\"Cosine similarities:\"))\n", "for index, item in enumerate(D):\n", " display(Markdown(\"$$\\\\quad\\\\c{D}(\\\\b{k},\\\\b{M}[\"+str(index+1)+\",\\\\cdot])=\"+str(item)+\"$$\"))\n", "\n", "# Key strength:\n", "beta= 1\n", "# Content-based weighting:\n", "C= softmax(D,beta)\n", "display(Markdown(\"Content-based weighting (with $\\\\beta$=\"+str(beta)+\"):\"))\n", "display(Markdown(\"$$\\\\quad\\\\c{C}(\\\\b{M},\\\\b{k},\\\\beta=\"+str(beta)+\")=\"+_matrix_to_latex(C.reshape(-1,1))+\"$$\"))\n", "\n", "# Key strength:\n", "beta= 10\n", "# Content-based weighting:\n", "C= softmax(D,beta)\n", "display(Markdown(\"Content-based weighting (with $\\\\beta$=\"+str(beta)+\"):\"))\n", "display(Markdown(\"$$\\\\quad\\\\c{C}(\\\\b{M},\\\\b{k},\\\\beta=\"+str(beta)+\")=\"+_matrix_to_latex(C.reshape(-1,1))+\"$$\"))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Computation of the allocation weighting vector\n", "\n", "Generation of the tables included in the guide for the computation of $\\b{a}_t$. Note that indexes start at one." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/markdown": [ "#### First example" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "Usage vector and resulting allocation vector:" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "$$\\b{u}_t=\\begin{bmatrix}1.0 \\\\\n", "0.0 \\\\\n", "0.8 \\\\\n", "0.4\\end{bmatrix}\\quad \\b{a}_t=\\begin{bmatrix}0.0 \\\\\n", "1.0 \\\\\n", "0.0 \\\\\n", "0.0\\end{bmatrix}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "Table with intermediate computations:" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "$$\\newcommand{\\b}[1]{\\boldsymbol{#1}}$$
$j$$\\b{u}_t[j]$$\\gamma : \\b{\\phi}_t[\\gamma] = j$$1-\\b{u}_t[j]$$\\prod_{i=1}^{\\gamma-1} \\b{u}_t[\\b{\\phi}_t[i]]$$\\b{a}_t[j]$
11400.00.0
201111
30.830.20.00.0
40.420.600.0
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "#### Second example" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "Another usage vector and resulting allocation vector:" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "$$\\b{u}_t=\\begin{bmatrix}0.4 \\\\\n", "0.6 \\\\\n", "0.2 \\\\\n", "0.5\\end{bmatrix}\\quad \\b{a}_t=\\begin{bmatrix}0.12 \\\\\n", "0.016 \\\\\n", "0.8 \\\\\n", "0.04\\end{bmatrix}$$" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "Table with intermediate computations:" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/markdown": [ "$$\\newcommand{\\b}[1]{\\boldsymbol{#1}}$$
$j$$\\b{u}_t[j]$$\\gamma : \\b{\\phi}_t[\\gamma] = j$$1-\\b{u}_t[j]$$\\prod_{i=1}^{\\gamma-1} \\b{u}_t[\\b{\\phi}_t[i]]$$\\b{a}_t[j]$
10.420.60.20.12
20.640.40.040.016
30.210.810.8
40.530.50.080.04
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "def _generate_allocation_weighting_vector (u):\n", " # Free list (with indexes starting at 0):\n", " phi0= sorted(range(len(u)), key=lambda k: u[k])\n", "\n", " a= [None]*len(u)\n", " _render_table(state=\"start\")\n", "\n", " for j in range(0,len(phi0)):\n", " gamma= phi0.index(j)\n", " prod= 1\n", " for i in range(0,gamma):\n", " prod= prod*u[phi0[i]]\n", " a[j]= (1-u[j])*prod\n", " _render_table(j+1,u[j],gamma+1,prod,a[j])\n", "\n", " s= _render_table(state=\"end\")\n", " return (a,s)\n", "\n", "\n", "from IPython.display import display, Markdown\n", "\n", "# Usage vector:\n", "u= [1,0,0.8,0.4]\n", "(a,s)= _generate_allocation_weighting_vector (u)\n", "display(Markdown(\"#### First example\"))\n", "display(Markdown(\"Usage vector and resulting allocation vector:\"))\n", "display(Markdown(\"$$\\\\b{u}_t=\"+_matrix_to_latex(np.array(u).reshape(-1,1))\n", " +\"\\\\quad \\\\b{a}_t=\"+_matrix_to_latex(np.array(a).reshape(-1,1))+\"$$\"))\n", "display(Markdown(\"Table with intermediate computations:\"))\n", "display(Markdown(s))\n", "\n", "u= [0.4,0.6,0.2,0.5]\n", "(a,s)= _generate_allocation_weighting_vector (u)\n", "display(Markdown(\"#### Second example\"))\n", "display(Markdown(\"Another usage vector and resulting allocation vector:\"))\n", "display(Markdown(\"$$\\\\b{u}_t=\"+_matrix_to_latex(np.array(u).reshape(-1,1))\n", " +\"\\\\quad \\\\b{a}_t=\"+_matrix_to_latex(np.array(a).reshape(-1,1))+\"$$\"))\n", "display(Markdown(\"Table with intermediate computations:\"))\n", "display(Markdown(s))\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.5.3" } }, "nbformat": 4, "nbformat_minor": 2 }