{ "cells": [ { "cell_type": "raw", "metadata": {}, "source": [ "---\n", "title: Biostat/Biomath M257 Homework 4\n", "subtitle: 'Due May 12 @ 11:59PM'\n", "author: Student Name and UID\n", "date: today\n", "format:\n", " html:\n", " theme: cosmo\n", " embed-resources: true\n", " number-sections: true\n", " toc: true\n", " toc-depth: 4\n", " toc-location: left\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "System information (for reproducibility):" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Julia Version 1.8.5\n", "Commit 17cfb8e65ea (2023-01-08 06:45 UTC)\n", "Platform Info:\n", " OS: macOS (arm64-apple-darwin21.5.0)\n", " CPU: 12 × Apple M2 Max\n", " WORD_SIZE: 64\n", " LIBM: libopenlibm\n", " LLVM: libLLVM-13.0.1 (ORCJIT, apple-m1)\n", " Threads: 8 on 8 virtual cores\n", "Environment:\n", " JULIA_NUM_THREADS = 8\n", " JULIA_EDITOR = code\n" ] } ], "source": [ "versioninfo()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Load packages:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\u001b[32m\u001b[1m Activating\u001b[22m\u001b[39m project at `~/Documents/github.com/ucla-biostat-257/2023spring/hw/hw4`\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "\u001b[32m\u001b[1mStatus\u001b[22m\u001b[39m `~/Documents/github.com/ucla-biostat-257/2023spring/hw/hw4/Project.toml`\n", " \u001b[90m [6e4b80f9] \u001b[39mBenchmarkTools v1.3.2\n", " \u001b[90m [944b1d66] \u001b[39mCodecZlib v0.7.1\n", " \u001b[90m [0b1a1467] \u001b[39mKrylovKit v0.6.0\n", " \u001b[90m [bdcacae8] \u001b[39mLoopVectorization v0.12.158\n", " \u001b[90m [b51810bb] \u001b[39mMatrixDepot v1.0.10\n", " \u001b[90m [295af30f] \u001b[39mRevise v3.5.2\n", " \u001b[90m [b8865327] \u001b[39mUnicodePlots v3.5.2\n", " \u001b[90m [8bb1440f] \u001b[39mDelimitedFiles\n", " \u001b[90m [37e2e46d] \u001b[39mLinearAlgebra\n", " \u001b[90m [2f01184e] \u001b[39mSparseArrays\n" ] } ], "source": [ "using Pkg\n", "\n", "Pkg.activate(pwd())\n", "Pkg.instantiate()\n", "Pkg.status()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We are going to try different numerical methods learnt in class on the [Google PageRank problem](https://en.wikipedia.org/wiki/PageRank)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q1 (5 pts) Recognize structure\n", "\n", "Let $\\mathbf{A} \\in \\{0,1\\}^{n \\times n}$ be the connectivity matrix of $n$ web pages with entries\n", "$$\n", "\\begin{eqnarray*}\n", "\ta_{ij}= \\begin{cases}\n", "\t1 & \\text{if page $i$ links to page $j$} \\\\\n", "\t0 & \\text{otherwise}\n", "\t\\end{cases}.\n", "\\end{eqnarray*}\n", "$$\n", "$r_i = \\sum_j a_{ij}$ is the out-degree of page $i$. That is $r_i$ is the number of links on page $i$. Imagine a random surfer exploring the space of $n$ pages according to the following rules. \n", "\n", "- From a page $i$ with $r_i>0$\n", " * with probability $p$, (s)he randomly chooses a link on page $i$ (uniformly) and follows that link to the next page \n", " * with probability $1-p$, (s)he randomly chooses one page from the set of all $n$ pages (uniformly) and proceeds to that page \n", "- From a page $i$ with $r_i=0$ (a dangling page), (s)he randomly chooses one page from the set of all $n$ pages (uniformly) and proceeds to that page \n", " \n", "The process defines a Markov chain on the space of $n$ pages. Write the transition matrix $\\mathbf{P}$ of the Markov chain as a sparse matrix plus rank 1 matrix." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q2 Relate to numerical linear algebra\n", "\n", "According to standard Markov chain theory, the (random) position of the surfer converges to the stationary distribution $\\mathbf{x} = (x_1,\\ldots,x_n)^T$ of the Markov chain. $x_i$ has the natural interpretation of the proportion of times the surfer visits page $i$ in the long run. Therefore $\\mathbf{x}$ serves as page ranks: a higher $x_i$ means page $i$ is more visited. It is well-known that $\\mathbf{x}$ is the left eigenvector corresponding to the top eigenvalue 1 of the transition matrix $\\mathbf{P}$. That is $\\mathbf{P}^T \\mathbf{x} = \\mathbf{x}$. Therefore $\\mathbf{x}$ can be solved as an **eigen-problem**. It can also be cast as **solving a linear system**. Since the row sums of $\\mathbf{P}$ are 1, $\\mathbf{P}$ is rank deficient. We can replace the first equation by the $\\sum_{i=1}^n x_i = 1$.\n", "\n", "Hint: For iterative solvers, we don't need to replace the 1st equation. We can use the matrix $\\mathbf{I} - \\mathbf{P}^T$ directly if we start with a vector with all positive entries." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q3 (10 pts) Explore data\n", "\n", "Obtain the connectivity matrix `A` from the `SNAP/web-Google` data in the MatrixDepot package. " ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\u001b[36m\u001b[1m[ \u001b[22m\u001b[39m\u001b[36m\u001b[1mInfo: \u001b[22m\u001b[39mverify download of index files...\n", "\u001b[36m\u001b[1m[ \u001b[22m\u001b[39m\u001b[36m\u001b[1mInfo: \u001b[22m\u001b[39mreading database\n", "\u001b[36m\u001b[1m[ \u001b[22m\u001b[39m\u001b[36m\u001b[1mInfo: \u001b[22m\u001b[39madding metadata...\n", "\u001b[36m\u001b[1m[ \u001b[22m\u001b[39m\u001b[36m\u001b[1mInfo: \u001b[22m\u001b[39madding svd data...\n", "\u001b[36m\u001b[1m[ \u001b[22m\u001b[39m\u001b[36m\u001b[1mInfo: \u001b[22m\u001b[39mwriting database\n", "\u001b[36m\u001b[1m[ \u001b[22m\u001b[39m\u001b[36m\u001b[1mInfo: \u001b[22m\u001b[39mused remote sites are sparse.tamu.edu with MAT index and math.nist.gov with HTML index\n" ] }, { "data": { "text/latex": [ "\\section{SNAP/web-Google}\n", "\\subsubparagraph{MatrixMarket matrix coordinate pattern general}\n", "\\rule{\\textwidth}{1pt}\n", "\\begin{itemize}\n", "\\item UF Sparse Matrix Collection, Tim Davis\n", "\n", "\n", "\\item http://www.cise.ufl.edu/research/sparse/matrices/SNAP/web-Google\n", "\n", "\n", "\\item name: SNAP/web-Google\n", "\n", "\n", "\\item [Web graph from Google]\n", "\n", "\n", "\\item id: 2301\n", "\n", "\n", "\\item date: 2002\n", "\n", "\n", "\\item author: Google\n", "\n", "\n", "\\item ed: J. Leskovec\n", "\n", "\n", "\\item fields: name title A id date author ed kind notes\n", "\n", "\n", "\\item kind: directed graph\n", "\n", "\\end{itemize}\n", "\\rule{\\textwidth}{1pt}\n", "\\begin{itemize}\n", "\\item notes:\n", "\n", "\n", "\\item Networks from SNAP (Stanford Network Analysis Platform) Network Data Sets, \n", "\n", "\n", "\\item Jure Leskovec http://snap.stanford.edu/data/index.html \n", "\n", "\n", "\\item email jure at cs.stanford.edu \n", "\n", "\n", "\\item \n", "\\item Google web graph \n", "\n", "\n", "\\item \n", "\\item Dataset information \n", "\n", "\n", "\\item \n", "\\item Nodes represent web pages and directed edges represent hyperlinks between them.\n", "\n", "\n", "\\item The data was released in 2002 by Google as a part of Google Programming \n", "\n", "\n", "\\item Contest. \n", "\n", "\n", "\\item \n", "\\item Dataset statistics \n", "\n", "\n", "\\item Nodes 875713 \n", "\n", "\n", "\\item Edges 5105039 \n", "\n", "\n", "\\item Nodes in largest WCC 855802 (0.977) \n", "\n", "\n", "\\item Edges in largest WCC 5066842 (0.993) \n", "\n", "\n", "\\item Nodes in largest SCC 434818 (0.497) \n", "\n", "\n", "\\item Edges in largest SCC 3419124 (0.670) \n", "\n", "\n", "\\item Average clustering coefficient 0.6047 \n", "\n", "\n", "\\item Number of triangles 13391903 \n", "\n", "\n", "\\item Fraction of closed triangles 0.05523 \n", "\n", "\n", "\\item Diameter (longest shortest path) 22 \n", "\n", "\n", "\\item 90-percentile effective diameter 8.1 \n", "\n", "\n", "\\item \n", "\\item Source (citation) \n", "\n", "\n", "\\item \n", "\\item J. Leskovec, K. Lang, A. Dasgupta, M. Mahoney. Community Structure in Large \n", "\n", "\n", "\\item Networks: Natural Cluster Sizes and the Absence of Large Well-Defined Clusters.\n", "\n", "\n", "\\item arXiv.org:0810.1355, 2008. \n", "\n", "\n", "\\item \n", "\\item Google programming contest, 2002 \n", "\n", "\n", "\\item http://www.google.com/programming-contest/ \n", "\n", "\n", "\\item \n", "\\item Files \n", "\n", "\n", "\\item File Description \n", "\n", "\n", "\\item web-Google.txt.gz Webgraph from the Google programming contest, 2002 \n", "\n", "\\end{itemize}\n", "\\rule{\\textwidth}{1pt}\n", "916428 916428 5105039\n", "\n" ], "text/markdown": [ "# SNAP/web-Google\n", "\n", "###### MatrixMarket matrix coordinate pattern general\n", "\n", "---\n", "\n", " * UF Sparse Matrix Collection, Tim Davis\n", " * http://www.cise.ufl.edu/research/sparse/matrices/SNAP/web-Google\n", " * name: SNAP/web-Google\n", " * [Web graph from Google]\n", " * id: 2301\n", " * date: 2002\n", " * author: Google\n", " * ed: J. Leskovec\n", " * fields: name title A id date author ed kind notes\n", " * kind: directed graph\n", "\n", "---\n", "\n", " * notes:\n", " * Networks from SNAP (Stanford Network Analysis Platform) Network Data Sets,\n", " * Jure Leskovec http://snap.stanford.edu/data/index.html\n", " * email jure at cs.stanford.edu\n", " * \n", " * Google web graph\n", " * \n", " * Dataset information\n", " * \n", " * Nodes represent web pages and directed edges represent hyperlinks between them.\n", " * The data was released in 2002 by Google as a part of Google Programming\n", " * Contest.\n", " * \n", " * Dataset statistics\n", " * Nodes 875713\n", " * Edges 5105039\n", " * Nodes in largest WCC 855802 (0.977)\n", " * Edges in largest WCC 5066842 (0.993)\n", " * Nodes in largest SCC 434818 (0.497)\n", " * Edges in largest SCC 3419124 (0.670)\n", " * Average clustering coefficient 0.6047\n", " * Number of triangles 13391903\n", " * Fraction of closed triangles 0.05523\n", " * Diameter (longest shortest path) 22\n", " * 90-percentile effective diameter 8.1\n", " * \n", " * Source (citation)\n", " * \n", " * J. Leskovec, K. Lang, A. Dasgupta, M. Mahoney. Community Structure in Large\n", " * Networks: Natural Cluster Sizes and the Absence of Large Well-Defined Clusters.\n", " * arXiv.org:0810.1355, 2008.\n", " * \n", " * Google programming contest, 2002\n", " * http://www.google.com/programming-contest/\n", " * \n", " * Files\n", " * File Description\n", " * web-Google.txt.gz Webgraph from the Google programming contest, 2002\n", "\n", "---\n", "\n", "916428 916428 5105039\n" ], "text/plain": [ "\u001b[1m SNAP/web-Google\u001b[22m\n", "\u001b[1m ≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡\u001b[22m\n", "\n", "\u001b[1m MatrixMarket matrix coordinate pattern general\u001b[22m\n", "\n", " ────────────────────────────────────────────────────────────────────────────\n", "\n", " • UF Sparse Matrix Collection, Tim Davis\n", "\n", " • http://www.cise.ufl.edu/research/sparse/matrices/SNAP/web-Google\n", "\n", " • name: SNAP/web-Google\n", "\n", " • [Web graph from Google]\n", "\n", " • id: 2301\n", "\n", " • date: 2002\n", "\n", " • author: Google\n", "\n", " • ed: J. Leskovec\n", "\n", " • fields: name title A id date author ed kind notes\n", "\n", " • kind: directed graph\n", "\n", " ────────────────────────────────────────────────────────────────────────────\n", "\n", " • notes:\n", "\n", " • Networks from SNAP (Stanford Network Analysis Platform) Network\n", " Data Sets,\n", "\n", " • Jure Leskovec http://snap.stanford.edu/data/index.html\n", "\n", " • email jure at cs.stanford.edu\n", "\n", " • \n", "\n", " • Google web graph\n", "\n", " • \n", "\n", " • Dataset information\n", "\n", " • \n", "\n", " • Nodes represent web pages and directed edges represent hyperlinks\n", " between them.\n", "\n", " • The data was released in 2002 by Google as a part of Google\n", " Programming\n", "\n", " • Contest.\n", "\n", " • \n", "\n", " • Dataset statistics\n", "\n", " • Nodes 875713\n", "\n", " • Edges 5105039\n", "\n", " • Nodes in largest WCC 855802 (0.977)\n", "\n", " • Edges in largest WCC 5066842 (0.993)\n", "\n", " • Nodes in largest SCC 434818 (0.497)\n", "\n", " • Edges in largest SCC 3419124 (0.670)\n", "\n", " • Average clustering coefficient 0.6047\n", "\n", " • Number of triangles 13391903\n", "\n", " • Fraction of closed triangles 0.05523\n", "\n", " • Diameter (longest shortest path) 22\n", "\n", " • 90-percentile effective diameter 8.1\n", "\n", " • \n", "\n", " • Source (citation)\n", "\n", " • \n", "\n", " • J. Leskovec, K. Lang, A. Dasgupta, M. Mahoney. Community Structure\n", " in Large\n", "\n", " • Networks: Natural Cluster Sizes and the Absence of Large\n", " Well-Defined Clusters.\n", "\n", " • arXiv.org:0810.1355, 2008.\n", "\n", " • \n", "\n", " • Google programming contest, 2002\n", "\n", " • http://www.google.com/programming-contest/\n", "\n", " • \n", "\n", " • Files\n", "\n", " • File Description\n", "\n", " • web-Google.txt.gz Webgraph from the Google programming contest,\n", " 2002\n", "\n", " ────────────────────────────────────────────────────────────────────────────\n", "\n", " 916428 916428 5105039" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using MatrixDepot\n", "\n", "md = mdopen(\"SNAP/web-Google\")\n", "# display documentation for the SNAP/web-Google data\n", "mdinfo(md)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "916428×916428 SparseArrays.SparseMatrixCSC{Bool, Int64} with 5105039 stored entries:\n", "⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿\n", "⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿\n", "⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿\n", "⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿\n", "⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿\n", "⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿\n", "⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿\n", "⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿\n", "⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿\n", "⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿\n", "⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿\n", "⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿\n", "⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿\n", "⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿\n", "⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿\n", "⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿\n", "⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿\n", "⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿\n", "⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿\n", "⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# connectivity matrix\n", "A = md.A" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compute summary statistics: \n", "\n", "* How much memory does `A` take? If converted to a `Matrix{Float64}` (don't do it!), how much memory will it take? \n", "\n", "* number of web pages\n", "\n", "* number of edges (web links)\n", "\n", "* number of dangling nodes (pages with no out links)\n", "\n", "* histogram of in-degrees \n", "\n", "* list the top 20 pages with the largest in-degrees? \n", "\n", "* histogram of out-degrees\n", "\n", "* which the top 20 pages with the largest out-degrees?\n", "\n", "* visualize the sparsity pattern of $\\mathbf{A}$ or a submatrix of $\\mathbf{A}$ say `A[1:10000, 1:10000]`. \n", "\n", "**Hint**: For plots, you can use the [UnicodePlots.jl](https://github.com/Evizero/UnicodePlots.jl) package (`spy`, `histogram`, etc), which is fast for large data. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q4 (5 pts) Dense linear algebra? \n", "\n", "Consider the following methods to obtain the page ranks of the `SNAP/web-Google` data. \n", "\n", "1. A dense linear system solver such as LU decomposition. \n", "2. A dense eigen-solver for asymmetric matrix. \n", "\n", "For the LU approach, estimate (1) the memory usage and (2) how long it will take assuming that the LAPACK functions can achieve the theoretical throughput of your computer. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q5 (75 pts) Iterative solvers\n", "\n", "Set the _teleportation_ parameter at $p = 0.85$. Consider the following methods for solving the PageRank problem. \n", "\n", "1. An iterative linear system solver such as GMRES. \n", "2. An iterative eigen-solver such as Arnoldi method.\n", "\n", "For iterative methods, we have many choices in Julia. See a list of existing Julia packages for linear solvers at this [page](https://jutho.github.io/KrylovKit.jl/stable/#Package-features-and-alternatives-1). The start-up code below uses the [KrylovKit.jl](https://github.com/Jutho/KrylovKit.jl) package. You can use other packages if you prefer. Make sure to utilize the special structure of $\\mathbf{P}$ (sparse + rank 1) to speed up the matrix-vector multiplication. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Step 1 (15 pts)\n", "\n", "Let's implement a type `PageRankImPt` that mimics the matrix $\\mathbf{M} = \\mathbf{I} - \\mathbf{P}^T$. For iterative methods, all we need to provide are methods for evaluating $\\mathbf{M} \\mathbf{v}$ and $\\mathbf{M}^T \\mathbf{v}$ for arbitrary vector $\\mathbf{v}$." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "using BenchmarkTools, LinearAlgebra, SparseArrays, Revise\n", "\n", "# a type for the matrix M = I - P^T in PageRank problem\n", "struct PageRankImPt{TA <: Number, IA <: Integer, T <: AbstractFloat} <: AbstractMatrix{T}\n", " A :: SparseMatrixCSC{TA, IA} # adjacency matrix\n", " telep :: T\n", " # working arrays\n", " # TODO: whatever intermediate arrays you may want to pre-allocate\n", "end\n", "\n", "# constructor\n", "function PageRankImPt(A::SparseMatrixCSC, telep::T) where T <: AbstractFloat\n", " n = size(A, 1)\n", " # TODO: initialize and pre-allocate arrays\n", " PageRankImPt(A, telep)\n", "end\n", "\n", "LinearAlgebra.issymmetric(::PageRankImPt) = false\n", "Base.size(M::PageRankImPt) = size(M.A)\n", "# TODO: implement this function for evaluating M[i, j]\n", "Base.getindex(M::PageRankImPt, i, j) = M.telep\n", "\n", "# overwrite `out` by `(I - Pt) * v`\n", "function LinearAlgebra.mul!(\n", " out :: Vector{T}, \n", " M :: PageRankImPt{<:Number, <:Integer, T}, \n", " v :: Vector{T}\n", " ) where T <: AbstractFloat\n", " # TODO: implement mul!(out, M, v)\n", " sleep(1e-2) # wait 10 ms as if your code takes 1ms\n", " return out\n", "end\n", "\n", "# overwrite `out` by `(I - P) * v`\n", "function LinearAlgebra.mul!(\n", " out :: Vector{T}, \n", " Mt :: Transpose{T, PageRankImPt{TA, IA, T}}, \n", " v :: Vector{T}\n", " ) where {TA<:Number, IA<:Integer, T <: AbstractFloat}\n", " M = Mt.parent\n", " # TODO: implement mul!(out, transpose(M), v)\n", " sleep(1e-2) # wait 10 ms as if your code takes 1ms\n", " out\n", "end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To check correctness. Note \n", "$$\n", "\\mathbf{M}^T \\mathbf{1} = \\mathbf{0}\n", "$$\n", "and\n", "$$\n", "\\mathbf{M} \\mathbf{x} = \\mathbf{0}\n", "$$\n", "for stationary distribution $\\mathbf{x}$.\n", "\n", "Download the solution file `pgrksol.csv.gz`. **Do not put this file in your Git**. You will lose points if you do. You can add a line `pgrksol.csv.gz` to your `.gitignore` file." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "916428-element Vector{Float64}:\n", " 3.3783428216975054e-5\n", " 2.0710155392568165e-6\n", " 3.663065984832893e-6\n", " 7.527510785028837e-7\n", " 8.63328599674051e-7\n", " 1.769418252415541e-6\n", " 2.431230382883396e-7\n", " 6.368417180141445e-7\n", " 4.744973703681939e-7\n", " 2.6895486110647536e-7\n", " 3.18574314847409e-6\n", " 7.375106374416742e-7\n", " 2.431230382883396e-7\n", " ⋮\n", " 1.1305006040148547e-6\n", " 4.874825281822915e-6\n", " 3.167946973112519e-6\n", " 9.72688040308568e-7\n", " 6.588614479285245e-7\n", " 7.737011774300648e-7\n", " 2.431230382883396e-7\n", " 1.6219204214797293e-6\n", " 3.912130060551738e-7\n", " 2.431230382883396e-7\n", " 7.296033831163157e-6\n", " 6.330939996912478e-7" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using CodecZlib, DelimitedFiles\n", "\n", "isfile(\"pgrksol.csv.gz\") || download(\"https://raw.githubusercontent.com/ucla-biostat-257/2023spring/master/hw/hw4/pgrksol.csv.gz\")\n", "xsol = open(\"pgrksol.csv.gz\", \"r\") do io\n", " vec(readdlm(GzipDecompressorStream(io)))\n", "end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**You will lose all 35 points (Steps 1 and 2)** if the following statements throw AssertError." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "M = PageRankImPt(A, 0.85)\n", "n = size(M, 1)\n", "\n", "#@assert transpose(M) * ones(n) ≈ zeros(n)\n", "@assert norm(transpose(M) * ones(n)) < 1e-12" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "#@assert M * xsol ≈ zeros(n)\n", "@assert norm(M * xsol) < 1e-12" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Step 2 (20 pts)\n", "\n", "We want to benchmark the hot functions `mul!` to make sure they are efficient and allocate no memory." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "BenchmarkTools.Trial: 373 samples with 1 evaluation.\n", " Range \u001b[90m(\u001b[39m\u001b[36m\u001b[1mmin\u001b[22m\u001b[39m … \u001b[35mmax\u001b[39m\u001b[90m): \u001b[39m\u001b[36m\u001b[1m10.912 ms\u001b[22m\u001b[39m … \u001b[35m 12.788 ms\u001b[39m \u001b[90m┊\u001b[39m GC \u001b[90m(\u001b[39mmin … max\u001b[90m): \u001b[39m0.00% … 0.00%\n", " Time \u001b[90m(\u001b[39m\u001b[34m\u001b[1mmedian\u001b[22m\u001b[39m\u001b[90m): \u001b[39m\u001b[34m\u001b[1m12.061 ms \u001b[22m\u001b[39m\u001b[90m┊\u001b[39m GC \u001b[90m(\u001b[39mmedian\u001b[90m): \u001b[39m0.00%\n", " Time \u001b[90m(\u001b[39m\u001b[32m\u001b[1mmean\u001b[22m\u001b[39m ± \u001b[32mσ\u001b[39m\u001b[90m): \u001b[39m\u001b[32m\u001b[1m11.956 ms\u001b[22m\u001b[39m ± \u001b[32m302.479 μs\u001b[39m \u001b[90m┊\u001b[39m GC \u001b[90m(\u001b[39mmean ± σ\u001b[90m): \u001b[39m0.00% ± 0.00%\n", "\n", " \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[32m \u001b[39m\u001b[39m \u001b[39m \u001b[39m▆\u001b[34m█\u001b[39m\u001b[39m▅\u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \n", " \u001b[39m▄\u001b[39m▁\u001b[39m▁\u001b[39m▁\u001b[39m▁\u001b[39m▆\u001b[39m▅\u001b[39m▆\u001b[39m▆\u001b[39m▄\u001b[39m▁\u001b[39m▅\u001b[39m▆\u001b[39m▆\u001b[39m▄\u001b[39m▅\u001b[39m▆\u001b[39m█\u001b[39m█\u001b[39m▆\u001b[39m▄\u001b[39m▄\u001b[39m▅\u001b[39m▅\u001b[39m▁\u001b[39m▁\u001b[39m▁\u001b[39m▅\u001b[39m▅\u001b[39m▅\u001b[39m▁\u001b[39m▁\u001b[39m▁\u001b[39m▅\u001b[39m▄\u001b[39m▁\u001b[39m▄\u001b[39m▄\u001b[39m▆\u001b[32m▁\u001b[39m\u001b[39m▄\u001b[39m▇\u001b[39m█\u001b[34m█\u001b[39m\u001b[39m█\u001b[39m█\u001b[39m▇\u001b[39m▆\u001b[39m▅\u001b[39m▄\u001b[39m▄\u001b[39m▄\u001b[39m▅\u001b[39m▁\u001b[39m▁\u001b[39m▁\u001b[39m▁\u001b[39m▄\u001b[39m▁\u001b[39m▆\u001b[39m \u001b[39m▆\n", " 10.9 ms\u001b[90m \u001b[39m\u001b[90mHistogram: \u001b[39m\u001b[90m\u001b[1mlog(\u001b[22m\u001b[39m\u001b[90mfrequency\u001b[39m\u001b[90m\u001b[1m)\u001b[22m\u001b[39m\u001b[90m by time\u001b[39m 12.5 ms \u001b[0m\u001b[1m<\u001b[22m\n", "\n", " Memory estimate\u001b[90m: \u001b[39m\u001b[33m144 bytes\u001b[39m, allocs estimate\u001b[90m: \u001b[39m\u001b[33m5\u001b[39m." ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M = PageRankImPt(A, 0.85)\n", "n = size(M, 1)\n", "v, out = ones(n), zeros(n)\n", "bm_mv = @benchmark mul!($out, $M, $v) setup=(fill!(out, 0); fill!(v, 1))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "BenchmarkTools.Trial: 374 samples with 1 evaluation.\n", " Range \u001b[90m(\u001b[39m\u001b[36m\u001b[1mmin\u001b[22m\u001b[39m … \u001b[35mmax\u001b[39m\u001b[90m): \u001b[39m\u001b[36m\u001b[1m10.966 ms\u001b[22m\u001b[39m … \u001b[35m 12.571 ms\u001b[39m \u001b[90m┊\u001b[39m GC \u001b[90m(\u001b[39mmin … max\u001b[90m): \u001b[39m0.00% … 0.00%\n", " Time \u001b[90m(\u001b[39m\u001b[34m\u001b[1mmedian\u001b[22m\u001b[39m\u001b[90m): \u001b[39m\u001b[34m\u001b[1m12.058 ms \u001b[22m\u001b[39m\u001b[90m┊\u001b[39m GC \u001b[90m(\u001b[39mmedian\u001b[90m): \u001b[39m0.00%\n", " Time \u001b[90m(\u001b[39m\u001b[32m\u001b[1mmean\u001b[22m\u001b[39m ± \u001b[32mσ\u001b[39m\u001b[90m): \u001b[39m\u001b[32m\u001b[1m11.960 ms\u001b[22m\u001b[39m ± \u001b[32m272.533 μs\u001b[39m \u001b[90m┊\u001b[39m GC \u001b[90m(\u001b[39mmean ± σ\u001b[90m): \u001b[39m0.00% ± 0.00%\n", "\n", " \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[32m \u001b[39m\u001b[39m \u001b[39m \u001b[39m▄\u001b[34m█\u001b[39m\u001b[39m▄\u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \u001b[39m \n", " \u001b[39m▄\u001b[39m▁\u001b[39m▁\u001b[39m▁\u001b[39m▆\u001b[39m▅\u001b[39m▅\u001b[39m▅\u001b[39m▁\u001b[39m▆\u001b[39m▄\u001b[39m▆\u001b[39m▅\u001b[39m▅\u001b[39m▄\u001b[39m▆\u001b[39m█\u001b[39m▇\u001b[39m▄\u001b[39m▄\u001b[39m▆\u001b[39m▅\u001b[39m▁\u001b[39m▄\u001b[39m▄\u001b[39m▄\u001b[39m▅\u001b[39m▅\u001b[39m▅\u001b[39m▅\u001b[39m▁\u001b[39m▆\u001b[39m▅\u001b[39m▄\u001b[39m▁\u001b[39m▆\u001b[39m▄\u001b[39m▅\u001b[32m▁\u001b[39m\u001b[39m▄\u001b[39m▅\u001b[39m█\u001b[34m█\u001b[39m\u001b[39m█\u001b[39m█\u001b[39m▇\u001b[39m▁\u001b[39m▁\u001b[39m▁\u001b[39m▄\u001b[39m▄\u001b[39m▁\u001b[39m▁\u001b[39m▁\u001b[39m▄\u001b[39m▁\u001b[39m▁\u001b[39m▁\u001b[39m▁\u001b[39m▅\u001b[39m \u001b[39m▆\n", " 11 ms\u001b[90m \u001b[39m\u001b[90mHistogram: \u001b[39m\u001b[90m\u001b[1mlog(\u001b[22m\u001b[39m\u001b[90mfrequency\u001b[39m\u001b[90m\u001b[1m)\u001b[22m\u001b[39m\u001b[90m by time\u001b[39m 12.5 ms \u001b[0m\u001b[1m<\u001b[22m\n", "\n", " Memory estimate\u001b[90m: \u001b[39m\u001b[33m144 bytes\u001b[39m, allocs estimate\u001b[90m: \u001b[39m\u001b[33m5\u001b[39m." ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bm_mtv = @benchmark mul!($out, $(transpose(M)), $v) setup=(fill!(out, 0); fill!(v, 1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You will lose 1 point for each 100 bytes memory allocation. So the points you will get is" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "17.12" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "clamp(10 - median(bm_mv).memory / 100, 0, 10) + \n", "clamp(10 - median(bm_mtv).memory / 100, 0, 10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Hint**: My median run times are about 10 ms and memory allocations are 0 bytes." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Step 3 (20 pts)\n", "\n", "Let's first try to solve the PageRank problem by the GMRES method for solving linear equations. " ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "(value = ([3.5373439728225696e-5, 1.1625074089088258e-6, 7.4732619144138796e-6, 6.642899479479004e-7, 9.964349219218506e-7, 2.6571597917916015e-6, 1.660724869869751e-7, 6.642899479479004e-7, 3.321449739739502e-7, 3.321449739739502e-7 … 2.989304765765552e-6, 1.3285798958958008e-6, 2.4910873048046265e-6, 4.982174609609253e-7, 1.660724869869751e-7, 2.4910873048046265e-6, 3.321449739739502e-7, 1.660724869869751e-7, 7.4732619144138796e-6, 4.982174609609253e-7], ConvergenceInfo: one converged value after 0 iterations and 1 applications of the linear map;\n", "norms of residuals are given by (2.4845421886e-314,).\n", "), time = 0.042158375, bytes = 27077034, gctime = 0.009942417, gcstats = Base.GC_Diff(27077034, 3, 0, 87986, 2, 50, 9942417, 1, 0))" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using KrylovKit\n", "\n", "# normalize in-degrees to be the start point\n", "x0 = vec(sum(A, dims = 1)) .+ 1.0\n", "x0 ./= sum(x0)\n", "\n", "# right hand side\n", "b = zeros(n)\n", "\n", "# warm up (compilation)\n", "linsolve(M, b, x0, issymmetric = false, isposdef = false, maxiter = 1) \n", "# output is complex eigenvalue/eigenvector\n", "(x_gmres, info), time_gmres, = @timed linsolve(M, b, x0, issymmetric = false, isposdef = false)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check correctness. **You will lose all 20 points if the following statement throws `AssertError`.**" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "tags": [] }, "outputs": [ { "ename": "LoadError", "evalue": "AssertionError: norm(x_gmres - xsol) < 1.0e-8", "output_type": "error", "traceback": [ "AssertionError: norm(x_gmres - xsol) < 1.0e-8", "", "Stacktrace:", " [1] top-level scope", " @ In[13]:1" ] } ], "source": [ "@assert norm(x_gmres - xsol) < 1e-8" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "GMRES should be reasonably fast. The points you'll get is" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20.0" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "clamp(20 / time_gmres * 20, 0, 20)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Hint**: My runtime is about 3-4 seconds." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Step 4 (20 pts)\n", "\n", "Let's first try to solve the PageRank problem by the Arnoldi method for solving eigen problems. " ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(value = ([0.0], [[0.005716124042701269, 0.00018785384177891497, 0.0012076318400073105, 0.00010734505244509426, 0.00016101757866764137, 0.00042938020978037703, 2.6836263111273564e-5, 0.00010734505244509426, 5.367252622254713e-5, 5.367252622254713e-5 … 0.00048305273600292417, 0.00021469010489018851, 0.00040254394666910346, 8.050878933382069e-5, 2.6836263111273564e-5, 0.00040254394666910346, 5.367252622254713e-5, 2.6836263111273564e-5, 0.0012076318400073105, 8.050878933382069e-5]], ConvergenceInfo: one converged value after 1 iterations and 1 applications of the linear map;\n", "norms of residuals are given by (0.0,).\n", "), time = 0.030408917, bytes = 32416471, gctime = 0.0, gcstats = Base.GC_Diff(32416471, 4, 0, 54556, 19, 0, 0, 0, 0))" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# warm up (compilation)\n", "eigsolve(M, x0, 1, :SR, issymmetric = false, maxiter = 1)\n", "# output is complex eigenvalue/eigenvector\n", "(vals, vecs, info), time_arnoldi, = @timed eigsolve(M, x0, 1, :SR, issymmetric = false)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check correctness. **You will lose all 20 points if the following statement throws `AssertError`.**" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "@assert abs(Real(vals[1])) < 1e-8" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "ename": "LoadError", "evalue": "AssertionError: norm(x_arnoldi - xsol) < 1.0e-8", "output_type": "error", "traceback": [ "AssertionError: norm(x_arnoldi - xsol) < 1.0e-8", "", "Stacktrace:", " [1] top-level scope", " @ In[17]:3" ] } ], "source": [ "x_arnoldi = abs.(Real.(vecs[1]))\n", "x_arnoldi ./= sum(x_arnoldi)\n", "@assert norm(x_arnoldi - xsol) < 1e-8" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Arnoldi should be reasonably fast. The points you'll get is" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20.0" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "clamp(20 / time_arnoldi * 20, 0, 20)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Hint**: My runtime is about 6-7 seconds." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q6 (5 pts) Results\n", "\n", "List the top 20 pages you found and their corresponding PageRank score. Do they match the top 20 pages ranked according to in-degrees? " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q7 Be proud of yourself\n", "\n", "Go to your resume/cv and claim you have experience performing analysis on a network of one million nodes." ] } ], "metadata": { "@webio": { "lastCommId": null, "lastKernelId": null }, "jupytext": { "formats": "ipynb,qmd" }, "kernelspec": { "display_name": "Julia (8 threads) 1.8.5", "language": "julia", "name": "julia-_8-threads_-1.8" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.8.5" }, "toc": { "colors": { "hover_highlight": "#DAA520", "running_highlight": "#FF0000", "selected_highlight": "#FFD700" }, "moveMenuLeft": true, "nav_menu": { "height": "87px", "width": "252px" }, "navigate_menu": true, "number_sections": false, "sideBar": true, "skip_h1_title": true, "threshold": 4, "toc_cell": false, "toc_section_display": "block", "toc_window_display": true, "widenNotebook": false } }, "nbformat": 4, "nbformat_minor": 4 }