<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ben Denis Shaffer on Medium]]></title>
        <description><![CDATA[Stories by Ben Denis Shaffer on Medium]]></description>
        <link>https://medium.com/@bdshaff?source=rss-7438041dcdf1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*yEY7uzs9mNQR7n6C</url>
            <title>Stories by Ben Denis Shaffer on Medium</title>
            <link>https://medium.com/@bdshaff?source=rss-7438041dcdf1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 25 Nov 2026 00:44:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bdshaff/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Estimation and Visualizalization for Conversion Path Data]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/geekculture/estimation-visualizalization-for-conversion-path-data-cf5a23a04eb4?source=rss-7438041dcdf1------2"><img src="https://cdn-images-1.medium.com/max/1074/1*rO25bQe26jDNoCuJDS_g_Q.png" width="1074"></a></p><p class="medium-feed-snippet">Intro to the ConversionPath library for marketing analytics</p><p class="medium-feed-link"><a href="https://medium.com/geekculture/estimation-visualizalization-for-conversion-path-data-cf5a23a04eb4?source=rss-7438041dcdf1------2">Continue reading on Geek Culture »</a></p></div>]]></description>
            <link>https://medium.com/geekculture/estimation-visualizalization-for-conversion-path-data-cf5a23a04eb4?source=rss-7438041dcdf1------2</link>
            <guid isPermaLink="false">https://medium.com/p/cf5a23a04eb4</guid>
            <category><![CDATA[markov-chains]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[analytics]]></category>
            <category><![CDATA[didital-marketing]]></category>
            <category><![CDATA[data-visualization]]></category>
            <dc:creator><![CDATA[Ben Denis Shaffer]]></dc:creator>
            <pubDate>Tue, 27 Jul 2021 03:51:49 GMT</pubDate>
            <atom:updated>2021-07-27T14:53:08.693Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Into to Markov Chain Multi-Touch Attribution]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/data-science/into-to-markov-chain-multi-touch-attribution-bb1968ff1f54?source=rss-7438041dcdf1------2"><img src="https://cdn-images-1.medium.com/max/1076/1*M-mZ8EtfkwTukN8fo29Mxg.png" width="1076"></a></p><p class="medium-feed-snippet">Using R for Simulation and Modeling</p><p class="medium-feed-link"><a href="https://medium.com/data-science/into-to-markov-chain-multi-touch-attribution-bb1968ff1f54?source=rss-7438041dcdf1------2">Continue reading on TDS Archive »</a></p></div>]]></description>
            <link>https://medium.com/data-science/into-to-markov-chain-multi-touch-attribution-bb1968ff1f54?source=rss-7438041dcdf1------2</link>
            <guid isPermaLink="false">https://medium.com/p/bb1968ff1f54</guid>
            <category><![CDATA[digital-marketing]]></category>
            <category><![CDATA[marketing]]></category>
            <category><![CDATA[advertising]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[analytics]]></category>
            <dc:creator><![CDATA[Ben Denis Shaffer]]></dc:creator>
            <pubDate>Sun, 04 Apr 2021 06:41:14 GMT</pubDate>
            <atom:updated>2021-04-05T03:47:33.411Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Eigenvalues and Eigenvectors]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/data-science/eigenvalues-and-eigenvectors-89483fb56d56?source=rss-7438041dcdf1------2"><img src="https://cdn-images-1.medium.com/max/1248/1*5_XBEdjLR5vQhm657DgRrg.gif" width="1248"></a></p><p class="medium-feed-snippet">Computing and Visualizing</p><p class="medium-feed-link"><a href="https://medium.com/data-science/eigenvalues-and-eigenvectors-89483fb56d56?source=rss-7438041dcdf1------2">Continue reading on TDS Archive »</a></p></div>]]></description>
            <link>https://medium.com/data-science/eigenvalues-and-eigenvectors-89483fb56d56?source=rss-7438041dcdf1------2</link>
            <guid isPermaLink="false">https://medium.com/p/89483fb56d56</guid>
            <category><![CDATA[r]]></category>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[mathematics]]></category>
            <category><![CDATA[linear-algebra]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Ben Denis Shaffer]]></dc:creator>
            <pubDate>Fri, 03 Jul 2020 20:28:03 GMT</pubDate>
            <atom:updated>2020-07-03T21:17:28.300Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[QR Matrix Factorization]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/data-science/qr-matrix-factorization-15bae43a6b2?source=rss-7438041dcdf1------2"><img src="https://cdn-images-1.medium.com/max/1589/1*Pnfy8X-3SzIal9RbGcFNSw.jpeg" width="1589"></a></p><p class="medium-feed-snippet">Least Squares and Computation (with R and C++)</p><p class="medium-feed-link"><a href="https://medium.com/data-science/qr-matrix-factorization-15bae43a6b2?source=rss-7438041dcdf1------2">Continue reading on TDS Archive »</a></p></div>]]></description>
            <link>https://medium.com/data-science/qr-matrix-factorization-15bae43a6b2?source=rss-7438041dcdf1------2</link>
            <guid isPermaLink="false">https://medium.com/p/15bae43a6b2</guid>
            <category><![CDATA[linear-algebra]]></category>
            <category><![CDATA[r-programming]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[mathematics]]></category>
            <category><![CDATA[linear-regression]]></category>
            <dc:creator><![CDATA[Ben Denis Shaffer]]></dc:creator>
            <pubDate>Thu, 27 Feb 2020 14:49:44 GMT</pubDate>
            <atom:updated>2020-02-28T05:50:16.920Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[An Entry Point to Data Science: Vector Projections (with R and C++)]]></title>
            <link>https://medium.com/analytics-vidhya/an-entry-point-to-data-science-vector-projections-with-r-and-c-34ff2e5d608c?source=rss-7438041dcdf1------2</link>
            <guid isPermaLink="false">https://medium.com/p/34ff2e5d608c</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[r-programming]]></category>
            <category><![CDATA[linear-algebra]]></category>
            <dc:creator><![CDATA[Ben Denis Shaffer]]></dc:creator>
            <pubDate>Wed, 12 Feb 2020 15:07:08 GMT</pubDate>
            <atom:updated>2024-01-30T03:51:32.361Z</atom:updated>
            <content:encoded><![CDATA[<h3>An Entry Point to Data Science</h3><h4>Vector Projections (with R and C++)</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_1w8jQWeIOsYhFDSIAG3gQ.jpeg" /></figure><h3>Entry Point</h3><p>The idea of <a href="https://en.wikipedia.org/wiki/Vector_projection">vector projections</a> is nothing new or advanced. It is something you learn about in your Linear Algebra course or even in high school. In and of itself there is not much sexiness in vector projections. Still, this topic is a fantastic entryway to<a href="https://www.analyticsvidhya.com/blog/2023/05/what-is-data-science-a-complete-guide/"> Data Science</a> for three reasons:</p><h4>1. Intuitive</h4><p>It is intuitive because once you built up the abstract image in your mind you can always return to it. In a way, it’s a perfect example of mathematics being an abstraction for the real world.</p><h4>2. Immediately useful in many applications</h4><p>Applications that quickly follow include things like</p><ul><li>The <a href="https://en.wikipedia.org/wiki/QR_decomposition">QR</a>, the <a href="https://en.wikipedia.org/wiki/Eigendecomposition_of_a_matrix">Eigenvalue</a>, and the <a href="https://en.wikipedia.org/wiki/Singular_value_decomposition">SVD</a> matrix decomposition.</li><li><a href="https://en.wikipedia.org/wiki/Least_squares">Least Squares</a>, Linear Regression, Multivariate Linear Regression.</li><li>Dimensionality Reduction, <a href="https://en.wikipedia.org/wiki/Principal_component_analysis">PCA</a>, <a href="https://en.wikipedia.org/wiki/Multidimensional_scaling">MDS</a>.</li></ul><p>These are some of the topics that I plan on exploring in future posts.</p><h4>3. A Nice Programming Exercise</h4><p>If you are an experienced R user and you write functions that are called by other R functions hundreds and thousands of times then perhaps you have thought about learning how to extend/optimize your R code with C++ using Rcpp. This is what I have personally been doing and I found that implementing simple functions in C++ is a really great exercise.</p><h3>Agenda</h3><ol><li>Review Vector Projections (A bit of math and intuition).</li><li>Implement in R and check the math.</li><li>Implement in C++ and benchmark against R.</li><li>Next Steps</li></ol><h3>Concept</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*HQ8r-HGfZM47R30j" /><figcaption>Photo by <a href="https://unsplash.com/@martino_pietropoli?utm_source=medium&amp;utm_medium=referral">Martino Pietropoli</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Imagine standing in the street with the sun above you. Look at the ground and you will see your shadow is cast onto the surface. In this way, the 3D shape of your body is projected onto a 2D surface of the ground. This is the idea behind projections. Your shadow is your body but it is ON the ground. In other words, it is in the <em>direction</em> of the ground. In the same way, imagine the shadow of your body on the building next to you. Still a 2D projection of you, but now in a different <em>direction</em>. Now take two abstract vectors a and u and imagine casting a shadow of a onto u. What you get is a vector that is in the direction of u. That vector is exactly what’s called a projection of a onto u.</p><h3>Formulas</h3><p>There are two ways to compute projections. 1st is to find the correct scalar to multiply u by. 2nd is to find the correct Matrix to multiply a by. This matrix is called the projection matrix and it is quite special. This projection matrix can be computed when you project a onto a single vector or onto a whole plane/hyperplane.</p><h4>Multiplying by a scalar:</h4><p>Projection of a onto u is given by the following expression.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/570/1*ardmk5jeiEijJI5vP3fJNw.png" /></figure><h4>Multiplying by the projection matrix:</h4><p>Alternatively, we can find a projection matrix P for u and find the projection as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/424/1*Th4zUGWmpv6Pump08BoAXw.png" /></figure><p>where</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/372/1*-Zqz4-qz7L6udap9uxW9EA.png" /></figure><p>and U can be a matrix with just one column.</p><h3>Computing with R</h3><p>Let’s first see an example of how a projection can be computed in R and check that both methods produce the same result. Let’s generate two little random vectors of length 4 from the standard normal distribution.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JD-5L8wOkPz41yuDTHWz3w.png" /><figcaption><a href="https://bdshaff.github.io/blog/2020-01-28-vector-projections/">code available here</a></figcaption></figure><p>Using the 1st method we simply compute a scalar and apply it to the vector that we are projecting onto. So here is our projection of a onto u.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ygz1HMhAyD7pmWenKhAlnQ.png" /><figcaption><a href="https://bdshaff.github.io/blog/2020-01-28-vector-projections/">code available here</a></figcaption></figure><p>Using the 2nd method we first compute the projection matrix Pmat and then apply it to a to get the projection. To do this we can just define a matrix with 1 column and apply the above formula. This is what the matrix looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5Q7VgN-woixCbv5splbnAQ.png" /><figcaption><a href="https://bdshaff.github.io/blog/2020-01-28-vector-projections/">code available here</a></figcaption></figure><p>And here is that same projection vector we had previously:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xsa24A6YYF-J5omV-2cIuw.png" /><figcaption><a href="https://bdshaff.github.io/blog/2020-01-28-vector-projections/">code available here</a></figcaption></figure><h3>Properties</h3><p>We computed the projection of a onto u but how do we check that this is, in fact, a projection, and what makes it special? To see this we can look at a couple of important properties and check them.</p><p>say that</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/252/1*I0Br5XRg3uhwgNVmCUenIg.png" /></figure><p>and</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/228/1*YTH0CiJtB5Oi1hKxmdF7vA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_T554Z1jCfr8p3J3TtWu6g.png" /><figcaption><a href="https://bdshaff.github.io/blog/2020-01-28-vector-projections/">code available here</a></figcaption></figure><h4>1. Direction</h4><p>The first thing to check is that the projection that we computed is in the same direction as u the vector that we projected a onto.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/192/1*N_nRFsFuWHpC4ZDYpVDf5g.png" /></figure><p>where c is some constant vector.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JRh0ofzRZ0W4sb7bb0_NWg.png" /><figcaption><a href="https://bdshaff.github.io/blog/2020-01-28-vector-projections/">code available here</a></figcaption></figure><p>As you can see this checks out.</p><h4>2. Orthogonality</h4><p>If we project a onto u then the vector e is supposed to be at 90 degrees to u and the projection. We can check this by computing the dot product of e and u or v and check that it is 0.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/226/1*WGwxLIyYGdEjtaTHucuDww.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GSfLuqDHGVYx24JLon0BRw.png" /><figcaption><a href="https://bdshaff.github.io/blog/2020-01-28-vector-projections/">code available here</a></figcaption></figure><p>This checks out as well.</p><h4>3. `P` is idempotent</h4><p>This is a cool property that sais that if you project the projection v onto uthen you will get back the same projection vector v. Mathematically</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/180/1*J2LmOv7Rt9vkzeflamBkcg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Rmzv1duKRyX50BsRtmHzsw.png" /><figcaption><a href="https://bdshaff.github.io/blog/2020-01-28-vector-projections/">code available here</a></figcaption></figure><p>We see that this holds true, and implies that Pa=PPa=Pv=v.</p><h3>Bit of Programming</h3><p>In practice, R is fantastic for absolutely everything data analysis related. In certain scenarios though it can be made even more powerful when extending with C++ through Rcpp. As we will see in the future a projection function is a perfect case for this. Here I implement the same function in R and in C++ and compare them on vectors of various lengths.</p><h4>the R function</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ka6PjWl1tNRYFmxwLOMlPg.png" /><figcaption><a href="https://bdshaff.github.io/blog/2020-01-28-vector-projections/">code available here</a></figcaption></figure><h4>the C++ function</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P3BN3nr8nNMFDwedpT0j7A.png" /><figcaption><a href="https://bdshaff.github.io/blog/2020-01-28-vector-projections/">code available here</a></figcaption></figure><p>As you can see the R function is much more compact and takes 30 seconds to write. C++ is longer but not too bad. In the end, they produce the exact same results.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1oto9cXMVwMnNQ51qTVMPw.png" /><figcaption><a href="https://bdshaff.github.io/blog/2020-01-28-vector-projections/">code available here</a></figcaption></figure><h3>Benchmarking</h3><p>I’ll make three comparisons. 1st will look at a benchmark for the original little vectors of length 4 that we looked at. 2nd will look at the same benchmark but for vectors of length 2000. Lastly, we will look at how the two functions scale for vector lengths starting with 4 all the way to 1,000,000.</p><h4>Little vectors</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1oto9cXMVwMnNQ51qTVMPw.png" /><figcaption><a href="https://bdshaff.github.io/blog/2020-01-28-vector-projections/">code available here</a></figcaption></figure><p>Looks like the C++ version is not worth using for this case given that we are calling it in R and there can be some overhead when we use Rcpp. That being said we are looking at nanoseconds here so there is practically no difference.</p><h4>Bigger vectors</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*03qhlXGq_D5kdqEcmp707w.png" /><figcaption><a href="https://bdshaff.github.io/blog/2020-01-28-vector-projections/">code available here</a></figcaption></figure><p>Here we clearly see that on average C++ is about 3X faster. Still, we are looking at microseconds so if we have to do this just once R is totally adequate.</p><h4>Scaling to larger vectors</h4><p>To see how well these two functions scale as the vector size grows we can write a function that will take two vectors and return the time it took to compute the projections using both versions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2R2hzegNUnQCkSupGZn05Q.png" /><figcaption><a href="https://bdshaff.github.io/blog/2020-01-28-vector-projections/">code available here</a></figcaption></figure><p>Now generating a sequence of vector lengths and pairs of vectors themselves we can apply the R and C++ functions and plot their execution times again the vector length.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MGV__KXDFjbR4sjh4Nx1Zg.png" /><figcaption><a href="https://bdshaff.github.io/blog/2020-01-28-vector-projections/">code available here</a></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XhdMppOvVH1kLbUbgGBGCA.png" /></figure><p>We can see from the plot that the difference in performance really grows as the vector length grows. This means that our C++ function will become more and more efficient in comparison to the R function.</p><p>You can imagine that if we need to compute projections for large vectors sequentially the time difference can become significant.</p><h3>Next Steps</h3><p><a href="https://en.wikipedia.org/wiki/Gram%E2%80%93Schmidt_process">Gram–Schmidt process</a> — a method for finding an orthogonal set of vectors that span the same space as the original set of vectors.</p><p><a href="https://en.wikipedia.org/wiki/QR_decomposition">QR matrix decomposition</a> — a representation of a matrix as a product of an orthogonal matrix Q and an upper triangular matrix R. This decomposition can be used to find the <a href="https://en.wikipedia.org/wiki/Least_squares">Least Squares</a> solution which is obviously important for Regression Analysis.</p><h3>Learning Resources</h3><p><a href="https://ocw.mit.edu/courses/mathematics/18-06sc-linear-algebra-fall-2011/least-squares-determinants-and-eigenvalues/">MIT Opencourswear Linea Algebra Course</a> — Unit 2 of this course covers this topic in much more detail and with a lot more math. The Video lectures are fantastic so if you want a bit more math this is a good place to go.</p><p><a href="https://adv-r.hadley.nz/rcpp.html">R and C++</a> — <a href="https://adv-r.hadley.nz/rcpp.html">Ch 25 of the Advanced R book</a> is a perfect introduction to Rcpp and a great resource if you want to learn how to extend R with C++. Another resource that I can recommend is a DataCamp course <a href="https://www.datacamp.com/courses/optimizing-r-code-with-rcpp">Optimizing R Code with Rcpp</a> which is my favorite way to learn more R.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=34ff2e5d608c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/an-entry-point-to-data-science-vector-projections-with-r-and-c-34ff2e5d608c">An Entry Point to Data Science: Vector Projections (with R and C++)</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>