<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>SaddlePoint — Soma S Dhavala</title>
<link>https://dhavala.github.io/saddlepoint/</link>
<atom:link href="https://dhavala.github.io/saddlepoint/index.xml" rel="self" type="application/rss+xml"/>
<description>Academic and technical writing on statistics, ML, and adjacent topics.</description>
<image>
<url>https://dhavala.github.io/images/og-image.png</url>
<title>SaddlePoint — Soma S Dhavala</title>
<link>https://dhavala.github.io/saddlepoint/</link>
<height>76</height>
<width>144</width>
</image>
<generator>quarto-1.9.37</generator>
<lastBuildDate>Sun, 17 May 2026 00:00:00 GMT</lastBuildDate>
<item>
  <title>PageRank and KNN: an equivalence</title>
  <dc:creator>Soma S Dhavala</dc:creator>
  <link>https://dhavala.github.io/saddlepoint/pagerank-knn/</link>
  <description><![CDATA[ 
<div class="section-banner">
  <img src="https://dhavala.github.io/images/banners/horsesskybw.jpg" alt="Blog banner">
</div>




<div id="cell-1" class="cell" data-execution_count="59">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># https://github.com/rfsantacruz/cvx-nb/blob/master/DoublyStochasticApproximation.ipynb</span></span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># approximate dh-matrix</span></span>
<span id="cb1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb1-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> networkx <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> nx</span>
<span id="cb1-6"></span>
<span id="cb1-7"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> matplotlib <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pyplot <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> plt</span>
<span id="cb1-8"><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span>matplotlib inline</span>
<span id="cb1-9"></span>
<span id="cb1-10"></span>
<span id="cb1-11"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> sinkhorn(X, max_it<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>):</span>
<span id="cb1-12">    Y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.copy(X)</span>
<span id="cb1-13">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> it <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(max_it):</span>
<span id="cb1-14">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Line multipliers normalizer</span></span>
<span id="cb1-15">        D1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.diagflat(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(Y, axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb1-16">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Cols multipliers normalizer</span></span>
<span id="cb1-17">        D2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.diagflat(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(np.dot(D1, Y), axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span>
<span id="cb1-18">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Normalize D1 Y D2</span></span>
<span id="cb1-19">        Y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.dot(np.dot(D1, Y),D2)</span>
<span id="cb1-20">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> Y</span>
<span id="cb1-21"></span>
<span id="cb1-22">X <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.random.rand(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb1-23">Y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sinkhorn(X)</span>
<span id="cb1-24"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Random input:</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span>(X))</span>
<span id="cb1-25"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sinkhorn Approx. DSM:</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{}</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span>(Y))</span>
<span id="cb1-26"></span>
<span id="cb1-27"></span>
<span id="cb1-28"></span>
<span id="cb1-29"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> generate_random_symmetric_doubly_stochastic_matrix(n, max_it <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>):</span>
<span id="cb1-30">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Generate a random matrix of size n x n</span></span>
<span id="cb1-31">    matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.random.rand(n, n)</span>
<span id="cb1-32"></span>
<span id="cb1-33">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Make the matrix symmetric</span></span>
<span id="cb1-34">    matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> matrix.T) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb1-35">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Normalize each column to sum up to 1</span></span>
<span id="cb1-36">    wc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.sqrt(np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(matrix, axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span>
<span id="cb1-37">    matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> wc</span>
<span id="cb1-38">    matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> wc[:, np.newaxis]</span>
<span id="cb1-39">    matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sinkhorn(matrix,max_it<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>max_it)</span>
<span id="cb1-40">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> matrix</span>
<span id="cb1-41">n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb1-42">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> generate_random_symmetric_doubly_stochastic_matrix(n)</span>
<span id="cb1-43"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(x<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>np.transpose(x)))</span>
<span id="cb1-44"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'row sum'</span>,np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(x,axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span>
<span id="cb1-45"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'col sum'</span>,np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(x,axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb1-46"></span>
<span id="cb1-47"></span>
<span id="cb1-48"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> get_pagerank(matrix):</span>
<span id="cb1-49">    graph <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> nx.from_numpy_array(matrix, create_using<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>nx.DiGraph)</span>
<span id="cb1-50">    pagerank <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> nx.pagerank(graph)</span>
<span id="cb1-51">    pr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>(pagerank.values()))</span>
<span id="cb1-52">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> pr</span>
<span id="cb1-53"></span>
<span id="cb1-54">pr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> get_pagerank(matrix)</span>
<span id="cb1-55"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'page rank: '</span>, pr)</span>
<span id="cb1-56"></span>
<span id="cb1-57">pr_rank <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.argsort(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>pr)</span>
<span id="cb1-58"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rank wrt pagerank'</span>, pr_rank)</span>
<span id="cb1-59">sim_rank <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.argsort(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(matrix,axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span>
<span id="cb1-60"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rank wrt pagerank'</span>, sim_rank)</span>
<span id="cb1-61"></span>
<span id="cb1-62"></span>
<span id="cb1-63">n1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pr_rank[:k]</span>
<span id="cb1-64">n2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sim_rank[:k]</span>
<span id="cb1-65"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'n1, n2'</span>,n1,n2)</span>
<span id="cb1-66"></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Random input:
[[0.55477541 0.00917532]
 [0.57030084 0.92816953]]
Sinkhorn Approx. DSM:
[[0.90583344 0.08904872]
 [0.09416656 0.91095128]]
0.0
row sum [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
col sum [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
page rank:  [0.30383867 0.29017848 0.40598286]
rank wrt pagerank [2 0 1]
rank wrt pagerank [2 0 1]
n1, n2 [2 0 1] [2 0 1]</code></pre>
</div>
</div>
<div id="cell-2" class="cell" data-execution_count="68">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1">N <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb3-2">n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb3-3">k <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>(np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">min</span>([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>n)]))</span>
<span id="cb3-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'top k is'</span>, k)</span>
<span id="cb3-5">result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.zeros((N,n))</span>
<span id="cb3-6"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(N):</span>
<span id="cb3-7">    X <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> generate_random_symmetric_doubly_stochastic_matrix(n,max_it<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>)</span>
<span id="cb3-8">    pr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> get_pagerank(X)</span>
<span id="cb3-9">    pr_rank <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.argsort(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>pr)</span>
<span id="cb3-10">    sim_rank <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.argsort(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(X,axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span>
<span id="cb3-11">    sim_rank <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sim_rank.astype(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>)</span>
<span id="cb3-12">    pr_rank <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pr_rank.astype(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>)</span>
<span id="cb3-13"></span>
<span id="cb3-14">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#print(sim_rank)</span></span>
<span id="cb3-15">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#print(pr_rank)</span></span>
<span id="cb3-16">    r <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">all</span>(pr_rank <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> sim_rank)</span>
<span id="cb3-17"></span>
<span id="cb3-18">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> k <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(n):</span>
<span id="cb3-19">        n1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pr_rank[:k]</span>
<span id="cb3-20">        n2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sim_rank[:k]</span>
<span id="cb3-21">        r2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">all</span>(n2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> n1)</span>
<span id="cb3-22">        result[i,k]<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>r2</span>
<span id="cb3-23">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#print(f'iter x: {i}, equal ranks: {r1}, {r}, top-k ranks {r2}')</span></span>
<span id="cb3-24">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#result.append(r2)</span></span>
<span id="cb3-25"></span>
<span id="cb3-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#print(100*np.sum(np.array(result))/N)</span></span>
<span id="cb3-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#print(100*np.sum(np.array(result))/N)</span></span>
<span id="cb3-28"></span>
<span id="cb3-29">mean_acc_by_k <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.mean(result, axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb3-30"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(mean_acc_by_k.shape)</span>
<span id="cb3-31">plt.plot(mean_acc_by_k,marker<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'o'</span>, linestyle<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'-'</span>)</span>
<span id="cb3-32">plt.plot(pr)</span>
<span id="cb3-33">    </span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>top k is 2
(10,)</code></pre>
</div>
<div class="cell-output cell-output-display">
<div>
<figure class="figure">
<p><img src="https://dhavala.github.io/saddlepoint/pagerank-knn/index_files/figure-html/cell-3-output-2.png" class="img-fluid figure-img"></p>
</figure>
</div>
</div>
</div>
<div id="cell-3" class="cell" data-execution_count="71">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1">pr</span>
<span id="cb5-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> generate_doubly_stochastic_matrix(n):</span>
<span id="cb5-3">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Generate a random matrix of size n x n</span></span>
<span id="cb5-4">    matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.random.rand(n, n)</span>
<span id="cb5-5">    matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(matrix, axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)[:, np.newaxis]</span>
<span id="cb5-6">    matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(matrix, axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb5-7">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> matrix</span>
<span id="cb5-8"></span>
<span id="cb5-9">new_matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> generate_doubly_stochastic_matrix(n)</span>
<span id="cb5-10"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(new_matrix)</span>
<span id="cb5-11">new_matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.ones((n, n)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> n</span>
<span id="cb5-12"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(new_matrix)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[[0.02445177 0.21162709 0.06239765 0.09071541 0.04288914 0.17370781
  0.10539216 0.01049049 0.11327745 0.14607955]
 [0.07486439 0.08766909 0.00333724 0.09027734 0.25243491 0.01962905
  0.17021015 0.23054708 0.03134022 0.07891263]
 [0.11564823 0.0628567  0.22507904 0.10508168 0.13495434 0.07762967
  0.05393516 0.03030433 0.09012407 0.12328124]
 [0.07370109 0.0830484  0.09544722 0.10601392 0.0285365  0.11602282
  0.12357837 0.21809933 0.03819294 0.13401458]
 [0.15151811 0.01225928 0.03968571 0.03440933 0.1847578  0.14357797
  0.02521022 0.07188493 0.11423148 0.17949642]
 [0.09793491 0.06454349 0.20788808 0.09529894 0.0180115  0.08205361
  0.13051871 0.09691211 0.10936077 0.10859913]
 [0.09885095 0.15953443 0.05044521 0.11993416 0.1717517  0.08436244
  0.0891729  0.18473981 0.04152612 0.03728877]
 [0.09955894 0.04410749 0.18894616 0.18085147 0.0159479  0.09800722
  0.10923791 0.07959759 0.13544544 0.05449909]
 [0.15354593 0.22776028 0.06533913 0.04602603 0.03822926 0.04376391
  0.1546066  0.07271264 0.17555689 0.00093622]
 [0.10992568 0.04659374 0.06143456 0.13139172 0.11248695 0.16124549
  0.03813783 0.00471169 0.15094462 0.13689237]]
[[0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
 [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
 [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
 [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
 [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
 [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
 [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
 [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
 [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
 [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]]</code></pre>
</div>
</div>
<div id="cell-4" class="cell" data-execution_count="73">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb7-2"></span>
<span id="cb7-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> generate_random_symmetric_doubly_stochastic_matrix(n):</span>
<span id="cb7-4">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Generate a random matrix of size n x n</span></span>
<span id="cb7-5">    matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.random.rand(n, n)</span>
<span id="cb7-6"></span>
<span id="cb7-7">    matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> matrix.T) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb7-8"></span>
<span id="cb7-9">    row_sums <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(matrix, axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb7-10">    col_sums <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(matrix, axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb7-11"></span>
<span id="cb7-12">    matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/=</span> np.maximum(row_sums, col_sums)[:, np.newaxis]</span>
<span id="cb7-13">    matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(matrix, axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb7-14"></span>
<span id="cb7-15">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> matrix</span>
<span id="cb7-16"></span>
<span id="cb7-17">n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Size of the matrix</span></span>
<span id="cb7-18">matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> generate_random_symmetric_doubly_stochastic_matrix(n)</span>
<span id="cb7-19"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(matrix)</span>
<span id="cb7-20"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(matrix,axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span>
<span id="cb7-21"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(matrix,axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb7-22"></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[[0.13497564 0.30151879 0.18229813 0.26785289 0.13428494]
 [0.2927742  0.22823658 0.14675887 0.09669389 0.21909977]
 [0.18351233 0.15214895 0.34839163 0.15068017 0.18286986]
 [0.25609461 0.09521047 0.14311237 0.30415806 0.17482364]
 [0.13264322 0.2228852  0.17943899 0.18061499 0.28892179]]
[1. 1. 1. 1. 1.]
[1.0209304  0.98356332 1.01760294 0.97339916 1.00450418]</code></pre>
</div>
</div>
<div id="cell-5" class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb9-2"></span>
<span id="cb9-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> generate_random_doubly_stochastic_matrix(n):</span>
<span id="cb9-4">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Generate a random matrix of size n x n</span></span>
<span id="cb9-5">    matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.random.rand(n, n)</span>
<span id="cb9-6"></span>
<span id="cb9-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Normalize each row to sum up to 1</span></span>
<span id="cb9-8">    matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(matrix, axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)[:, np.newaxis]</span>
<span id="cb9-9"></span>
<span id="cb9-10">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Normalize each column to sum up to 1</span></span>
<span id="cb9-11">    matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> np.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(matrix, axis<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb9-12"></span>
<span id="cb9-13">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> matrix</span>
<span id="cb9-14"></span>
<span id="cb9-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Specify the size of the matrix</span></span>
<span id="cb9-16">n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span></span>
<span id="cb9-17"></span>
<span id="cb9-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Generate a random doubly stochastic matrix</span></span>
<span id="cb9-19">matrix <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> generate_random_doubly_stochastic_matrix(n)</span>
<span id="cb9-20"></span>
<span id="cb9-21"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(matrix)</span></code></pre></div></div>
</div>



 ]]></description>
  <category>graphs</category>
  <category>ml</category>
  <category>notebook</category>
  <guid>https://dhavala.github.io/saddlepoint/pagerank-knn/</guid>
  <pubDate>Sun, 17 May 2026 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Welcome to the blog</title>
  <dc:creator>Soma S Dhavala</dc:creator>
  <link>https://dhavala.github.io/saddlepoint/welcome/</link>
  <description><![CDATA[ 
<div class="section-banner">
  <img src="https://dhavala.github.io/images/banners/horsesskybw.jpg" alt="Blog banner">
</div>




<p>This is the first post on the relaunched site. Expect notes on statistics, applied machine learning, teaching, and the occasional aside on tooling.</p>
<p>The site is built with <a href="https://quarto.org">Quarto</a>. Comments are powered by <a href="https://giscus.app">giscus</a> (GitHub Discussions).</p>



 ]]></description>
  <category>meta</category>
  <category>intro</category>
  <guid>https://dhavala.github.io/saddlepoint/welcome/</guid>
  <pubDate>Sun, 17 May 2026 00:00:00 GMT</pubDate>
</item>
</channel>
</rss>
