<commit_before>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Joint Embedding\n",
    "\n",
    "by <NAME>, reviewed by <NAME>\n",
    "\n",
    "## Setting\n",
    "\n",
    "### Task\n",
    "\n",
    "Given $\\left\\{G_i\\right\\}_{i=1}^N$, a collection of graphs with adjacency matrix $A_i \\in \\mathbb{R}^{v\\times v}$ where each graph contains the same vertex set of length $v$. We desire to embed each $G_i$ into $\\mathbb{R}^d$ by a vector $\\lambda_i \\in \\mathbb{R}^d$, where $\\mathbb{R}^d$ is a $d$ dimensional space.\n",
    "\n",
    "\n",
    "Notation:\n",
    "\n",
    "\\begin{align*}\n",
    "    H &= \\begin{bmatrix}\n",
    "        h_1^{(1)} & ...&h_d^{(1)} \\\\\n",
    "        h_1^{(2)} & ...& h_d^{(2)} \\\\\n",
    "        \\vdots & \\ddots & \\vdots \\\\\n",
    "        h_1^{(v)} & ... & h_d^{(v)}\n",
    "    \\end{bmatrix}\n",
    "\\end{align*}\n",
    "\n",
    "where each $H \\in \\mathbb{R}^{v \\times d}, h_i \\in \\mathbb{R}^v$. Here, each $h_i$ is a  norm-1 vector. Then we can use the Multiple Eigen Graphs (MREG) model to form a probability matrix. First, we define the probability of each edge as a linear combination of our $1$-norms that define our $d$-dimensional space, and our coefficients $\\lambda_i$:\n",
    "\n",
    "\\begin{align*}\n",
    "    A_i[s, t] \\sim Bern\\left(p = \\sum_{k=1}^d\\lambda_i^{(k)} h_k^{(s)}h_k^{(t)} \\right)\n",
    "\\end{align*}\n",
    "\n",
    "Then we can define the probability of each edge as:\n",
    "\n",
    "\\begin{align*}\n",
    "    P_i &= \\sum_{k=1}^d \\lambda_i^{(k)} h_k^T h_k\n",
    "\\end{align*}\n",
    "\n",
    "And we can say that our graphs are simulated from the MREG model with coefficients $\\Lambda$ and $H$:\n",
    "\n",
    "\\begin{align*}\n",
    "    \\left\\{A_i \\right\\}_{i=1}^n \\sim MREG(\\lambda, H)\n",
    "\\end{align*}\n",
    "\n",
    "that is, each graph $A_i$ is somewhere on the space spanned by $H$ given by $\\lambda_i$.\n",
    "\n",
    "### Loss Function\n",
    "\n",
    "\\begin{align*}\n",
    "    L(\\lambda, H | A) = \\sum_{i=1}^n \\left|\\left|A_i - \\sum_{k=1}^d \\lambda_i^{(k)} h_k^Th_k\\right|\\right|^2\n",
    "\\end{align*}\n",
    "\n",
    "### Statistical Goal\n",
    "\n",
    "The statistical goal is to find the norm-1 vectors $h_k$ that define the subspace, and the coefficients $\\lambda_i$ that define the unique combinations of $h_k^Th_k$ for each $A_i$ that maximize our fit to $A_i$. Stated as an optimization problem, we have:\n",
    "\n",
    "\\begin{align*}\n",
    "    \\left(\\hat{\\Lambda}, \\hat{H}\\right) &= \\textrm{argmin}_{\\lambda_i, h_k:||h_k||=1} \\sum_{i=1}^n \\left|\\left|A_i - \\sum_{k=1}^d \\lambda_i^{(k)} h_k^Th_k\\right|\\right|^2\n",
    "\\end{align*}\n",
    "\n",
    "Another problem (addressed in other papers referenced by Shangsi) is how to choose $d$, the dimensionality of the subspace we are embedding to. In experimental settings, it is fine to arbitrarily set $d$, however, in real world settings, $d$ can be chosen by computing the algorithm under a range of $d = 1,2,...D$ for some sufficiently large $D$, and then plotting the objective function given the optimal parameters selected by each $d$ and selecting $d$ where the objective roughly begins to flatten out. From a theoretical perspective, this amounts to fitting continuously larger $d$ until adding more dimensions to our subspace provides a substantially small enough improvement in the objective that we can assume that we have found optimal $d$.\n",
    "\n",
    "### Desiderata\n",
    "\n",
    "It is clear that the hypothesis class is going to be heavily non-convex. this means that any algorithms is going to have to make concessions somewhere to arrive at a robust, reliable solution in any sort of computationally-efficient time period. So our desiderata are:\n",
    "\n",
    "1. works well in theory in certain settings\n",
    "2. epirically performs well in simulation settings\n",
    "3. empirically performs well in real data scenarios\n",
    "4. is relatively fast in that it doesn't traverse the entire hypothesis set, as the hypothesis set is incredibly large.\n",
    "\n",
    "## Approach\n",
    "\n",
    "### Algorithm\n",
    "\n",
    "For the algorithm, the hypothesis set is incredibly large. We are attempting to maximize two high-dimensional quantities, $\\Lambda$ and $H$, simultaneously as we pass over each of the graphs. To accomplish this, Shangsi chooses the Alternating Descent approach; that is, maximize $\\lambda_k$ by holding $h_k$ fixed using a gradient-descent approach, and then maximize $H$ using least-squares, in an alternating fashion. We repeat this until convergence for each $k=1:d$ individually, the dimensions we are attempting to embed our graphs on.  Our algorithm is as follows:\n",
    "\n",
    "1. Set residuals $R_i^{(1)} = A_i$  // initialize our error as the graphs themselves\n",
    "2. for $k=1:d$  \n",
    "    a. Initialize $h_k, \\lambda_k$  \n",
    "    b. while not converged:\n",
    "      * Fix $\\lambda_k$, and update $h_k$ using gradient descent on $L(\\Lambda | A, H)$ by minimizing wrt $\\lambda_k$\n",
    "      * Renormalize $h_k$\n",
    "      * Fix $h_k$, and update $\\lambda_k$ using gradient descent on $L(H | A, \\Lambda)$ by minimizing wrt $h_k$\n",
    "      * Recompute $L(\\Lambda, H | A)$\n",
    "\n",
    "  c. endwhile  \n",
    "  d. Update residuals $R_i^{(k+1)} = R_i^{(k)} - \\Lambda_{i}^{(k)}h_k^Th_k$  \n",
    "3. endfor\n",
    "4. Output $\\Lambda, H$\n",
    "\n",
    "### Evaluation Methods\n",
    "\n",
    "1. Generate a simple example with a $1$ dimensional subspace and vary the number of graphs to see how effectively we can find the joint embedding by checking the mean difference between our single prediction vector and the true norm-$1$ vector.\n",
    "2. Generate pairs of related graphs and find a classifier with minimal classification error from a $2$ dimensional subspace, measuring accuracy as the correct-classification rate.\n",
    "3. Predict the composite creativity index from samples of DTI\n",
    "\n",
    "## Results Overview\n",
    "\n",
    "### Simulations\n",
    "\n",
    "#### Basic Simulation\n",
    "\n",
    "For this experiment, we generate $\\lambda_i \\sim Unif(1, 2)$ and $h_1$, our single norm vector that our graphs embed on. Then we simulate graphs $\\left\\{G_i\\right\\}_{i=1}^n \\sim MREG(\\left\\{\\lambda_i\\right\\}_{i=1}^n, h_1)$ using the $MREG$ model described above. \n",
    "\n",
    "We run the joint-embedding algorithm 100 times, and vary the number of graphs simulated from $n=2^j$ for $j=4...16$. We compute the quantity $||\\hat{h}_1^m - h_1||$, or the model bias after convergence on iteration $m$. This is simply a measure of how close we are to the expected $h_1$. As we can see in the plots below, the more graph examples we have, the more accurately we are able to predict $\\hat{h}_1$. Additionally in this simulation, we look at the model convergence $||\\hat{h}_1^n - \\hat{h}_1^{n/2}||$. This compares our $\\hat{h}_1^n$ on $n$ graphs to previous estimate $\\hat{h}_1^{n/2}$ on $n/2$ graphs, and verifies that our model converges towards a solution as we increase the number of graphs. \n",
    "\n",
    "<img=\"joint embedding simple graph\", src=\"./images/week_25/JE_simple.png\">\n",
    "\n",
    "As we can see, our estimation gets closer to the correct estimation (blue line), and our solution converges as we increase the number of iterations.\n",
    "\n",
    "#### Classification Simulation\n",
    "\n",
    "For this experiment, we generate $m$ pairs $\\left\\{(A_i, Y_i)\\right\\}_{i=1}^m$ of binarized graphs with a binarized label, $Y_i \\in \\left\\{0, 1\\right\\}$, with our embedding space defined by two vectors $h_1$ and $h_2$. To classify graphs, $m=200$ graphs are sampled and analyzed using the Joint-Embedding algorithm. A $1-$NN rule is used to classify the graphs, and prediction accuracy is measured from $m=4:200$ graphs. As can be seen below, Joint-Embedding (JE) vastly outperforms the similar algorithm Laplacian Eigenmap (LE) in terms of the classification.\n",
    "\n",
    "<img=\"JE vs LE\", src=\"./images/week_25/JE_vs_LE.png\">\n",
    "\n",
    "### Real Data\n",
    "\n",
    "For this study, Shangsi investigates the usage of the Joint-Embedding algorithm on predicting individual composite creativity index (CCI) given sets of diffusion-tensor imaging (DTI)-derived connectomes. Predictions are made by embedding 113 graphs with $d=10$, and fitting a linear model by performing:\n",
    "\n",
    "\\begin{align*}\n",
    "    CCI_i \\sim \\beta_0 + \\hat{\\lambda}^T_i \\beta + \\epsilon_i\n",
    "\\end{align*}\n",
    "\n",
    "Or a linear regression of the $CCI_i$ onto $\\lambda_i$. The model performs significantly better than the null model with a p-value of 0.0018 after performing an $F$-test. We compare this to exclusively looking at the graphs themselves, which produces a p-value of 0.0039. The first test reveals particularly that having more connections across hemispheres in particular results in greater creativity, while the experiment performed on the raw graphs only indicates that more connectivity itself is responsible for greater creativity.\n",
    "\n",
    "\n",
    "<img=\"Real Data1\", >\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {<commit_msg>added back joint embedding image on real data<commit_after>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Joint Embedding\n",
    "\n",
    "by <NAME>, reviewed by <NAME>\n",
    "\n",
    "## Setting\n",
    "\n",
    "### Task\n",
    "\n",
    "Given $\\left\\{G_i\\right\\}_{i=1}^N$, a collection of graphs with adjacency matrix $A_i \\in \\mathbb{R}^{v\\times v}$ where each graph contains the same vertex set of length $v$. We desire to embed each $G_i$ into $\\mathbb{R}^d$ by a vector $\\lambda_i \\in \\mathbb{R}^d$, where $\\mathbb{R}^d$ is a $d$ dimensional space.\n",
    "\n",
    "\n",
    "Notation:\n",
    "\n",
    "\\begin{align*}\n",
    "    H &= \\begin{bmatrix}\n",
    "        h_1^{(1)} & ...&h_d^{(1)} \\\\\n",
    "        h_1^{(2)} & ...& h_d^{(2)} \\\\\n",
    "        \\vdots & \\ddots & \\vdots \\\\\n",
    "        h_1^{(v)} & ... & h_d^{(v)}\n",
    "    \\end{bmatrix}\n",
    "\\end{align*}\n",
    "\n",
    "where each $H \\in \\mathbb{R}^{v \\times d}, h_i \\in \\mathbb{R}^v$. Here, each $h_i$ is a  norm-1 vector. Then we can use the Multiple Eigen Graphs (MREG) model to form a probability matrix. First, we define the probability of each edge as a linear combination of our $1$-norms that define our $d$-dimensional space, and our coefficients $\\lambda_i$:\n",
    "\n",
    "\\begin{align*}\n",
    "    A_i[s, t] \\sim Bern\\left(p = \\sum_{k=1}^d\\lambda_i^{(k)} h_k^{(s)}h_k^{(t)} \\right)\n",
    "\\end{align*}\n",
    "\n",
    "Then we can define the probability of each edge as:\n",
    "\n",
    "\\begin{align*}\n",
    "    P_i &= \\sum_{k=1}^d \\lambda_i^{(k)} h_k^T h_k\n",
    "\\end{align*}\n",
    "\n",
    "And we can say that our graphs are simulated from the MREG model with coefficients $\\Lambda$ and $H$:\n",
    "\n",
    "\\begin{align*}\n",
    "    \\left\\{A_i \\right\\}_{i=1}^n \\sim MREG(\\lambda, H)\n",
    "\\end{align*}\n",
    "\n",
    "that is, each graph $A_i$ is somewhere on the space spanned by $H$ given by $\\lambda_i$.\n",
    "\n",
    "### Loss Function\n",
    "\n",
    "\\begin{align*}\n",
    "    L(\\lambda, H | A) = \\sum_{i=1}^n \\left|\\left|A_i - \\sum_{k=1}^d \\lambda_i^{(k)} h_k^Th_k\\right|\\right|^2\n",
    "\\end{align*}\n",
    "\n",
    "### Statistical Goal\n",
    "\n",
    "The statistical goal is to find the norm-1 vectors $h_k$ that define the subspace, and the coefficients $\\lambda_i$ that define the unique combinations of $h_k^Th_k$ for each $A_i$ that maximize our fit to $A_i$. Stated as an optimization problem, we have:\n",
    "\n",
    "\\begin{align*}\n",
    "    \\left(\\hat{\\Lambda}, \\hat{H}\\right) &= \\textrm{argmin}_{\\lambda_i, h_k:||h_k||=1} \\sum_{i=1}^n \\left|\\left|A_i - \\sum_{k=1}^d \\lambda_i^{(k)} h_k^Th_k\\right|\\right|^2\n",
    "\\end{align*}\n",
    "\n",
    "Another problem (addressed in other papers referenced by Shangsi) is how to choose $d$, the dimensionality of the subspace we are embedding to. In experimental settings, it is fine to arbitrarily set $d$, however, in real world settings, $d$ can be chosen by computing the algorithm under a range of $d = 1,2,...D$ for some sufficiently large $D$, and then plotting the objective function given the optimal parameters selected by each $d$ and selecting $d$ where the objective roughly begins to flatten out. From a theoretical perspective, this amounts to fitting continuously larger $d$ until adding more dimensions to our subspace provides a substantially small enough improvement in the objective that we can assume that we have found optimal $d$.\n",
    "\n",
    "### Desiderata\n",
    "\n",
    "It is clear that the hypothesis class is going to be heavily non-convex. this means that any algorithms is going to have to make concessions somewhere to arrive at a robust, reliable solution in any sort of computationally-efficient time period. So our desiderata are:\n",
    "\n",
    "1. works well in theory in certain settings\n",
    "2. epirically performs well in simulation settings\n",
    "3. empirically performs well in real data scenarios\n",
    "4. is relatively fast in that it doesn't traverse the entire hypothesis set, as the hypothesis set is incredibly large.\n",
    "\n",
    "## Approach\n",
    "\n",
    "### Algorithm\n",
    "\n",
    "For the algorithm, the hypothesis set is incredibly large. We are attempting to maximize two high-dimensional quantities, $\\Lambda$ and $H$, simultaneously as we pass over each of the graphs. To accomplish this, Shangsi chooses the Alternating Descent approach; that is, maximize $\\lambda_k$ by holding $h_k$ fixed using a gradient-descent approach, and then maximize $H$ using least-squares, in an alternating fashion. We repeat this until convergence for each $k=1:d$ individually, the dimensions we are attempting to embed our graphs on.  Our algorithm is as follows:\n",
    "\n",
    "1. Set residuals $R_i^{(1)} = A_i$  // initialize our error as the graphs themselves\n",
    "2. for $k=1:d$  \n",
    "    a. Initialize $h_k, \\lambda_k$  \n",
    "    b. while not converged:\n",
    "      * Fix $\\lambda_k$, and update $h_k$ using gradient descent on $L(\\Lambda | A, H)$ by minimizing wrt $\\lambda_k$\n",
    "      * Renormalize $h_k$\n",
    "      * Fix $h_k$, and update $\\lambda_k$ using gradient descent on $L(H | A, \\Lambda)$ by minimizing wrt $h_k$\n",
    "      * Recompute $L(\\Lambda, H | A)$\n",
    "\n",
    "  c. endwhile  \n",
    "  d. Update residuals $R_i^{(k+1)} = R_i^{(k)} - \\Lambda_{i}^{(k)}h_k^Th_k$  \n",
    "3. endfor\n",
    "4. Output $\\Lambda, H$\n",
    "\n",
    "### Evaluation Methods\n",
    "\n",
    "1. Generate a simple example with a $1$ dimensional subspace and vary the number of graphs to see how effectively we can find the joint embedding by checking the mean difference between our single prediction vector and the true norm-$1$ vector.\n",
    "2. Generate pairs of related graphs and find a classifier with minimal classification error from a $2$ dimensional subspace, measuring accuracy as the correct-classification rate.\n",
    "3. Predict the composite creativity index from samples of DTI\n",
    "\n",
    "## Results Overview\n",
    "\n",
    "### Simulations\n",
    "\n",
    "#### Basic Simulation\n",
    "\n",
    "For this experiment, we generate $\\lambda_i \\sim Unif(1, 2)$ and $h_1$, our single norm vector that our graphs embed on. Then we simulate graphs $\\left\\{G_i\\right\\}_{i=1}^n \\sim MREG(\\left\\{\\lambda_i\\right\\}_{i=1}^n, h_1)$ using the $MREG$ model described above. \n",
    "\n",
    "We run the joint-embedding algorithm 100 times, and vary the number of graphs simulated from $n=2^j$ for $j=4...16$. We compute the quantity $||\\hat{h}_1^m - h_1||$, or the model bias after convergence on iteration $m$. This is simply a measure of how close we are to the expected $h_1$. As we can see in the plots below, the more graph examples we have, the more accurately we are able to predict $\\hat{h}_1$. Additionally in this simulation, we look at the model convergence $||\\hat{h}_1^n - \\hat{h}_1^{n/2}||$. This compares our $\\hat{h}_1^n$ on $n$ graphs to previous estimate $\\hat{h}_1^{n/2}$ on $n/2$ graphs, and verifies that our model converges towards a solution as we increase the number of graphs. \n",
    "\n",
    "<img=\"joint embedding simple graph\", src=\"./images/week_25/JE_simple.png\">\n",
    "\n",
    "As we can see, our estimation gets closer to the correct estimation (blue line), and our solution converges as we increase the number of iterations.\n",
    "\n",
    "#### Classification Simulation\n",
    "\n",
    "For this experiment, we generate $m$ pairs $\\left\\{(A_i, Y_i)\\right\\}_{i=1}^m$ of binarized graphs with a binarized label, $Y_i \\in \\left\\{0, 1\\right\\}$, with our embedding space defined by two vectors $h_1$ and $h_2$. To classify graphs, $m=200$ graphs are sampled and analyzed using the Joint-Embedding algorithm. A $1-$NN rule is used to classify the graphs, and prediction accuracy is measured from $m=4:200$ graphs. As can be seen below, Joint-Embedding (JE) vastly outperforms the similar algorithm Laplacian Eigenmap (LE) in terms of the classification.\n",
    "\n",
    "<img=\"JE vs LE\", src=\"./images/week_25/JE_vs_LE.png\">\n",
    "\n",
    "### Real Data\n",
    "\n",
    "For this study, Shangsi investigates the usage of the Joint-Embedding algorithm on predicting individual composite creativity index (CCI) given sets of diffusion-tensor imaging (DTI)-derived connectomes. Predictions are made by embedding 113 graphs with $d=10$, and fitting a linear model by performing:\n",
    "\n",
    "\\begin{align*}\n",
    "    CCI_i \\sim \\beta_0 + \\hat{\\lambda}^T_i \\beta + \\epsilon_i\n",
    "\\end{align*}\n",
    "\n",
    "Or a linear regression of the $CCI_i$ onto $\\lambda_i$. The model performs significantly better than the null model with a p-value of 0.0018 after performing an $F$-test. We compare this to exclusively looking at the graphs themselves, which produces a p-value of 0.0039. The first test reveals particularly that having more connections across hemispheres in particular results in greater creativity, while the experiment performed on the raw graphs only indicates that more connectivity itself is responsible for greater creativity.\n",
    "\n",
    "\n",
    "<img=\"Real Data\", src=\"./images/week_25/JE_real.png\">\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
{'fraction_non_alphanumeric': 0.16655389579971894, 'fraction_numerical': 0.010357570395045021, 'mean_word_length': 3.010436234606554, 'pattern_counts': {'":': 24, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7228351', 'n_tokens_mistral': 6330, 'n_tokens_neox': 6090, 'n_words': 2361}