<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>God, Your Book Is Great !!</title>
	<atom:link href="https://saravananthirumuruganathan.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>https://saravananthirumuruganathan.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Sun, 02 Dec 2012 13:43:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<site xmlns="com-wordpress:feed-additions:1">11164265</site><cloud domain='saravananthirumuruganathan.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>https://s0.wp.com/i/buttonw-com.png</url>
		<title>God, Your Book Is Great !!</title>
		<link>https://saravananthirumuruganathan.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="https://saravananthirumuruganathan.wordpress.com/osd.xml" title="God, Your Book Is Great !!" />
	<atom:link rel='hub' href='https://saravananthirumuruganathan.wordpress.com/?pushpress=hub'/>
	<item>
		<title>Fixing Matlab errors in Ubuntu 12.10</title>
		<link>https://saravananthirumuruganathan.wordpress.com/2012/12/02/fixing-matlab-errors-in-ubuntu-12-10/</link>
					<comments>https://saravananthirumuruganathan.wordpress.com/2012/12/02/fixing-matlab-errors-in-ubuntu-12-10/#comments</comments>
		
		<dc:creator><![CDATA[Saravanan Thirumuruganathan]]></dc:creator>
		<pubDate>Sun, 02 Dec 2012 13:43:00 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Techie]]></category>
		<category><![CDATA[matlab]]></category>
		<category><![CDATA[ubuntu]]></category>
		<guid isPermaLink="false">http://saravananthirumuruganathan.wordpress.com/?p=641</guid>

					<description><![CDATA[I recently installed Ubuntu 12.10 in my system and found that the upgrade a lot of my system. A reasonable number of fixes were simple but some of them were very annoying. In this post I just wanted to discuss two specific errors I faced and how to fix them. I use the student version [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>I recently installed Ubuntu 12.10 in my system and found that the upgrade a lot of my system. A reasonable number of fixes were simple but some of them were very annoying. In this post I just wanted to discuss two specific errors I faced and how to fix them.</p>
<p>I use the student version of Matlab (32 bit) on a 64 bit machine. As part of upgrade to Ubuntu 12.10 , the installer removed multiple 32 bit files which caused the issue. When I tried to run Matlab , I got the following errors :</p>
<blockquote>
<p>~/matlab/bin/matlab: 1: ~/matlab/bin/util/oscheck.sh: /lib/libc.so.6: not found</p>
<p>~/matlab/bin/glnx86/MATLAB: error while loading shared libraries: libXpm.so.4: cannot open shared object file: No such file or directory</p>
</blockquote>
<p>The first error is mostly harmless – I will describe how to fix it later in the blog post. Fixing the first error needed to install 386 (32bit) version of few packages. Of course, fixing one exposed the next error and so on. In the interest of time, I will put all the packages in the single command.</p>
<blockquote>
<p><font color="#333333">sudo apt-get install libxpm4:i386 libxmu6:i386 libxp6:i386</font></p>
</blockquote>
<p>Running the above command worked and allowed Matlab to run. However, I then faced another issue – when I tried to save a plot, it was failing again with the following error : (fixing first caused the second)</p>
<blockquote>
<p>MATLAB:dispatcher:loadLibrary Can&#8217;t load &#8216;~/matlab/bin/glnx86/libmwdastudio.so&#8217;: libXfixes.so.3: cannot open shared object file: No such file or directory.     <br />??? Error while evaluating uipushtool ClickedCallback</p>
<p>MATLAB:dispatcher:loadLibrary Can&#8217;t load &#8216;~/matlab/bin/glnx86/libmwdastudio.so&#8217;: libGLU.so.1: cannot open shared object file: No such file or directory.     <br />??? Error while evaluating uipushtool ClickedCallback      </p>
<p>&#160;</p>
</blockquote>
<p>To fix this, run the following command :</p>
<blockquote>
<p><font color="#333333">sudo apt-get install libxfixes3:i386 libglu1-mesa:i386</font></p>
</blockquote>
<p>Finally, to fix the innocuous error :</p>
<blockquote>
<p><font color="#333333">~matlab/bin/matlab: 1: /home/neo/gLingua/matlab/bin/util/oscheck.sh: /lib/libc.so.6: not found</font></p>
</blockquote>
<p>do the following :</p>
<blockquote>
<p>sudo ln -s /lib/x86_64-linux-gnu/libc-2.15.so /lib/libc.so.6</p>
</blockquote>
<p>Of course, make sure the libc-2.xx.so version is the correct one before running this command. </p>
<p>&#160;</p>
<p>Hope this post helped !</p>
]]></content:encoded>
					
					<wfw:commentRss>https://saravananthirumuruganathan.wordpress.com/2012/12/02/fixing-matlab-errors-in-ubuntu-12-10/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">641</post-id>
		<media:content url="https://2.gravatar.com/avatar/e2957c9d69690d3fef33fa2bb570d139abce0df84a663fcc30c2ffc3a77b2583?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">saravananthirumuruganathan</media:title>
		</media:content>
	</item>
		<item>
		<title>How to get shareable link for an entire folder within Public folder on Dropbox</title>
		<link>https://saravananthirumuruganathan.wordpress.com/2012/02/13/how-to-get-shareable-link-for-an-entire-folder-within-public-folder-on-dropbox/</link>
					<comments>https://saravananthirumuruganathan.wordpress.com/2012/02/13/how-to-get-shareable-link-for-an-entire-folder-within-public-folder-on-dropbox/#comments</comments>
		
		<dc:creator><![CDATA[Saravanan Thirumuruganathan]]></dc:creator>
		<pubDate>Tue, 14 Feb 2012 05:32:21 +0000</pubDate>
				<category><![CDATA[Techie]]></category>
		<category><![CDATA[dropbox]]></category>
		<category><![CDATA[sharing]]></category>
		<guid isPermaLink="false">http://saravananthirumuruganathan.wordpress.com/?p=638</guid>

					<description><![CDATA[As part of my research, I collaborate and share datasets with fellow researchers. These datasets are usually huge and hence cannot be shared via email. Since I started using Dropbox, I felt it to be a convenient mechanism t share them. The workflow I came up with was very simple. Create a folder inside my [&#8230;]]]></description>
										<content:encoded><![CDATA[<p align="justify">As part of my research, I collaborate and share datasets with fellow researchers. These datasets are usually huge and hence cannot be shared via email. Since I started using Dropbox, I felt it to be a convenient mechanism t share them. </p>
<p align="justify">The workflow I came up with was very simple. Create a folder inside my Dropbox public folder, put the dataset in it and share the link with my friends. Sounds simple enough. Although in practice I hit a snag. </p>
<p align="justify">I was surprised to find that I was not able to get a shareable link for folders within Public folder. For any other folder in my Dropbox, I was able to right click and get&#160; a shareable link. Long story short, the way to enable it is very simple. You just need to visit this site :</p>
<blockquote>
<p align="justify"><a title="https://www.dropbox.com/enable_shmodel" href="https://www.dropbox.com/enable_shmodel">https://www.dropbox.com/enable_shmodel</a></p>
</blockquote>
<p align="justify">&#160;</p>
<p align="justify">Now you can right click on any folder (either using the client or in Web UI) to get the shareable link. An interesting side effect was that I was able to get the shareable link for any file/folder in my Dropbox folder. That’s right – I was able to share a folder that was not in my Public folder. This is a bit surprising but could be useful someday nevertheless. </p>
]]></content:encoded>
					
					<wfw:commentRss>https://saravananthirumuruganathan.wordpress.com/2012/02/13/how-to-get-shareable-link-for-an-entire-folder-within-public-folder-on-dropbox/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">638</post-id>
		<media:content url="https://2.gravatar.com/avatar/e2957c9d69690d3fef33fa2bb570d139abce0df84a663fcc30c2ffc3a77b2583?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">saravananthirumuruganathan</media:title>
		</media:content>
	</item>
		<item>
		<title>Fixing &#8216;Updating player&#8230;&#8217; issue in Amazon Instant Video on Linux</title>
		<link>https://saravananthirumuruganathan.wordpress.com/2012/02/13/fixing-updating-player-issue-in-amazon-instant-video-on-linux/</link>
					<comments>https://saravananthirumuruganathan.wordpress.com/2012/02/13/fixing-updating-player-issue-in-amazon-instant-video-on-linux/#comments</comments>
		
		<dc:creator><![CDATA[Saravanan Thirumuruganathan]]></dc:creator>
		<pubDate>Tue, 14 Feb 2012 05:17:02 +0000</pubDate>
				<category><![CDATA[Techie]]></category>
		<category><![CDATA[amazon]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[instant]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[video]]></category>
		<guid isPermaLink="false">http://saravananthirumuruganathan.wordpress.com/?p=636</guid>

					<description><![CDATA[I have been a happy Amazon Prime customer for the last couple of years. One of the biggest perks of using it the availability of large number of videos available for instantly watching. Infact, I watched almost all episodes of Star Trek (TOS to Voyager) using this method. Sometime in the second or third week [&#8230;]]]></description>
										<content:encoded><![CDATA[<p align="justify">I have been a happy Amazon Prime customer for the last couple of years. One of the biggest perks of using it the availability of large number of videos available for instantly watching. Infact, I watched almost all episodes of Star Trek (TOS to Voyager) using this method. </p>
<p align="justify">Sometime in the second or third week of January, this method broke down. Whenever, I tried to play the episodes of Voyager, I got an error in Flash player. Basically, it will open a dialog box saying <strong>‘Updating Player’ </strong>which will soon error out saying &quot;<strong>an error occurred and your player could not be updated</strong>”. If you retry, it will get stuck with <strong>&#8216;Updating Player’ </strong>. </p>
<p align="justify">I was using Ubuntu 11.10 on a 64 bit machine. I tried lot of things and nothing really worked. I installed and reinstalled Adobe Flash plugin and other codecs and basically made a mess of my system. Finally, I found a simple solution in Amazon Instant Video forum in an unrelated thread. The link is <strong><a href="http://www.amazon.com/forum/amazon%20video%20on%20demand/ref=cm_cd_et_md_pl?_encoding=UTF8&amp;cdForum=Fx3EQAX98ED5WQ3&amp;cdMsgNo=37&amp;cdPage=2&amp;cdSort=oldest&amp;cdThread=TxFTGOK5LRL3JM&amp;cdMsgID=MxV5K1A6CV81VT#MxV5K1A6CV81VT">here</a></strong> . The solution is very simple . Install <strong>hal</strong> and <strong>libhal1 </strong>package for your distro. If you are using Ubuntu, the command is</p>
<blockquote>
<p>sudo apt-get install libhal1 hal</p>
</blockquote>
<p>&#160;</p>
<p>Few of my friends also had this issue and installing these packages seems to fix the issue. Unfortunately, this useful tip seems buried under other&#160; noise and hence I decided to put a separate blog post. If this did not fix the issue I recommend looking at <a href="http://kb2.adobe.com/cps/929/cpsid_92948.html"><strong>Adobe’s Problems playing protected video content on 64-bit Ubuntu Linux</strong></a><strong>&#160;</strong>page. This has some additional information on making flash work.    </p>
]]></content:encoded>
					
					<wfw:commentRss>https://saravananthirumuruganathan.wordpress.com/2012/02/13/fixing-updating-player-issue-in-amazon-instant-video-on-linux/feed/</wfw:commentRss>
			<slash:comments>44</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">636</post-id>
		<media:content url="https://2.gravatar.com/avatar/e2957c9d69690d3fef33fa2bb570d139abce0df84a663fcc30c2ffc3a77b2583?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">saravananthirumuruganathan</media:title>
		</media:content>
	</item>
		<item>
		<title>Detecting Mixtures of Genres in Movie Dialogues</title>
		<link>https://saravananthirumuruganathan.wordpress.com/2012/01/10/detecting-mixtures-of-genres-in-movie-dialogues/</link>
					<comments>https://saravananthirumuruganathan.wordpress.com/2012/01/10/detecting-mixtures-of-genres-in-movie-dialogues/#comments</comments>
		
		<dc:creator><![CDATA[Saravanan Thirumuruganathan]]></dc:creator>
		<pubDate>Tue, 10 Jan 2012 06:23:07 +0000</pubDate>
				<category><![CDATA[Uncategorized]]></category>
		<guid isPermaLink="false">http://saravananthirumuruganathan.wordpress.com/?p=633</guid>

					<description><![CDATA[Author Note : My blog is getting the first guest post ! This post is written by dear friend and fellow geek Kripa who just graduated from Columbia University. He specializes in Machine Learning, NLP and Data Mining. Over the last few months,&#160; he has been doing some amazing exploratory work in Data Mining. This [&#8230;]]]></description>
										<content:encoded><![CDATA[<p align="justify"><em>Author Note : My blog is getting the first guest post ! This post is written by dear friend and fellow geek <a href="http://www.linkedin.com/pub/kripa-k-s/11/1b/675"><strong>Kripa</strong></a> who just graduated from Columbia University. He specializes in Machine Learning, NLP and Data Mining. Over the last few months,&#160; he has been doing some amazing exploratory work in Data Mining. This blog post describes one such work. Here he describes how he applied LDA on movie scripts to determine the mixture of genres. Aside from being a cool application of LDA, finding such genre mixture has multiple applications. For eg, this can provide a mechanism to improve movie recommendations. You can even use it to <a href="http://en.wikipedia.org/wiki/Deconstruction">deconstruct</a> the work of your favorite artist !</em></p>
<p align="justify">Every movie predominantly falls into a particular genre that can be one of war, crime, adventure, thriller, comedy, romance, etc. Sometimes there is more than one genre a movie can belong to. For example a movie like <a href="http://www.imdb.com/title/tt0181689/"><i>Minority Report</i> </a>though predominantly belongs to sci-fi has very strong elements of crime in the storyline. Given a movie, I wanted to look at some approximate percentages of genres contained in it.     </p>
<p>If I throw a movie like <a href="http://www.imdb.com/title/tt0213149/"><i>Pearl Harbor</i> </a>at my system, I expect it to return an output like: </p>
<blockquote>
<p align="justify"><strong>war: 35%        <br /></strong>crime: 13%       <br />comedy: 13%       <br />horror: 9%       <br /><strong>romance: 19%        <br /></strong>sci-fi: 11%<strong>        <br /></strong></p>
</blockquote>
<p align="justify">I decided to consider only these six genres as others like Adventure, Action, Thriller, etc are mostly overlaps. For example, action is a broad category &#8211; war and crime could also be considered action; and a thriller could be a medley of action and crime. For my experiment I considered only those genres which I felt were distinct from each other. </p>
<h3 align="justify">Data</h3>
<p align="justify">The first question is what kind of data to go after? Movie plots are available from the IMDB dataset. They do express the major genre well but they don&#8217;t contain a lot of information to get the minor genres out. Wikipedia usually contains a detailed plot, but I found something better: <a href="http://www.imsdb.com/">Internet Movie Script Database</a> (imsdb here forth). This is an online repository of movie scripts which contain dialogues and also contextual scene information. </p>
<p align="justify">For example consider this scene from <a href="http://www.imsdb.com/Movie%20Scripts/Patriot,%20The%20Script.html">The Patriot</a>:     </p>
<p><i>Marion leaves the workshop with Susan at his side. Nathan and Samuel walk past, exhausted from their day in the field.      <br /><b>NATHAN:</b> Father, I saw a post rider at the house.       <br /><b>MARION:</b> Thank you. Did you finish the upper field?       <br /><b>SAMUEL:</b> We got it all cut and we bundled half of it.       <br /><b>MARION:</b> Those swimming breaks cut into the day, don&#8217;t they? </i>    </p>
<p>This contains both the dialogues and the description of the scene. It struck me as the right kind of data I should be looking at. Also this had a good amount of scripts (~1000 movies) for running a quick experiment. I did a quick and dirty <i>wget</i> of all the webpages followed by a bunch of shell scripts to weed out html tags and expressions such as &quot;CAMERA PANS OUT&quot; and &quot;SCENE SHIFTS TO&quot; that add no information to the script. I also removed the stop words and proper nouns. After pre-processing I had just the text of dialogues and the scene description. </p>
<p align="justify">Now that I have data, how did I go about finding the composition of genres in a movie? </p>
<h3 align="justify">Supervised vs Unsupervised</h3>
<p align="justify">Let us define the problem elaborately. I have the pre-processed imsdb content which is the <i>data</i> and its genre is its <i>label</i>. A simple <i>classification</i> task will involve extracting features from the labeled data and <i>training</i> it using a classifier to build a <i>model</i>. This model can then be used to predict the genre of a movie that is out of the training data. This is a supervised approach where we already have the labeled data &#8211; genre of a movie. For my task of detecting a mixture of genres, all I would need to do is to tweak the classifier output so that it gives the individual probabilities of each genre a movie can possibly belong to. But is this the right approach? Will the output probabilities be close to the actual representation of genres in a movie? </p>
<p align="justify">There are couple of issues with this supervised approach. </p>
<li>
<div align="justify">For training using a classifier, I need to assign a label to a script. For this problem I should be assigning more than one label to an instance. This is not possible. If during traning, I were to assign a single label (genre) to a whole movie, I&#8217;m making a strong (and wrong!) assumption that each movie script represents one genre. </div>
</li>
<li>
<div align="justify">Consider an example like <a href="http://www.imdb.com/title/tt0181689/"><i>Minority Report</i> </a>. Here imdb does not list &#8216;Crime&#8217; as a category at all! We know very well that minority report is a <i>&#8216;Sci-Fi Crime&#8217; </i>movie. The validity of labels itself poses an issue here. </div>
<p align="justify">My expectation from an <b>unsupervised approach</b> is simple. I have a huge collection of <i>unlabeled</i> scripts. <b>I&#8217;m assuming that each script contains a mixture of genres. </b></p>
<p align="justify">I now need to extract features (words) that are relevant to each genre from this corpus. In better words, given a corpus of scripts, the unsupervised system needs to <i>discover</i> from the corpus a bunch of topics and list the most relevant features (words) that belong to each topic. I would then be able to use the features to infer the topic mixture of each individual script. This is known as <a href="http://en.wikipedia.org/wiki/Topic_model"><i><strong>Topic Modelling</strong> </i></a>. Intuitively I&#8217;m trying to <i>generate</i> a bag of words model out of a corpus of scripts. </p>
<h3 align="justify">Latent Dirichlet Allocation</h3>
<p align="justify"><a href="http://en.wikipedia.org/wiki/Latent_Dirichlet_allocation"><strong>LDA</strong></a> is a topic model and one which is most widely used today. I will not spend much effort in giving a rigorous treatment on LDA since there are excellent resources available online. I&#8217;m listing those that I found extremely useful for understanding LDA. </p>
<ol>
<li>
<div align="justify">Prof. David Blei gave a talk on LDA and its applications at NYU during the NYC Machine Learning Week sometime in February 2011. There is a nice video recording of it: <a href="http://blip.tv/file/4446448">Part 1 </a>and <a href="http://blip.tv/file/4446525">Part 2 </a></div>
</li>
<li>
<div align="justify">You can find Prof.Blei&#8217;s implementation of LDA in C on his <a href="http://www.cs.princeton.edu/~blei/topicmodeling.html">webpage</a>. </div>
</li>
<li>
<div align="justify">Edwin Chen&#8217;s blog <a href="http://blog.echen.me/2011/08/22/introduction-to-latent-dirichlet-allocation/">article</a> is one of the best sources for grasping the intuition behind LDA even if you are mathematically less inclined. </div>
</li>
</ol>
<p align="justify"><b>A word of warning here!</b> I will write a brief note about my understanding of LDA. Honestly, there are better ways of explaining LDA. At this point all you need know is that LDA is a statistical black box &#8211; throw a bunch of documents at it; specify &#8216;k&#8217; &#8211; the number of topics you think are represented by the documents and LDA will output &#8216;k&#8217; topic vectors. Each topic vector contains words arranged in decreasing probabilities of being associated with that particular topic. You need to know ONLY THIS! So feel free to skip to the next section.       </p>
<p>In the case of <a href="http://en.wikipedia.org/wiki/Maximum_likelihood">Maximum Likelihood</a> given <i>n</i> data points, we assume the underlying distribution that generated this data is a <i>Gaussian</i> and fit the data to the best <a href="http://en.wikipedia.org/wiki/Gaussian_function">Gaussian</a> possible. LDA also makes a similar assumption that there is a hidden structure to the data. And that hidden structure is a multinomial whose parameter <img src="https://s0.wp.com/latex.php?latex=%5Ctheta&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Ctheta&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Ctheta&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;theta" class="latex" /> comes from a <a href="http://en.wikipedia.org/wiki/Dirichlet_distribution">Dirichlet</a> Prior. </p>
<p align="justify">Let us say that I want to generate a random document; I don&#8217;t care if its meaningful or not. I first fix the number of words I would want to generate in that document. I can on the other hand draw a random number from say a <i>Poisson</i>. Once I have the number of words (N) to be generated, I go ahead to generate those many words from the corpus. </p>
<p align="justify">Each word is generated thus: draw a <img src="https://s0.wp.com/latex.php?latex=%5Ctheta&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Ctheta&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Ctheta&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;theta" class="latex" /> from a <a href="http://en.wikipedia.org/wiki/Dirichlet_distribution">Dirichlet</a>. (Dirichlet is a distribution over the simplex.) Consider <img src="https://s0.wp.com/latex.php?latex=%5Calpha+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Calpha+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Calpha+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;alpha " class="latex" /> as the parameter that decides the shape of the Dirichlet similar to how mean and variance decide the shape of the Gaussian bell curve. In a 3-D space, for some choice of <img src="https://s0.wp.com/latex.php?latex=%5Calpha&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Calpha&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Calpha&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;alpha" class="latex" />, consider the probability to be more near (1,0,0), (0,1,0) and (0,0,1). For some other choice of <img src="https://s0.wp.com/latex.php?latex=%5Calpha+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Calpha+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Calpha+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;alpha " class="latex" /> all points in the 3-D simplex might get the same probability! This represents what kind of topic mixtures I can generally expect! (If my initial guess is that each document has only one topic, mostly I will choose an <img src="https://s0.wp.com/latex.php?latex=%5Calpha&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Calpha&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Calpha&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;alpha" class="latex" /> such that I get more probability on the (1,0,0) points. This is just a <i>prior</i> which could be wrong! And in this way it is not strictly analogous to Maximum Likelihood). </p>
<p align="justify">So I have an <img src="https://s0.wp.com/latex.php?latex=%5Calpha&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Calpha&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Calpha&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;alpha" class="latex" /> now and I draw a sample from the Dirichlet. What I actually get is a vector that sums up to 1. I call this <img src="https://s0.wp.com/latex.php?latex=%5Ctheta+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Ctheta+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Ctheta+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;theta " class="latex" />. </p>
<p align="justify">Remember that I&#8217;m trying to generate a random document and I haven&#8217;t generated a single word yet! The <img src="https://s0.wp.com/latex.php?latex=%5Ctheta+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Ctheta+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Ctheta+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;theta " class="latex" /> I have is my guess on the topic vector! I have obtained this <img src="https://s0.wp.com/latex.php?latex=%5Ctheta+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Ctheta+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Ctheta+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;theta " class="latex" /> by sampling from a k-dimensional vector (here k=3 in the above example.) </p>
<p align="justify">Now that <img src="https://s0.wp.com/latex.php?latex=%5Ctheta+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Ctheta+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Ctheta+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;theta " class="latex" /> represents a topic vector which can also be re-imagined as a probability distribution and because any draw is guaranteed to be from the simplex, I can use this drawn vector (<img src="https://s0.wp.com/latex.php?latex=%5Ctheta+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Ctheta+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Ctheta+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;theta " class="latex" />) as the weights of a loaded &#8216;k&#8217; faced die. And I throw this die! Lets say it shows up 5 (a number between 1 and k). I will now say that the word I&#8217;m going to generate belongs to <i>Topic-5</i>. </p>
<p align="justify">I have not yet generated a word! To generate a word that belongs to a topic, I need a |V| faced die. |V| is the size of the vocabulary of the corpus. How do I get such a huge die?! </p>
<p align="justify">I will get that in a similar way as for the topic vector. I will sample again from a Dirichlet &#8211; but a different Dirichlet &#8211; one that is over a v-dimensional simplex. Any draw from this Dirichlet will give a v-faced die. Call this the Dirichlet <img src="https://s0.wp.com/latex.php?latex=%5Cbeta&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cbeta&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cbeta&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;beta" class="latex" />. For each topic (<img src="https://s0.wp.com/latex.php?latex=%5Ctheta&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Ctheta&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Ctheta&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;theta" class="latex" />) you need a different v-faced die (<img src="https://s0.wp.com/latex.php?latex=%5Cbeta&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cbeta&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cbeta&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;beta" class="latex" />). Thus I end up drawing &#8216;k&#8217; such &#8216;v&#8217;-faced dice. </p>
<p align="justify">So for topic-5, I throw the 5th v-faced die. Let us say it shows 42; I then go to the vocabulary and pick the 42nd word!&#160; I will do this whole process &#8216;N&#8217; times (N was the number of words to be generated for the random document.) </p>
<p align="justify">The crux of this discussion is this: for every document , the dice (i.e samples from the dirichlet(<img src="https://s0.wp.com/latex.php?latex=%5Calpha&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Calpha&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Calpha&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;alpha" class="latex" />) and dirichelt(<img src="https://s0.wp.com/latex.php?latex=%5Cbeta&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cbeta&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cbeta&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;beta" class="latex" />) ) are generated only once. It is just that to generate each word, the dice are rolled multiple times. Once to get a topic and once to get a word given this topic! </p>
</li>
<li>
<h3 align="justify">Running LDA on Movie Scripts</h3>
<p align="justify">I converted the imsdb data to a format accepted by Prof. Blei&#8217;s code. For more information you should look at his README. I initially ran the code with k = 10 (guessing that the movie scripts could represent a mixture of 10 genres). I will jump from here to give a sneak peek at the end results. The end results have been very encouraging. A sample of words clustered under different topics is here: </p>
<h3 align="justify">&#160;<img style="display:block;float:none;margin-left:auto;margin-right:auto;" src="https://saravananthirumuruganathan.wordpress.com/wp-content/uploads/2012/01/topics.jpg?w=500" /></img>       <br /></h3>
<p align="justify">At this point eyeballing the clusters it is apparent that the starting from the red cloud, clockwise, the clusters represent romance, sci-fi, war, horror, comedy and crime respectively. </p>
<p align="justify">Just running LDA on the data with k=10 did not turn out such relevant results. So what was that extra mile which had to be crossed? </p>
<p align="justify">LDA does not always guarantee relevant features. Nor is every topic discovered meaningful. After the first run, by eyeballing through the 10 topics, I was able to distinguish 3 topics very easily – war, romance and sci-fi. Playing around with different &#8216;k&#8217; values did not yield more discernible topics. There are reasons to this: </p>
</li>
<li>
<div align="justify">&#8216;war&#8217; and &#8216;sci-fi&#8217; are distinctly different from other genres &#8211; they are pretty exclusive. </div>
</li>
<li>
<div align="justify">words belonging to &#8216;romance&#8217; were getting repeated in other topics. This clearly indicated a skew in the data. </div>
</li>
<li>
<div align="justify">so if I remove the scripts of genres that have been already detected from the dataset, maybe LDA will detect more latent topics due to a change in the underlying data distribution caused by removal of data. </div>
<p align="justify">I removed the scripts corresponding to war and sci-fi from the dataset. This was achieved by comparing each script in the dataset against the top features from these topics. Each script was scored based on the occurance of features. I removed the scripts that scored greater than a threshold. The new dataset D* contained scripts other than war and science fiction movies. Then I ran LDA on this new dataset D*. Now I found that the topics pertaining to crime and comedy were getting discovered but their features were mangled with romance. This feature set was improved by removing words which were common to most categories and were not exclusive to one particular category. </p>
<p align="justify"><a href="http://www.umiacs.umd.edu/~jbg/docs/nips2009-rtl.pdf">This paper </a>deals with using human judgments to examine topics. This is one way of picking the odd feature out of a topic vector.&#160; The final feature set looked something like <a href="http://justpaste.it/mwh">this</a>. </p>
<h3 align="justify">Results</h3>
<p align="justify">I wrote a simple python script that does soft-clustering based on occurance and count of features. A sample of the results is here: </p>
<h3 align="justify"><img src="https://saravananthirumuruganathan.wordpress.com/wp-content/uploads/2012/01/classify.jpg?w=500" />&#160;</h3>
<p align="justify">The results were quite convincing to look at. I wanted to obtain some hard metrics. IMSDB already categorises movies based on its genre. So I consider the category as the actual label. A movie is considered to be rightly classified if the label matches one of the top two genres detected by my system. The following are some encouraging results! </p>
<h3 align="justify"><img src="https://saravananthirumuruganathan.wordpress.com/wp-content/uploads/2012/01/results.jpg?w=500" /> </img>      </p>
</h3>
<p align="justify">These proportions cannot be considered accurate, but they do give an idea of what to expect from a movie! The complete list of results is <a href="https://github.com/saravanan-thirumuruganathan/Misc-Files-Used-in-Blog/blob/master/results.json">here</a> . </p>
</li>
]]></content:encoded>
					
					<wfw:commentRss>https://saravananthirumuruganathan.wordpress.com/2012/01/10/detecting-mixtures-of-genres-in-movie-dialogues/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">633</post-id>
		<media:content url="https://2.gravatar.com/avatar/e2957c9d69690d3fef33fa2bb570d139abce0df84a663fcc30c2ffc3a77b2583?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">saravananthirumuruganathan</media:title>
		</media:content>

		<media:content url="https://saravananthirumuruganathan.wordpress.com/wp-content/uploads/2012/01/topics.jpg?w=500" medium="image" />

		<media:content url="https://saravananthirumuruganathan.wordpress.com/wp-content/uploads/2012/01/classify.jpg?w=500" medium="image" />

		<media:content url="https://saravananthirumuruganathan.wordpress.com/wp-content/uploads/2012/01/results.jpg?w=500" medium="image" />
	</item>
		<item>
		<title>New home for Chrome Nanny</title>
		<link>https://saravananthirumuruganathan.wordpress.com/2011/11/15/new-home-for-chrome-nanny/</link>
					<comments>https://saravananthirumuruganathan.wordpress.com/2011/11/15/new-home-for-chrome-nanny/#comments</comments>
		
		<dc:creator><![CDATA[Saravanan Thirumuruganathan]]></dc:creator>
		<pubDate>Tue, 15 Nov 2011 07:04:07 +0000</pubDate>
				<category><![CDATA[Google Chrome]]></category>
		<category><![CDATA[chrome]]></category>
		<category><![CDATA[chrome nanny]]></category>
		<guid isPermaLink="false">http://saravananthirumuruganathan.wordpress.com/2011/11/15/new-home-for-chrome-nanny/</guid>

					<description><![CDATA[I wrote a Chrome extension called Chrome Nanny. Sometime back, Google took it down as it was against their branding guidelines … So I have created another extension with the same code but with the name : Nanny for Google Chrome. You can find the extension at https://chrome.google.com/webstore/detail/cljcgchbnolheggdgaeclffeagnnmhno . &#160; All new development will take [&#8230;]]]></description>
										<content:encoded><![CDATA[<p align="justify">I wrote a Chrome extension called Chrome Nanny. Sometime back, Google took it down as it was against their branding guidelines … So I have created another extension with the same code but with the name : Nanny for Google Chrome. You can find the extension at <a title="https://chrome.google.com/webstore/detail/cljcgchbnolheggdgaeclffeagnnmhno" href="https://chrome.google.com/webstore/detail/cljcgchbnolheggdgaeclffeagnnmhno">https://chrome.google.com/webstore/detail/cljcgchbnolheggdgaeclffeagnnmhno</a> .</p>
<p align="justify">&#160;</p>
<p align="justify">All new development will take place in this extension . I am still thinking about how to create a migration plan for Chrome Nanny users. I will post as I have more information . I will also update older blogs to point to the new url. Please spread the word !</p>
]]></content:encoded>
					
					<wfw:commentRss>https://saravananthirumuruganathan.wordpress.com/2011/11/15/new-home-for-chrome-nanny/feed/</wfw:commentRss>
			<slash:comments>56</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">624</post-id>
		<media:content url="https://2.gravatar.com/avatar/e2957c9d69690d3fef33fa2bb570d139abce0df84a663fcc30c2ffc3a77b2583?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">saravananthirumuruganathan</media:title>
		</media:content>
	</item>
		<item>
		<title>Back from Hiatus !</title>
		<link>https://saravananthirumuruganathan.wordpress.com/2011/11/15/back-from-hiatus/</link>
					<comments>https://saravananthirumuruganathan.wordpress.com/2011/11/15/back-from-hiatus/#respond</comments>
		
		<dc:creator><![CDATA[Saravanan Thirumuruganathan]]></dc:creator>
		<pubDate>Tue, 15 Nov 2011 06:52:46 +0000</pubDate>
				<category><![CDATA[General]]></category>
		<guid isPermaLink="false">http://saravananthirumuruganathan.wordpress.com/2011/11/15/back-from-hiatus/</guid>

					<description><![CDATA[As many of you noticed, this blog has been very silent for the past 3 months – This was partly due to one of my research papers and few other minor reasons. Now that I know the drill, hopefully, the future papers should not result in full silence in the blog 🙂 Lets see how [&#8230;]]]></description>
										<content:encoded><![CDATA[<p align="justify">As many of you noticed, this blog has been very silent for the past 3 months – This was partly due to one of my research papers and few other minor reasons. Now that I know the drill, hopefully, the future papers should not result in full silence in the blog <img src="https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Lets see how it goes….</p>
<p align="justify">I learned lot of very fascinating this in data mining, machine learning etc Expect few good blog posts. I will try to add a page discussion the next few topics to be blogged. If you want me to discuss about any topic feel free to post it in the comments !</p>
<p align="justify">During the past 3 months, my blog readership has almost doubled .. Hi to all my new readers ! Hope you enjoy my new posts too ! </p>
]]></content:encoded>
					
					<wfw:commentRss>https://saravananthirumuruganathan.wordpress.com/2011/11/15/back-from-hiatus/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">622</post-id>
		<media:content url="https://2.gravatar.com/avatar/e2957c9d69690d3fef33fa2bb570d139abce0df84a663fcc30c2ffc3a77b2583?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">saravananthirumuruganathan</media:title>
		</media:content>
	</item>
		<item>
		<title>Tutorial on Sampling Theory for Approximate Query Processing</title>
		<link>https://saravananthirumuruganathan.wordpress.com/2011/11/15/tutorial-on-sampling-theory-for-approximate-query-processing/</link>
					<comments>https://saravananthirumuruganathan.wordpress.com/2011/11/15/tutorial-on-sampling-theory-for-approximate-query-processing/#respond</comments>
		
		<dc:creator><![CDATA[Saravanan Thirumuruganathan]]></dc:creator>
		<pubDate>Tue, 15 Nov 2011 06:18:13 +0000</pubDate>
				<category><![CDATA[Data Mining]]></category>
		<category><![CDATA[Statistics]]></category>
		<category><![CDATA[Theory]]></category>
		<category><![CDATA[approximate query processing]]></category>
		<category><![CDATA[aqp]]></category>
		<category><![CDATA[probability]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[reservoir sampling]]></category>
		<category><![CDATA[sampling]]></category>
		<category><![CDATA[statistics]]></category>
		<guid isPermaLink="false">http://saravananthirumuruganathan.wordpress.com/2011/11/15/tutorial-on-sampling-theory-for-approximate-query-processing/</guid>

					<description><![CDATA[Prelude This blog post is based on the lecture notes I prepared for one of the courses in which I am the teaching assistant. Hopefully, this notes will be useful for anyone interested in either Approximate Query Processing (AQP) or basic Sampling theory as applied in Databases. Approximate Query Processing Consider a database D which [&#8230;]]]></description>
										<content:encoded><![CDATA[<h3 align="justify">Prelude</h3>
<p align="justify">This blog post is based on the lecture notes I prepared for one of the courses in which I am the teaching assistant. Hopefully, this notes will be useful for anyone interested in either Approximate Query Processing (AQP) or basic Sampling theory as applied in Databases.</p>
<h3 align="justify">Approximate Query Processing</h3>
<p align="justify">Consider a database <strong>D</strong> which contains billions of tuples. Such a large database is not uncommon in data warehousing. When we want to get some answers from the database, we construct a SQL query to get the results. Due to the large size of the database, any query should take quite a bit of time. This is regardless of the use of techniques like indexing which can speed up the processing time but does not really reduce the time asymptotically. </p>
<p align="justify">One observation is that the queries that are posed to the database returns very precise and accurate answers &#8211; probably after looking at each and every tuple. For a lot of use cases of OLAP we may not need such a precise answer. Consider some sample queries like &#8211; what is the ratio of male to female in UTA ? What percentage of US people live in Texas? What is the average salary of all employees in the company? and so on. </p>
<p align="justify">Notice that for such queries we really do not need answers that are correct to the last decimal point. Also notice that each of those query is an <strong>aggregate</strong> over some column. Approximate query processing is a viable technique to use in these cases. A slightly less accurate result but which is computed instantly is desirable in these cases. This is because most analysts are performing exploratory operation on the database and do not need precise answers. An approximate answer along with a confidence interval would suit most of the use cases.</p>
<p align="justify">There are multiple techniques to perform approximate query processing. The two most popular involve histograms and sampling. Histograms store some statistic about the database and then use it to answer queries approximately. Sampling creates a very small subset of the database and then uses this smaller database to answer the query. In this course we will focus on AQP techniques that use sampling. Sampling is a very well studied problem backed up by a rich theory that can guide us in selecting the subset so that we can provide reasonably accurate results and also provide statistical error bounds. </p>
<h3 align="justify">Introduction to Sampling</h3>
<p align="justify">The idea behind sampling is very simple. We want to estimate some specific characteristic of a <strong>population</strong>. For eg this might be the fraction of people who support some presidential candidate or the fraction of people who work in a specific field or fraction of people infected with a disease and so on. The naive strategy is to evaluate the entire population. Most of the time , this is infeasible due to constraints on time, cost or other factors. </p>
<p align="justify">An alternate approach that is usually used is to pick a subset of people . This subset is usually called a <strong>sample</strong>. The size of the sample is usually an order of magnitude smaller than the population. We then use the sample to perform our desired evaluation. Once we get some result, we can use this to estimate the characteristic for the entire population. Sampling theory helps, among other things, on how to select the subset ,&#160; what is the size of population, how to extrapolate the result from sample to original population , how to estimate the confidence interval of our prediction etc. </p>
<p align="justify">The process of randomly picking a subset of the population and using it to perform some estimation should appear very strange. On a first glance it might look that we might get wildly inaccurate results. We will later see how to give statistical guarantees over our prediction. Sampling is a very powerful and popular technique. More and more problems in the real world are being solved using sampling. Lots of recent problems in data mining and machine learning essentially use sampling and randomization to approximately solve very complex problems which are not at all solvable otherwise. This is for this reason that most DBMS provide sampling as a fundamental operator. (Eg Oracle provides a SAMPLE operator in select and also dbms_stats package. SQL Server provides TABLESAMPLE operator and so on).</p>
<p align="justify">We represent the population with <strong>P</strong> and the sample with <strong>S</strong>. <strong>N</strong> represents the size of population and <strong>n</strong> represents the size of the sample. We will use these letters to denote statistics on the population and sample. For eg, <img src="https://s0.wp.com/latex.php?latex=%5Cmu_P&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cmu_P&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cmu_P&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;mu_P" class="latex" /> represents the mean of the population and <img src="https://s0.wp.com/latex.php?latex=%5Cmu_S&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cmu_S&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cmu_S&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;mu_S" class="latex" /> represents the mean of the sample. Similarly, <img src="https://s0.wp.com/latex.php?latex=%5Csigma_P&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Csigma_P&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Csigma_P&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;sigma_P" class="latex" /> and <img src="https://s0.wp.com/latex.php?latex=%5Csigma_S&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Csigma_S&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Csigma_S&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;sigma_S" class="latex" /> represent the standard deviation of the population and sample respectively.</p>
<h3 align="justify">Types of Sampling</h3>
<p align="justify">There are different ways to perform sampling. The ones that we will use most in AQP are :</p>
<h3 align="justify">Sampling With/Without Replacement</h3>
<p align="justify">The aim of sampling is to get a subset (sample) from a larger population. There are two ways to go about it. In the first approach, we randomly pick some entity, perform measurements if any and add it to the sample. We then replace the entity back to the original population and repeat the experiment. This means that same entity can come in the sample multiple times. This approach is called <strong>Sampling with replacement</strong>. This is the simplest approach to sampling. There is no additional overhead to check if an entity is already in sample or not. Typically, sampling with replacement is modeled with <strong>binomial distribution</strong>.</p>
<p align="justify">In the second approach, we explicitly make sure that an entity does not appear in the sample more than once. So we randomly pick an entity from the population, verify it is not already in the sample, perform measurement and so on. Alternatively, we can remove entities that were added to sample from the population. This approach is called <strong>Sampling without replacement</strong> and is usually modeled with an <strong>hypergeometric distribution</strong>.</p>
<h3 align="justify">Bernoulli Sampling</h3>
<p>  </p>
<p align="justify"><strong>Bernoulli Trial :</strong> A Bernoulli Trial is an experiment which has exactly two outcomes &#8211; Success or failure. Each of these outcomes has an associated probability that completely determines the trial. For eg consider a coin which produces head with probability 0.6 and tail with probability 0.4 . This constitutes a bernoulli trial as there are exactly two outcomes.</p>
<p align="justify">In Bernoulli Sampling, we perform a Bernoulli trial on each tuple in the database. Each tuple can be selected into a sample with uniform probability. If the trial is a success, the tuple is added to the sample. Else it is not. The most important thing to notice is that all the tuples have exactly the sample probability of getting into the sample. Alternatively, the success probability for Bernoulli trial remains the same for each tuple. Pseudo code for Bernoulli Sampling is given below :</p>
<blockquote>
<p align="justify">success probability SP = <img src="https://s0.wp.com/latex.php?latex=%5Cfrac%7Bn%7D%7BN%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cfrac%7Bn%7D%7BN%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cfrac%7Bn%7D%7BN%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;frac{n}{N}" class="latex" />      <br />for i = 1 to N       <br />&#160;&#160;&#160; Perform a Bernoulli trial with success probability SP       <br />&#160;&#160;&#160; If outcome is success add i-th tuple to sample       </p>
</blockquote>
<p align="justify">It is important to note that Bernoulli sampling falls under Sampling without replacement. Size of the sample follows a binomial distribution with parameters <img src="https://s0.wp.com/latex.php?latex=B%28N%2C%5Cfrac%7Bn%7D%7BN%7D%29&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=B%28N%2C%5Cfrac%7Bn%7D%7BN%7D%29&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=B%28N%2C%5Cfrac%7Bn%7D%7BN%7D%29&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="B(N,&#92;frac{n}{N})" class="latex" />. ie it can vary between 0 and N-1 with the expected size of sample as <strong>n</strong>.</p>
<h3 align="justify">Uniform Random Sampling</h3>
<p align="justify">In Uniform Random Sampling, we pick each tuple in the database with a constant probability. This means that the probability of any tuple entering the sample is constant. Typically, this is implemented as sampling with replacement. Pseudo code for Uniform Random Sampling is given below :</p>
<blockquote>
<p align="justify">1. Generate a set of <strong>n</strong> random numbers S between 1 and N.       <br />2. Select tuples with index in S and add it to sample.       </p>
</blockquote>
<p align="justify">Note that in this case we have exactly <strong>n</strong> tuples in the sample. We can also notice that sample tuple might appear multiple times. The number of times a tuple appears in the sample forms a binomial distribution with parameters <img src="https://s0.wp.com/latex.php?latex=B%28n%2C%5Cfrac%7B1%7D%7Bn%7D%29&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=B%28n%2C%5Cfrac%7B1%7D%7Bn%7D%29&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=B%28n%2C%5Cfrac%7B1%7D%7Bn%7D%29&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="B(n,&#92;frac{1}{n})" class="latex" />. </p>
<h3 align="justify">Weighted Random Sampling</h3>
<p align="justify">In Weighted Random Sampling, we perform a Bernoulli trial on each tuple in the database. The difference with Uniform random sampling that the success probability for each Bernoulli trial varies. In other words, each tuple has a different probability of getting into the sample.</p>
<h3 align="justify">Stratified Sampling</h3>
<p>  </p>
<p align="justify">If the population can be subdivided into sub population that are distinct and non overlapping , stratified sampling can be used. In Stratified sampling, we split the population into a bunch of strata and then form sampling over each strata independently. For eg a political poll can be stratified on gender, race , state etc.</p>
<p align="justify">There are multiple advantages in using stratified sampling. For one, this allows the convenience to use different sampling techniques over each strata. If there is some specific strata that might be under represented in a general random sampling, we can easily provide additional weights for the samples taken from that. It is also possible to vary the number of samples from a strata to minimize the error. For eg, we can take less number of samples from a strata with low variance while preserving them for strata with high variance.</p>
<p align="justify">Stratified sampling is not a panacea because getting a good stratification strategy may not be obvious. In lot of population, the best feature to stratify is not obvious. Even worse, the population may not contain subgroups that are homogeneous and non overlapping.</p>
<p align="justify">Given a population , sample size and strata there are many ways to allocate the sample across different strata. The two commonly used strategies are :    </p>
<p><strong>1. Proportional Allocation :</strong> In this approach the contribution of each strata to the sample is proportional to the size of the strata. So a strata that accounts to 10\% of the population will use 10\% of the sample. Note that this approach does not use any information about a sub population other than its size.     <br /><strong>2. Variance based Allocation :</strong> This strategy allocates samples in proportion to their variance. So a strata with high variance will have higher representation than one with smaller variance. This is logical as we need few samples to accurately estimate the parameters of a sub population when its variance is low. Any additional samples do not add much additional information or reduce the final estimation dramatically.     </p>
<h3 align="justify">Reservoir Sampling</h3>
<p align="justify">Reservoir Sampling is an algorithm that is widely used to take <strong>n</strong> random samples from a large database of size <strong>N</strong>. The real utility of reservoir sampling is realized when <strong>N</strong> is a large number or is not really known at sample time. This scenario is quite common when the input is a streaming data or when the database is frequently updated . Running the simple uniform random sampling algorithm (say Bernoulli sampling) is inefficient as <strong>N</strong> is large or the old tuples may be purged (or goes out of Sliding Window). Reservoir sampling allows you to get the random sample in a linear pass such that you only inspect any tuple at most once.</p>
<h3 align="justify">Reservoir Sampling with Single Sample</h3>
<p align="justify">Consider the following contrived example. We have a database which is constantly updated and we want to have a single random tuple from it. </p>
<p align="justify">The base case occurs when there is only one tuple in the database. In this case our random sample is the first tuple. Hence the sample&#160; <strong>S = t1</strong>. </p>
<p align="justify">Lets say the database is updated and a new tuple&#160; <strong>t2</strong> is added. The naive approach is to restart the entire sampling process. Instead, in reservoir sampling, we accept the new tuple as the random sample with probability <img src="https://s0.wp.com/latex.php?latex=%5Cfrac%7B1%7D%7B2%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cfrac%7B1%7D%7B2%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cfrac%7B1%7D%7B2%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;frac{1}{2}" class="latex" />. ie toss a coin which returns head with probability 0.5 and if it returns head , then replace <strong>t1</strong> with <strong>t2</strong>.</p>
<p align="justify">We can see why S is a uniform sample. The probability that S contains <strong>t1</strong> or <strong>t2</strong> remains the same.     </p>
<p align="justify">1. <img src="https://s0.wp.com/latex.php?latex=Pr%28S%3Dt1%29+%3D+1+%2A+%5Cfrac%7B1%7D%7B2%7D+%3D+%5Cfrac%7B1%7D%7B2%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=Pr%28S%3Dt1%29+%3D+1+%2A+%5Cfrac%7B1%7D%7B2%7D+%3D+%5Cfrac%7B1%7D%7B2%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=Pr%28S%3Dt1%29+%3D+1+%2A+%5Cfrac%7B1%7D%7B2%7D+%3D+%5Cfrac%7B1%7D%7B2%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="Pr(S=t1) = 1 * &#92;frac{1}{2} = &#92;frac{1}{2}" class="latex" /> . The random sample is t1 when it was selected first into S (with probability 1) and then not rejected by <strong>t2</strong> with probability <img src="https://s0.wp.com/latex.php?latex=1-%5Cfrac%7B1%7D%7B2%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=1-%5Cfrac%7B1%7D%7B2%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=1-%5Cfrac%7B1%7D%7B2%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="1-&#92;frac{1}{2}" class="latex" />.     <br />2. <img src="https://s0.wp.com/latex.php?latex=Pr%28S%3Dt2%29+%3D+%5Cfrac%7B1%7D%7B2%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=Pr%28S%3Dt2%29+%3D+%5Cfrac%7B1%7D%7B2%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=Pr%28S%3Dt2%29+%3D+%5Cfrac%7B1%7D%7B2%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="Pr(S=t2) = &#92;frac{1}{2}" class="latex" /> . The random sample is t2 when it replaces&#160; <strong>t1</strong> in the second step. This occurs with probability <img src="https://s0.wp.com/latex.php?latex=%5Cfrac%7B1%7D%7B2%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cfrac%7B1%7D%7B2%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cfrac%7B1%7D%7B2%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;frac{1}{2}" class="latex" />     </p>
<p align="justify">The database is updated and lets assume a new tuple <strong>t3</strong> is added. We accept the new tuple as the random sample with probability <img src="https://s0.wp.com/latex.php?latex=%5Cfrac%7B1%7D%7B3%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cfrac%7B1%7D%7B3%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cfrac%7B1%7D%7B3%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;frac{1}{3}" class="latex" />. ie toss a coin which returns head with probability 0.33 and if it returns head , then replace the previous value of S (<strong>t1 or t2</strong>) with <strong>t3</strong>. More generally when inspecting the i-th tuple, accept it with probability <img src="https://s0.wp.com/latex.php?latex=%5Cfrac%7B1%7D%7Bi%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cfrac%7B1%7D%7Bi%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cfrac%7B1%7D%7Bi%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;frac{1}{i}" class="latex" />. </p>
<p align="justify">It might look as if we are treating <strong>t3</strong> unfairly because we only accept it with probability <strong>0.33</strong>. But we can show probabilistically that the sample is still uniform. The probability that S is <strong>t1 or t2 or t3</strong> remains the same.&#160; </p>
<p>1. <img src="https://s0.wp.com/latex.php?latex=Pr%28S%3Dt1%29+%3D+1+%5Ctimes+%5Cfrac%7B1%7D%7B2%7D+%5Ctimes+%5Cfrac%7B2%7D%7B3%7D%3D%5Cfrac%7B1%7D%7B3%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=Pr%28S%3Dt1%29+%3D+1+%5Ctimes+%5Cfrac%7B1%7D%7B2%7D+%5Ctimes+%5Cfrac%7B2%7D%7B3%7D%3D%5Cfrac%7B1%7D%7B3%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=Pr%28S%3Dt1%29+%3D+1+%5Ctimes+%5Cfrac%7B1%7D%7B2%7D+%5Ctimes+%5Cfrac%7B2%7D%7B3%7D%3D%5Cfrac%7B1%7D%7B3%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="Pr(S=t1) = 1 &#92;times &#92;frac{1}{2} &#92;times &#92;frac{2}{3}=&#92;frac{1}{3}" class="latex" /> . The only scenario when random sample is still t1 occurs when it was selected first into S (with probability 1) ,not rejected by <strong>t2</strong> with probability <img src="https://s0.wp.com/latex.php?latex=1-%5Cfrac%7B1%7D%7B2%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=1-%5Cfrac%7B1%7D%7B2%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=1-%5Cfrac%7B1%7D%7B2%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="1-&#92;frac{1}{2}" class="latex" /> and not rejected by <strong>t3</strong> with probability <img src="https://s0.wp.com/latex.php?latex=1-%5Cfrac%7B1%7D%7B3%7D+%3D+%5Cfrac%7B2%7D%7B3%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=1-%5Cfrac%7B1%7D%7B3%7D+%3D+%5Cfrac%7B2%7D%7B3%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=1-%5Cfrac%7B1%7D%7B3%7D+%3D+%5Cfrac%7B2%7D%7B3%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="1-&#92;frac{1}{3} = &#92;frac{2}{3}" class="latex" />.     <br />2. <img src="https://s0.wp.com/latex.php?latex=Pr%28S%3Dt2%29+%3D+%5Cfrac%7B1%7D%7B2%7D+%5Ctimes+%5Cfrac%7B2%7D%7B3%7D+%3D+%5Cfrac%7B1%7D%7B3%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=Pr%28S%3Dt2%29+%3D+%5Cfrac%7B1%7D%7B2%7D+%5Ctimes+%5Cfrac%7B2%7D%7B3%7D+%3D+%5Cfrac%7B1%7D%7B3%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=Pr%28S%3Dt2%29+%3D+%5Cfrac%7B1%7D%7B2%7D+%5Ctimes+%5Cfrac%7B2%7D%7B3%7D+%3D+%5Cfrac%7B1%7D%7B3%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="Pr(S=t2) = &#92;frac{1}{2} &#92;times &#92;frac{2}{3} = &#92;frac{1}{3}" class="latex" /> . The random sample is 2 when it replaces <strong>t1</strong> in the second step. This occurs with probability <img src="https://s0.wp.com/latex.php?latex=%5Cfrac%7B1%7D%7B2%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cfrac%7B1%7D%7B2%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cfrac%7B1%7D%7B2%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;frac{1}{2}" class="latex" />. Then in the next step is not replaced by <strong>t3</strong>. This occurs with probability <img src="https://s0.wp.com/latex.php?latex=1-%5Cfrac%7B1%7D%7B3%7D+%3D+%5Cfrac%7B2%7D%7B3%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=1-%5Cfrac%7B1%7D%7B3%7D+%3D+%5Cfrac%7B2%7D%7B3%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=1-%5Cfrac%7B1%7D%7B3%7D+%3D+%5Cfrac%7B2%7D%7B3%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="1-&#92;frac{1}{3} = &#92;frac{2}{3}" class="latex" />.     <br />3. <img src="https://s0.wp.com/latex.php?latex=Pr%28S%3Dt3%29+%3D+%5Cfrac%7B1%7D%7B3%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=Pr%28S%3Dt3%29+%3D+%5Cfrac%7B1%7D%7B3%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=Pr%28S%3Dt3%29+%3D+%5Cfrac%7B1%7D%7B3%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="Pr(S=t3) = &#92;frac{1}{3}" class="latex" /> . The random sample is 3 when S contains either <strong>t1</strong> or <strong>t2</strong> and it is replaced by <strong>t3</strong>. This occurs with probability <img src="https://s0.wp.com/latex.php?latex=%5Cfrac%7B1%7D%7B3%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cfrac%7B1%7D%7B3%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cfrac%7B1%7D%7B3%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;frac{1}{3}" class="latex" />.     </p>
<p align="justify">The pseudo code looks like :</p>
<blockquote>
<p align="justify">S = t1       <br />for i = 2 to N       <br />&#160;&#160;&#160; Replace S with tuple t_i with probability <img src="https://s0.wp.com/latex.php?latex=%5Cfrac%7B1%7D%7Bi%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cfrac%7B1%7D%7Bi%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cfrac%7B1%7D%7Bi%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;frac{1}{i}" class="latex" />       </p>
</blockquote>
<h3 align="justify">Reservoir Sampling with Multiple Samples</h3>
<p align="justify">A very similar approach works when the sample size is more than 1. Lets say that we need a sample of size <strong>n</strong>. Then we initially set the first <strong>n</strong> tuples of the database as the sample. The next steps is a bit different. In the previous case, there was only one sample so we replaced the sample with the selected tuple. When sample size is more than 1, then this steps splits to two parts :     </p>
<p>1. <strong>Acceptance :</strong> For any new tuple t_i, we need to decide if this tuple enters the sample. This occurs with probability <img src="https://s0.wp.com/latex.php?latex=%5Cfrac%7Bn%7D%7Bi%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cfrac%7Bn%7D%7Bi%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cfrac%7Bn%7D%7Bi%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;frac{n}{i}" class="latex" />.     <br />2. <strong>Replacement :</strong> Once we decided to accept the new tuple into the sample, some tuple already in the sample needs to make way. This is usually done randomly. We randomly pick a tuple in the sample and replace it with tuple t_i.</p>
<p align="justify">The pseudo code looks like :</p>
<blockquote>
<p align="justify">Store first n elements into S       <br />for i = n+1 to N       <br />&#160;&#160;&#160; Accept tuple t_i with probability <img src="https://s0.wp.com/latex.php?latex=%5Cfrac%7Bn%7D%7Bi%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cfrac%7Bn%7D%7Bi%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cfrac%7Bn%7D%7Bi%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;frac{n}{i}" class="latex" />       <br />&#160;&#160;&#160; If accepted, replace a random tuple in S with tuple t_i       </p>
</blockquote>
<p align="justify">A coding trick that avoids the &quot;coin tossing&quot; by generating a random index and then accepts it if it is less than our sample size. The pseudo code looks like :</p>
<blockquote>
<p align="justify">Store first n elements into S       <br />for i = n+1 to N       <br />&#160;&#160;&#160; randIndex = random number between 1 and i       <br />&#160;&#160;&#160; if randIndex &lt;= n       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; replace tuple at index &quot;randIndex&quot; in the sample with tuple t_i       </p>
</blockquote>
<p align="justify">We can similarly analyze that the classical reservoir sampling does provide a uniform random sample. Please refer to the paper&#160; Random Sampling with a Reservoir by Jeffrey Vitter for additional details.</p>
<h3 align="justify">Sampling in AQP</h3>
<p>  </p>
<p align="justify"><strong></strong>As discussed above, our main aim is to discuss how sampling techniques is used in AQP. Let us assume that we have a sample S of size <strong>n</strong>. The usual strategy that we will follow is to apply any aggregate query on the sample S instead of database <strong>D</strong>. We then use the result of the query from S to <strong>estimate</strong> the result for D. </p>
<p align="justify">One thing to note is that we will only use aggregate queries for approximate processing. Specifically we will look at <strong>COUNT, SUM</strong> and <strong>AVERAGE</strong>. The formulas for estimating the values of the aggregate query for the entire database from the sample for these 3 operators is well studied. For additional details refer to the paper “<em>Random Sampling from Databases&quot;</em> by Frank Olken.</p>
<p align="justify"><strong>Uniform Random Sample</strong>     </p>
<p>1. <strong>Count :</strong> <img src="https://s0.wp.com/latex.php?latex=%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+T_i+p_i%7D%7Bn%7D+%3D+%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+T_i+%5Cfrac%7B1%7D%7BN%7D%7D%7Bn%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+T_i+p_i%7D%7Bn%7D+%3D+%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+T_i+%5Cfrac%7B1%7D%7BN%7D%7D%7Bn%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+T_i+p_i%7D%7Bn%7D+%3D+%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+T_i+%5Cfrac%7B1%7D%7BN%7D%7D%7Bn%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;frac{&#92;sum_{i=1}^{n} T_i p_i}{n} = &#92;frac{&#92;sum_{i=1}^{n} T_i &#92;frac{1}{N}}{n}" class="latex" /> where <img src="https://s0.wp.com/latex.php?latex=T_i&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=T_i&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=T_i&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="T_i" class="latex" /> is an indicator random variable that is 1 when tuple t_i satisfies our clause. <img src="https://s0.wp.com/latex.php?latex=p_i&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=p_i&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=p_i&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="p_i" class="latex" /> is the probability that tuple will be selected into the sample. Intuitively, the formula finds the fraction of tuples in Sample which satisfied the query and applies the same fraction to the entire database.     <br />2. <strong>Sum :</strong> <img src="https://s0.wp.com/latex.php?latex=%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+x_i+%5Cfrac%7B1%7D%7Bp_i%7D%7D%7Bn%7D+%3D+%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+x_i+N%7D%7Bn%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+x_i+%5Cfrac%7B1%7D%7Bp_i%7D%7D%7Bn%7D+%3D+%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+x_i+N%7D%7Bn%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+x_i+%5Cfrac%7B1%7D%7Bp_i%7D%7D%7Bn%7D+%3D+%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+x_i+N%7D%7Bn%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;frac{&#92;sum_{i=1}^{n} x_i &#92;frac{1}{p_i}}{n} = &#92;frac{&#92;sum_{i=1}^{n} x_i N}{n}" class="latex" />     <br />3. <strong>Average : </strong><img src="https://s0.wp.com/latex.php?latex=%5Cfrac%7BSum%7D%7BCount%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cfrac%7BSum%7D%7BCount%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cfrac%7BSum%7D%7BCount%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;frac{Sum}{Count}" class="latex" />     <br />\end{itemize}</p>
<p align="justify"><strong>Weighted Random Sample</strong>     </p>
<p>1. <strong>Count : </strong><img src="https://s0.wp.com/latex.php?latex=%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+T_i+p_i%7D%7Bn%7D+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+T_i+p_i%7D%7Bn%7D+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+T_i+p_i%7D%7Bn%7D+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;frac{&#92;sum_{i=1}^{n} T_i p_i}{n} " class="latex" /> where <img src="https://s0.wp.com/latex.php?latex=T_i&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=T_i&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=T_i&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="T_i" class="latex" /> is an indicator random variable that is 1 when tuple t_i satisfies our clause. <img src="https://s0.wp.com/latex.php?latex=p_i&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=p_i&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=p_i&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="p_i" class="latex" /> is the probability that tuple will be selected into the sample. Intuitively, the formula reweighs each tuple according to the selection probability of the tuple.     <br />2. <strong>Sum :</strong> <img src="https://s0.wp.com/latex.php?latex=%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+x_i+%5Cfrac%7B1%7D%7Bp_i%7D%7D%7Bn%7D+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+x_i+%5Cfrac%7B1%7D%7Bp_i%7D%7D%7Bn%7D+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+x_i+%5Cfrac%7B1%7D%7Bp_i%7D%7D%7Bn%7D+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;frac{&#92;sum_{i=1}^{n} x_i &#92;frac{1}{p_i}}{n} " class="latex" />     <br />3. <strong>Average : </strong><img src="https://s0.wp.com/latex.php?latex=%5Cfrac%7BSum%7D%7BCount%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cfrac%7BSum%7D%7BCount%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cfrac%7BSum%7D%7BCount%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;frac{Sum}{Count}" class="latex" />     </p>
<h3 align="justify">Probability/Statistics Refresher</h3>
<p align="justify">Here are few commonly used equations related to Expectation and variance.</p>
<p align="justify">1. E[X] = <img src="https://s0.wp.com/latex.php?latex=%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+x_i+p_i&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+x_i+p_i&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+x_i+p_i&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;sum_{i=1}^{n} x_i p_i" class="latex" />    <br />2. E[aX+b] = aE[X] + b     <br />3. E[X+Y] = E[X] + E[Y] (also called Linearity of Expectations)     <br />4. Var[X] = <img src="https://s0.wp.com/latex.php?latex=E%5B+%28X+-+E%5BX%5D+%29+%5E2%5D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=E%5B+%28X+-+E%5BX%5D+%29+%5E2%5D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=E%5B+%28X+-+E%5BX%5D+%29+%5E2%5D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="E[ (X - E[X] ) ^2]" class="latex" />     <br />5. Var[X+a] = Var[X]     <br />6. Var[aX] = <img src="https://s0.wp.com/latex.php?latex=a%5E2&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=a%5E2&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=a%5E2&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="a^2" class="latex" /> Var[X]     <br />7. Var[X+Y] = Var[X] + Var[Y] if X and Y are independent.     </p>
<p align="justify"><strong>Law of Large Numbers :</strong> This law is one of the fundamental laws in probability. Let <img src="https://s0.wp.com/latex.php?latex=X_1%2CX_2+%5Cldots+%2C+X_n&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=X_1%2CX_2+%5Cldots+%2C+X_n&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=X_1%2CX_2+%5Cldots+%2C+X_n&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="X_1,X_2 &#92;ldots , X_n" class="latex" /> be random variables drawn iid. Very informally, as n increases, the average of the variables approaches the expected value of the distribution from which the variables are drawn. For eg, if we have a coin which provides head with probability 0.5 and toss it 10000 times, the number of heads will be very close to 5000. </p>
<p align="justify"><strong>Binomial Distribution:</strong> Suppose you repeat a Bernoulli trial with success probability of <strong>p</strong> , <strong>n</strong> times. The distribution of the number of successes in the <strong>n</strong> trials is provided by binomial distribution <strong>B(n,p)</strong>. This is a very important distribution for modeling sampling with replacement. For eg if we perform Bernoulli sampling, the final size of the sample is a Binomial distribution. Also if we randomly pick n tuples from database of size N , the number of times any tuple is repeated in the sample can also modeled by Binomial distribution. The expected value is given by <img src="https://s0.wp.com/latex.php?latex=E%5BX%5D%3Dnp&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=E%5BX%5D%3Dnp&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=E%5BX%5D%3Dnp&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="E[X]=np" class="latex" /> and variance is given by <img src="https://s0.wp.com/latex.php?latex=np%281-p%29&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=np%281-p%29&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=np%281-p%29&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="np(1-p)" class="latex" />.</p>
<p align="justify"><strong>Normal Distribution :</strong> Normal distribution , aka Gaussian distribution, is one of the most important probability distributions. It is usually represented with parameters <img src="https://s0.wp.com/latex.php?latex=N%28%5Cmu%2C%5Csigma%5E2%29&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=N%28%5Cmu%2C%5Csigma%5E2%29&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=N%28%5Cmu%2C%5Csigma%5E2%29&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="N(&#92;mu,&#92;sigma^2)" class="latex" />. It has the familiar bell curve shape. <img src="https://s0.wp.com/latex.php?latex=%5Cmu&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cmu&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cmu&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;mu" class="latex" /> determines the center of the normal curve and <img src="https://s0.wp.com/latex.php?latex=%5Csigma%5E2&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Csigma%5E2&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Csigma%5E2&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;sigma^2" class="latex" /> determines the width of it. A smaller value results in a tighter curve while a larger value results in a more flat/wide curve.</p>
<p align="justify">Equations (2) and (7) above gives us some detail about how expected value and variance of sum of two random variables can be computed from the expected value/variance of the constituent random variables . We can extend by induction for the rules to hold for the sum of arbitrarily large number of random variables. This introduces us to the concept of Central Limit Theorem.</p>
<p align="justify"><strong>Central Limit Theorem :</strong>&#160; This is one of the most fundamental rules in probability. Assume that we have <strong>n</strong> different random variables <img src="https://s0.wp.com/latex.php?latex=X_1%2CX_2+%5Cldots+%2C+X_n&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=X_1%2CX_2+%5Cldots+%2C+X_n&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=X_1%2CX_2+%5Cldots+%2C+X_n&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="X_1,X_2 &#92;ldots , X_n" class="latex" />. Each of these random variables have mean <img src="https://s0.wp.com/latex.php?latex=%5Cmu&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cmu&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cmu&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;mu" class="latex" /> and variance <img src="https://s0.wp.com/latex.php?latex=%5Csigma%5E2&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Csigma%5E2&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Csigma%5E2&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;sigma^2" class="latex" />. For a large n, the distribution of sums <img src="https://s0.wp.com/latex.php?latex=X+%3D+X_1+%2B+X_2+%2B+%5Cldots+%2B+X_n&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=X+%3D+X_1+%2B+X_2+%2B+%5Cldots+%2B+X_n&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=X+%3D+X_1+%2B+X_2+%2B+%5Cldots+%2B+X_n&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="X = X_1 + X_2 + &#92;ldots + X_n" class="latex" /> is normal with parameters <img src="https://s0.wp.com/latex.php?latex=N%28n+%5Cmu%2C+n+%5Csigma%5E2%29&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=N%28n+%5Cmu%2C+n+%5Csigma%5E2%29&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=N%28n+%5Cmu%2C+n+%5Csigma%5E2%29&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="N(n &#92;mu, n &#92;sigma^2)" class="latex" />. Similarly, the distribution of the averages , <img src="https://s0.wp.com/latex.php?latex=X+%3D+%5Cfrac%7BX_1+%2B+X_2+%2B+%5Cldots+%2B+X_n%7D%7Bn%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=X+%3D+%5Cfrac%7BX_1+%2B+X_2+%2B+%5Cldots+%2B+X_n%7D%7Bn%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=X+%3D+%5Cfrac%7BX_1+%2B+X_2+%2B+%5Cldots+%2B+X_n%7D%7Bn%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="X = &#92;frac{X_1 + X_2 + &#92;ldots + X_n}{n}" class="latex" /> is normal with parameters <img src="https://s0.wp.com/latex.php?latex=N%28%5Cmu%2C+%5Cfrac%7B%5Csigma%5E2%7D%7Bn%7D%29&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=N%28%5Cmu%2C+%5Cfrac%7B%5Csigma%5E2%7D%7Bn%7D%29&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=N%28%5Cmu%2C+%5Cfrac%7B%5Csigma%5E2%7D%7Bn%7D%29&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="N(&#92;mu, &#92;frac{&#92;sigma^2}{n})" class="latex" />.</p>
<p align="justify">Law of large numbers and Central limit theorem are two important results that allow analysis of the results. law of large number says that if we pick a large enough sample then the average of the sample will be closer to the true average of population. Central limit theorem states that if you repeat the sampling experiment multiple times and plot the distribution of the average value of the samples, they follow a normal distribution. Jointly, they allow you to derive the expression for Standard error.</p>
<h3 align="justify"><strong>Standard Error</strong></h3>
<p>  </p>
<p align="justify"><strong></strong>The essential idea behind sampling is to perform the experiment on a randomly chosen subset of the population instead of the original population. So far we discussed how to perform the sampling and how to estimate the value from sample to the larger population. In this section let us discuss about the error in our prediction. The concept of standard error is usually used for the same.</p>
<p align="justify">Lets say we want to estimate the mean of the population <img src="https://s0.wp.com/latex.php?latex=%5Cmu_P&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cmu_P&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cmu_P&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;mu_P" class="latex" /> . We performed the sampling and found the sample mean as <img src="https://s0.wp.com/latex.php?latex=%5Cmu_S&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cmu_S&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cmu_S&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;mu_S" class="latex" />. Since the sample mean is an unbiased estimator of the population mean we can announce that they are the same. But it need not always be the case that these two values are same. There are two common ways to analyze the error. </p>
<p align="justify">1. <strong>Absolute Error </strong>: <img src="https://s0.wp.com/latex.php?latex=%7C%5Cmu_P+-+%5Cmu_S%7C%5E2&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%7C%5Cmu_P+-+%5Cmu_S%7C%5E2&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%7C%5Cmu_P+-+%5Cmu_S%7C%5E2&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="|&#92;mu_P - &#92;mu_S|^2" class="latex" />     <br />2. <strong>Relative Error </strong>: <img src="https://s0.wp.com/latex.php?latex=%5Cfrac%7B%7C%5Cmu_P+-+%5Cmu_S%7C%5E2%7D%7B%5Cmu_P%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cfrac%7B%7C%5Cmu_P+-+%5Cmu_S%7C%5E2%7D%7B%5Cmu_P%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cfrac%7B%7C%5Cmu_P+-+%5Cmu_S%7C%5E2%7D%7B%5Cmu_P%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;frac{|&#92;mu_P - &#92;mu_S|^2}{&#92;mu_P}" class="latex" />     </p>
<p align="justify">The primary problem we have in estimating these error metrics is that we do not know the value of <img src="https://s0.wp.com/latex.php?latex=%5Cmu_P&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cmu_P&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cmu_P&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;mu_P" class="latex" />. We will use an indirect way to estimate the error. </p>
<p align="justify">Consider the sampling process again. We picked a sample <img src="https://s0.wp.com/latex.php?latex=S%3D%7BX_1%2CX_2%2C%5Cldots%2CX_n%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=S%3D%7BX_1%2CX_2%2C%5Cldots%2CX_n%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=S%3D%7BX_1%2CX_2%2C%5Cldots%2CX_n%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="S={X_1,X_2,&#92;ldots,X_n}" class="latex" /> of random tuples from the database and computed the sample mean as <img src="https://s0.wp.com/latex.php?latex=%5Cmu_S%3D%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+X_i%7D%7Bn%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cmu_S%3D%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+X_i%7D%7Bn%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cmu_S%3D%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+X_i%7D%7Bn%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;mu_S=&#92;frac{&#92;sum_{i=1}^{n} X_i}{n}" class="latex" />. But since the tuples in S are picked randomly, the sample mean changes based on the sample. This means that the <strong>sample mean </strong><img src="https://s0.wp.com/latex.php?latex=%5Cmu_S&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cmu_S&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cmu_S&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;mu_S" class="latex" /> is itself a <strong>random variable</strong>.</p>
<p align="justify">Let the population <img src="https://s0.wp.com/latex.php?latex=P%3D%7BX_1%2CX_2%2C%5Cldots%2CX_N%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=P%3D%7BX_1%2CX_2%2C%5Cldots%2CX_N%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=P%3D%7BX_1%2CX_2%2C%5Cldots%2CX_N%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="P={X_1,X_2,&#92;ldots,X_N}" class="latex" />. Then the population mean <img src="https://s0.wp.com/latex.php?latex=%5Cmu_P+%3D+%5Coverline%7BX%7D+%3D+%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7BN%7D+X_i%7D%7BN%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cmu_P+%3D+%5Coverline%7BX%7D+%3D+%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7BN%7D+X_i%7D%7BN%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cmu_P+%3D+%5Coverline%7BX%7D+%3D+%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7BN%7D+X_i%7D%7BN%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;mu_P = &#92;overline{X} = &#92;frac{&#92;sum_{i=1}^{N} X_i}{N}" class="latex" />.&#160; Let the sample be <img src="https://s0.wp.com/latex.php?latex=S%3D%7BY_1%2CY_2%2C%5Cldots%2CY_n%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=S%3D%7BY_1%2CY_2%2C%5Cldots%2CY_n%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=S%3D%7BY_1%2CY_2%2C%5Cldots%2CY_n%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="S={Y_1,Y_2,&#92;ldots,Y_n}" class="latex" /> and sample mean is <img src="https://s0.wp.com/latex.php?latex=%5Cmu_S+%3D+%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+Y_i%7D%7Bn%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cmu_S+%3D+%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+Y_i%7D%7Bn%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cmu_S+%3D+%5Cfrac%7B%5Csum_%7Bi%3D1%7D%5E%7Bn%7D+Y_i%7D%7Bn%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;mu_S = &#92;frac{&#92;sum_{i=1}^{n} Y_i}{n}" class="latex" />.</p>
<h3 align="justify">Standard Error when Sample Size = 1</h3>
<p>  </p>
<p align="justify">Consider the case where the sample S consists of only one element. Our aim is to find population mean. As per our approach, we pick a random element <img src="https://s0.wp.com/latex.php?latex=Y_1&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=Y_1&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=Y_1&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="Y_1" class="latex" /> and then announce it as the sample mean ie <img src="https://s0.wp.com/latex.php?latex=%7B%5Cmu%7D_S%3DY_1&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%7B%5Cmu%7D_S%3DY_1&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%7B%5Cmu%7D_S%3DY_1&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="{&#92;mu}_S=Y_1" class="latex" /> . The error in this case is the difference between <img src="https://s0.wp.com/latex.php?latex=Y_1&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=Y_1&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=Y_1&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="Y_1" class="latex" /> and <img src="https://s0.wp.com/latex.php?latex=%5Cmu_P&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cmu_P&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cmu_P&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;mu_P" class="latex" />. Since the sample is randomly picked , the sample mean is a random variable. Then it also implies that the error is also a random variable. </p>
<p align="left">We can derive the expected value for the error as follows :    <br /><img src="https://s0.wp.com/latex.php?latex=E%5Berr%5E2%5D+%3D+%5Csum_%7Bi%3D1%7D%5E%7BN%7D%5Cfrac%7B%28%5Coverline%7BX%7D-X_i%29%5E2%7D%7BN%7D+%3D+%5Csigma_P%5E2&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=E%5Berr%5E2%5D+%3D+%5Csum_%7Bi%3D1%7D%5E%7BN%7D%5Cfrac%7B%28%5Coverline%7BX%7D-X_i%29%5E2%7D%7BN%7D+%3D+%5Csigma_P%5E2&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=E%5Berr%5E2%5D+%3D+%5Csum_%7Bi%3D1%7D%5E%7BN%7D%5Cfrac%7B%28%5Coverline%7BX%7D-X_i%29%5E2%7D%7BN%7D+%3D+%5Csigma_P%5E2&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="E[err^2] = &#92;sum_{i=1}^{N}&#92;frac{(&#92;overline{X}-X_i)^2}{N} = &#92;sigma_P^2" class="latex" />.</p>
<p align="justify">We can see that the expected value of the error is nothing but the standard deviation of the population !</p>
<h3 align="justify">Standard Error when Sample Size = 2</h3>
<p align="justify">Consider the case where the sample S consists of exactly two elements. Our aim is to find population mean. As per our approach, we pick two random elements <img src="https://s0.wp.com/latex.php?latex=Y_1%2CY_2&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=Y_1%2CY_2&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=Y_1%2CY_2&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="Y_1,Y_2" class="latex" /> and then announce the sample mean as <img src="https://s0.wp.com/latex.php?latex=%7B%5Cmu%7D_S+%3D+%5Cfrac%7BY_1%2BY_2%7D%7B2%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%7B%5Cmu%7D_S+%3D+%5Cfrac%7BY_1%2BY_2%7D%7B2%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%7B%5Cmu%7D_S+%3D+%5Cfrac%7BY_1%2BY_2%7D%7B2%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="{&#92;mu}_S = &#92;frac{Y_1+Y_2}{2}" class="latex" />. The error in this case is the difference between <img src="https://s0.wp.com/latex.php?latex=%5Cmu_S&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cmu_S&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cmu_S&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;mu_S" class="latex" /> and <img src="https://s0.wp.com/latex.php?latex=%5Cmu_P&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cmu_P&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cmu_P&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;mu_P" class="latex" />. Since the samples are randomly picked , the sample mean is again a random variable. Then it also implies that the error is also a random variable. </p>
<p align="justify">We can derive the expected value for the error as <img src="https://s0.wp.com/latex.php?latex=E%5Berr%5E2%5D+%3D+E%5B%28%5Coverline%7BX%7D+-+%5Coverline%7BY%7D%29%5E2%5D+%3D+Var%5B%5Coverline%7BY%7D%5D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=E%5Berr%5E2%5D+%3D+E%5B%28%5Coverline%7BX%7D+-+%5Coverline%7BY%7D%29%5E2%5D+%3D+Var%5B%5Coverline%7BY%7D%5D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=E%5Berr%5E2%5D+%3D+E%5B%28%5Coverline%7BX%7D+-+%5Coverline%7BY%7D%29%5E2%5D+%3D+Var%5B%5Coverline%7BY%7D%5D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="E[err^2] = E[(&#92;overline{X} - &#92;overline{Y})^2] = Var[&#92;overline{Y}]" class="latex" />. We can see that the sample can be any one of <img src="https://s0.wp.com/latex.php?latex=N%5E2&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=N%5E2&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=N%5E2&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="N^2" class="latex" /> different combination of elements . The calculation for variance might be tricky if not for the independence of samples. Since the two elements were picked randomly , they two are independent and we can use that to estimate the variance easily. </p>
<p align="justify"><img src="https://s0.wp.com/latex.php?latex=Var%5B%5Coverline%7BY%7D%5D+%3D+Var+%5B%5Cfrac%7BY_1+%2B+Y_2%7D%7B2%7D%5D+%3D+Var+%5B%5Cfrac%7BY_1%7D%7B2%7D+%2B+%5Cfrac%7BY_2%7D%7B2%7D%5D+%3D+%5Cfrac%7B1%7D%7B4%7D+Var%5BY_1%5D+%2B+%5Cfrac%7B1%7D%7B4%7D+Var%5BY_2%5D+%3D+%5Cfrac%7B%5Csigma_P%5E2%7D%7B4%7D+%2B+%5Cfrac%7B%5Csigma_P%5E2%7D%7B4%7D+%3D+%5Cfrac%7B%5Csigma_P%5E2%7D%7B2%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=Var%5B%5Coverline%7BY%7D%5D+%3D+Var+%5B%5Cfrac%7BY_1+%2B+Y_2%7D%7B2%7D%5D+%3D+Var+%5B%5Cfrac%7BY_1%7D%7B2%7D+%2B+%5Cfrac%7BY_2%7D%7B2%7D%5D+%3D+%5Cfrac%7B1%7D%7B4%7D+Var%5BY_1%5D+%2B+%5Cfrac%7B1%7D%7B4%7D+Var%5BY_2%5D+%3D+%5Cfrac%7B%5Csigma_P%5E2%7D%7B4%7D+%2B+%5Cfrac%7B%5Csigma_P%5E2%7D%7B4%7D+%3D+%5Cfrac%7B%5Csigma_P%5E2%7D%7B2%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=Var%5B%5Coverline%7BY%7D%5D+%3D+Var+%5B%5Cfrac%7BY_1+%2B+Y_2%7D%7B2%7D%5D+%3D+Var+%5B%5Cfrac%7BY_1%7D%7B2%7D+%2B+%5Cfrac%7BY_2%7D%7B2%7D%5D+%3D+%5Cfrac%7B1%7D%7B4%7D+Var%5BY_1%5D+%2B+%5Cfrac%7B1%7D%7B4%7D+Var%5BY_2%5D+%3D+%5Cfrac%7B%5Csigma_P%5E2%7D%7B4%7D+%2B+%5Cfrac%7B%5Csigma_P%5E2%7D%7B4%7D+%3D+%5Cfrac%7B%5Csigma_P%5E2%7D%7B2%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="Var[&#92;overline{Y}] = Var [&#92;frac{Y_1 + Y_2}{2}] = Var [&#92;frac{Y_1}{2} + &#92;frac{Y_2}{2}] = &#92;frac{1}{4} Var[Y_1] + &#92;frac{1}{4} Var[Y_2] = &#92;frac{&#92;sigma_P^2}{4} + &#92;frac{&#92;sigma_P^2}{4} = &#92;frac{&#92;sigma_P^2}{2}" class="latex" /> </p>
<p align="justify">Note that we used rules 3, 6 and 7 from above. </p>
<h3 align="justify">Standard Error when Sample Size = n</h3>
<p align="justify">We will not derive the formula here but we can easily show that when the sample contains n different elements the standard error is given by <img src="https://s0.wp.com/latex.php?latex=E%5Berr%5E2%5D+%3D+%5Cfrac%7B%5Csigma_P%5E2%7D%7Bn%7D+%3D+%5Cfrac%7BVariance%5C%3Bof%5C%3Bpopulation%7D%7BSample%5C%3Bsize%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=E%5Berr%5E2%5D+%3D+%5Cfrac%7B%5Csigma_P%5E2%7D%7Bn%7D+%3D+%5Cfrac%7BVariance%5C%3Bof%5C%3Bpopulation%7D%7BSample%5C%3Bsize%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=E%5Berr%5E2%5D+%3D+%5Cfrac%7B%5Csigma_P%5E2%7D%7Bn%7D+%3D+%5Cfrac%7BVariance%5C%3Bof%5C%3Bpopulation%7D%7BSample%5C%3Bsize%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="E[err^2] = &#92;frac{&#92;sigma_P^2}{n} = &#92;frac{Variance&#92;;of&#92;;population}{Sample&#92;;size}" class="latex" />.</p>
<p align="justify">There are some interesting things to note here :&#160; </p>
<p>1. The expected error when using 2 samples is less than that of the expected error when we used only one sample.     <br />2. As the size of sample increases the error decreases even faster. The rate of decrease is inversely proportional to square of size of the sample size.     <br />3. This also formalizes the intuition that if the variance of the population is less, we need less number of samples to provide estimates with small error.     <br />4. Usually our work will dictate the tolerable error and we can use the formula to find the appropriate n that will make standard error less than our tolerance factor.     </p>
<p align="justify">Hope this post was useful <img src="https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://saravananthirumuruganathan.wordpress.com/2011/11/15/tutorial-on-sampling-theory-for-approximate-query-processing/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">614</post-id>
		<media:content url="https://2.gravatar.com/avatar/e2957c9d69690d3fef33fa2bb570d139abce0df84a663fcc30c2ffc3a77b2583?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">saravananthirumuruganathan</media:title>
		</media:content>
	</item>
		<item>
		<title>Tutorial on writing Ubuntu Unity Lenses/Places in Python</title>
		<link>https://saravananthirumuruganathan.wordpress.com/2011/08/05/tutorial-on-writing-ubuntu-lensesplaces-in-python/</link>
					<comments>https://saravananthirumuruganathan.wordpress.com/2011/08/05/tutorial-on-writing-ubuntu-lensesplaces-in-python/#comments</comments>
		
		<dc:creator><![CDATA[Saravanan Thirumuruganathan]]></dc:creator>
		<pubDate>Fri, 05 Aug 2011 16:41:33 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[dbus]]></category>
		<category><![CDATA[imdb]]></category>
		<category><![CDATA[imdbpy]]></category>
		<category><![CDATA[lens]]></category>
		<category><![CDATA[place]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[unity]]></category>
		<guid isPermaLink="false">http://saravananthirumuruganathan.wordpress.com/2011/08/05/tutorial-on-writing-ubuntu-lensesplaces-in-python/</guid>

					<description><![CDATA[Ubuntu Unity is a really cool and innovative approach for Ubuntu UI. There are many people who love it and many who hate it. I fall somewhere in the middle. I am really fascinated by the possibilities that Unity offers and hope that it becomes better as it matures. Lenses are one of the coolest [&#8230;]]]></description>
										<content:encoded><![CDATA[<p align="justify">Ubuntu Unity is a really cool and innovative approach for Ubuntu UI. There are many people who love it and many who hate it. I fall somewhere in the middle. I am really fascinated by the possibilities that Unity offers and hope that it becomes better as it matures. Lenses are one of the coolest pieces in Unity and unfortunately not much tutorial is written. So this is my stab to fill that void. I have tried to organize as many material as possible. Your comments on improving the article is always welcome !</p>
<p align="justify">In <a href="https://saravananthirumuruganathan.wordpress.com/2011/05/28/detailed-tutorial-to-unity/"><strong>my tutorial on Unity</strong></a> , I mentioned that if no one else writes a Lens tutorial, I will write one and here it is <img src="https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Lenses in Ubuntu are a really neat idea &#8211; One simplistic way is to view them as specialized search tools. You open the appropriate lens, type something and you get a list of results. But this explanation does not really do justice to the incredible amount of flexibility that they give. Lenses can group their results, have Unity render the results in different styles, have multiple sections that influence they way search is done and so on. Unity also provides a very powerful API that allows you to write literally anything that you can think of. This post will discuss the architecture, components and their potential capabilities in detail.</p>
<h3 align="justify">Ubuntu Lens Terminology</h3>
<p align="justify">If you are using Unity, then you must be familiar with two default lens that come with it : Application lens and Files &amp; Folders lens. Let me use that to explain the various parts of a Lens.</p>
<p align="justify"><strong>Places/Lenses :</strong> Both these words represent the same idea. Places is a terminology that was primarily used when Unity was introduced. Its usage is now slowly deprecated and the word Lenses is preferred. In the rest of this blog post, I will refer it as Lenses for uniformity. Let us take a look at the various parts of lens by taking the application lens as an example.</p>
<p align="justify">If you click on the Application lens, you can see that it is split into two important pieces. At the top, is a search textbox like entity that allows you to enter the query. At the bottom is the results pane that shows results of your query. At the left end of the search textbox is a search result indicator. When the search is still going on, the indicator will show a spinning circle. Once the search is done, it reverts to its default icon of a cross.</p>
<p align="justify"><strong>Sections : </strong>The simplest way to describe sections is that they behave like categories that are non overlapping. So if a given search query might result in different results when searched under different categories, each of the qualify as a section.</p>
<p align="justify">Consider a simple example &#8211; You are writing a lens for StackOverflow. You may want to search questions, tags, users and so on. The results of search query q as a question is not the same as when searching q as a tag. Hence questions, tags, users become sections. Unity allows you to easily have different sections and switch between them.</p>
<p align="justify">Sections are the entries in the dropdown list  at the right end of the search textbox. They are optional and many places need not have it. You can see all sections of a lens by clicking on the dropdown. Taking the example of Application lens, some of the sections are &#8220;All Applications&#8221;, &#8220;Accessories&#8221;, &#8220;Games&#8221;, &#8220;Graphics&#8221; etc.</p>
<p align="justify"><strong>Groups :</strong> Groups is a mechanism by which you can club certain search results under a meaningful entity. Each search result, might have one or more groups that allow you to organize the results. For eg, when you search in Application lens, the groups are &#8220;Most frequently used&#8221;, &#8220;Installed&#8221;, &#8220;Available for download&#8221; etc. Groups need not be non overlapping like sections. Given a search query, same result can occur in different groups. In addition, Unity allows you to have different icons for different groups. Also, each group can be rendered in a different style. I will discuss further details later in the blog post.</p>
<p align="justify"><strong>Difference between Sections and Groups :</strong> There are lot of differences between groups and sections. Sections are mostly non overlapping set of entries. Each section provides a different perspective of the same search query. In contrast, groups allow you to classify the search results from a query (and a section). Groups can be considered as a way to organize the results rather than influencing how the results are obtained (which the sections do).</p>
<p align="justify"><strong>Results : </strong>These are the output of a search query in a lens. Given a query and a section, you get a list of entries for results. Each of them can optionally be assigned to a group. Each result entry can display information about themselves in a concise way. In addition, Unity allows them to have any URI and a mime type. So the results of one lens might be files clicking which executes them. Or it can be results from web and clicking on them opens the link in a browser and so on. Unity also allows them to have custom icons.</p>
<h3 align="justify">Unity Lens &#8211; Technical Terminology</h3>
<p align="justify">In the following section, we will discuss some terms used in Lens development.</p>
<p align="justify"><strong>DBUS :</strong> DBUS is one of the most important component in the development of lens. One way to view a lens is as a daemon that accepts a search query from Unity, does some processing and returns a result along with information on how to display them. DBUS is the conduit through which the entire communication between the daemon and Unity happens. For the purpose of the lens, you need to own a bus which will be used by Unity for communication.</p>
<p align="justify"><strong>Lens/Place Daemon :</strong> Lens/Place daemon is a program that exposes lens functionality. A single daemon can expose multiple lens functionalities. Each lens is identified by an entry. It accepts search string from Unity and sends out search results that annotated with group and other additional information.</p>
<p align="justify"><strong>Place File :</strong> A place file is how you inform Unity about the various lenses that your daemon is going to expose. It is a text file that ends with .place extension and usually placed in <strong>/usr/share/unity/places </strong>. It contains different groups of entries that describe the different lenses that are exposed.</p>
<p align="justify"><strong>Lens Entry :</strong> A lens or an entry in the place file is distinct entity that can accept a search query and provide appropriate results. Every place file contains atleast one lens entry. Every entry has a distinct location for itself in the Unity places bar (the vertical strip) on the left. The lens entry has 3 broad components : A model each for sections, groups and results. Results contain information about the groups they fall in and how to display themselves.</p>
<p align="justify"><strong>Results :</strong> As discussed above, a result is an entry that is the response to a query. A result can be assigned to one or more groups. In the most general sense, a result consists of 5 subparts :<br />
(a) <strong>URI</strong> : This is a resource identifier to uniquely denote the location of search result. It can be a local item (file://) , web item (<a href="http://)">http://)</a> and so on.<br />
(b) <strong>Display Name </strong>: This is a name for the result that the user will see in the results pane.<br />
(c) <strong>Comment</strong> : This is additional information about the result entry.<br />
(d) <strong>MimeType </strong>: Since the URI can point to anything, it is important to inform Unity what action to perform when the user clicks on the result. The mimetype allows Unity to invoke the appropriate launcher.<br />
(e) <strong>Icon</strong> : Each result can have its own icon to represent it visually. It can be an icon file or the generic name of the icon (eg video).</p>
<p align="justify"><strong>Renderer :</strong> Unity provides some flexibility in how the results are displayed. Each result falls into some group. We can request Unity to display each group in a specific way. This is accomplished by the use of renderers. When you setup the model for groups for the lens, we also specify how each group will be rendered. Some common renderers are :</p>
<p align="justify">(a) <strong>UnityDefaultRenderer :</strong> This is the default way to display the results. In this renderer, each result shows the icon and the display name beneath it.</p>
<p align="justify"><a href="https://saravananthirumuruganathan.wordpress.com/wp-content/uploads/2011/08/defaultrenderer.png"><img title="Ubuntu Unity Lens results under UnityDefaultRenderer" src="https://saravananthirumuruganathan.wordpress.com/wp-content/uploads/2011/08/defaultrenderer.png?w=500&#038;h=281" alt="Ubuntu Unity Lens results under UnityDefaultRenderer" width="500" height="281" /> </a><br />
(b) <strong>UnityHorizontalTileRenderer :</strong> In this renderer, the icon and display name for each result is shown. In addition, it also displays the comment associated with the result.</p>
<p align="justify"><a href="https://saravananthirumuruganathan.wordpress.com/wp-content/uploads/2011/08/horizontaltilerenderer.png"><img title="Ubuntu Unity Lens results under UnityHorizontalTileRenderer" src="https://saravananthirumuruganathan.wordpress.com/wp-content/uploads/2011/08/horizontaltilerenderer.png?w=500&#038;h=281" alt="Ubuntu Unity Lens results under UnityHorizontalTileRenderer" width="500" height="281" /> </a><br />
(c) <strong>UnityEmptySearchRenderer : </strong>This renderer is used when the current search query returned no results. The result item construction is very similar to the results above. But the display name is displayed across the results pane.</p>
<p align="justify"><a href="https://saravananthirumuruganathan.wordpress.com/wp-content/uploads/2011/08/emptysearchrenderer.png"><img title="Ubuntu Unity Lens results under UnityEmptySearchRenderer" src="https://saravananthirumuruganathan.wordpress.com/wp-content/uploads/2011/08/emptysearchrenderer.png?w=500&#038;h=281" alt="Ubuntu Unity Lens results under UnityEmptySearchRenderer" width="500" height="281" /> </a><br />
(d) <strong>UnityShowcaseRenderer :</strong> I tried using it and the result was not much different from UnityDefaultRenderer. But according to the documentation, it should have a bigger icon.</p>
<p align="justify"><a href="https://saravananthirumuruganathan.wordpress.com/wp-content/uploads/2011/08/showcaserenderer.png"><img title="Ubuntu Unity Lens results under UnityShowcaseRenderer" src="https://saravananthirumuruganathan.wordpress.com/wp-content/uploads/2011/08/showcaserenderer.png?w=500&#038;h=281" alt="Ubuntu Unity Lens results under UnityShowcaseRenderer" width="500" height="281" /> </a><br />
(e) <strong>UnityEmptySectionRenderer :</strong> I did not try this as it not make sense in the application I developed. Supposedly, it is used to imply that the current section is empty. If my interpretation is correct, then this is used when the current section returned no results for query but a different section might produce results. UnityEmptySearchRenderer on the other hand is used to imply that the current search query itself (regardless of the section) provides zero results.<br />
(f) <strong>UnityFileInfoRenderer :</strong> Copying from the reference, this renderer is used in files place to show search results. Expects a valid URI, and will show extra information such as the parent folder and whatever data is in the comment column of the results.</p>
<h3 align="justify">Steps to write a Lens :</h3>
<p align="justify">There are three major steps in writing a Lens.<br />
(1) Writing a .place file<br />
(2) Writing a Lens daemon<br />
(3) If you want to distribute , Writing a service file</p>
<p align="justify">As an example, let us develop a Lens that does the following :<br />
(1) It has two sections &#8211; In &#8220;Movie Names&#8221; section, only the names of the movie are shown. In &#8220;Genre&#8221; section, all movies are clubbed according to their genre which act as a groups. So if movie X is in genres Y and Z, X will be displayed in both groups Y and Z.<br />
(2) Clicking on the result will open a browser and redirect to the IMDB page for the movie.<br />
(3) The lens accepts queries from two places &#8211; When entered directly in the IMDB Lens and also from the dash. When searched from Dash , it will display movie names as a separate group.</p>
<p align="justify">At the initial state, the lens will look like this :</p>
<p><a href="https://saravananthirumuruganathan.wordpress.com/wp-content/uploads/2011/08/lensinitialstate.png"><img title="Ubuntu Unity Lens Initial state" src="https://saravananthirumuruganathan.wordpress.com/wp-content/uploads/2011/08/lensinitialstate.png?w=500&#038;h=281" alt="Ubuntu Unity Lens Initial state" width="500" height="281" /> </a></p>
<p align="justify">For the sake of completeness, here is now it looks when you just search for Movie Names. <a href="https://saravananthirumuruganathan.wordpress.com/wp-content/uploads/2011/08/horizontaltilerenderer.png"><img title="Ubuntu Unity Lens Search for Movie Names" src="https://saravananthirumuruganathan.wordpress.com/wp-content/uploads/2011/08/horizontaltilerenderer.png?w=500&#038;h=281" alt="Ubuntu Unity Lens Search for Movie Names" width="500" height="281" /> </a></p>
<p align="justify">For the sake of completeness, here is now it looks when you just search for Genres. Basically, the results are grouped using the Genre. If a movie falls under multiple genres, it also falls into different groups. <a href="https://saravananthirumuruganathan.wordpress.com/wp-content/uploads/2011/08/genresection.png"><img title="Ubuntu Unity Lens Search for Movie Genre" src="https://saravananthirumuruganathan.wordpress.com/wp-content/uploads/2011/08/genresection.png?w=500&#038;h=281" alt="Ubuntu Unity Lens Search for Movie Genre" width="500" height="281" /> </a></p>
<p align="justify">Let us take a look at how to write the IMDB lens as per the major steps above.</p>
<h3 align="justify">Writing a Place File :</h3>
<p align="justify">As discussed above a place file contains the information that allows Unity to display the entries in the places bar. Information like which bus to use for communication, what shortcut to use etc are also involved. For the purpose of this tutorial, I will put up the place file I used for the IMDB lens. I will then explain the significance of each line. The file is named as<strong> imdbSearch.place</strong> and is placed in <strong>/usr/share/unity/places/</strong> .</p>
<pre class="brush: plain; title: ; notranslate">
[Place]
DBusName=net.launchpad.IMDBSearchLens
DBusObjectPath=/net/launchpad/imdbsearchlens

[Entry:IMDBSearch]
DBusObjectPath=/net/launchpad/imdbsearchlens/mainentry
Icon=/usr/share/app-install/icons/totem.png
Name=IMDB Search
Description=Search Movies using IMDB
SearchHint=Seach Movies using IMDB
Shortcut=i
ShowGlobal=true
ShowEntry=true

[Desktop Entry]
X-Ubuntu-Gettext-Domain=imdbSearch-place
</pre>
<p align="justify">As you can see, the file has three sections. Each section contains a set of key-value pairs.</p>
<p align="justify"><strong>Place Section :<br />
</strong>This is the simplest section and contains two key values &#8211; DBusName and DBusObjectPath. <strong>DBusName</strong> gives the name of the bus under which this entry can be found while <strong>DBusObjectPath</strong> provides the actual dbus object path. The names can be any valid DBus names.</p>
<p align="justify"><strong>Entry Section :<br />
</strong>A place file can contain one or more entries. Each entry must contain a section that corresponds to it. Each entry will occupy a distinct location in the places bar. If there are multiple entries, each of them must have a unique name and object path. In the daemon code, all those entries must be added individually.</p>
<p align="justify">The various keys are :<br />
<strong>DBusObjectPath</strong> : This is the DBus path used to communicate with this specific entry. Note that this path must be a child of mail DBusObjectPath. In our case, there is only one entry and hence we put it under the path &#8220;mainentry&#8221; of the original path.<br />
<strong>Icon</strong> : Link to the icon file<br />
<strong>Name</strong> : Name of the entry. When you hover over the entry in the places bar, this will be displayed.<br />
<strong>Description</strong> : A long description of the lens. I am not sure where this is used.<br />
<strong>SearchHint</strong> : This is the text that is displayed by default in the lens when the user selects it. In the image above, the search hint &#8220;Search Movies using IMDB&#8221;is displayed when the user selects the IMDB lens.<br />
<strong>Shortcut</strong> : This gives the key which is used to trigger the lens. Of course, you can always use the mouse to select it. If this key is specified,then pressing Super+shortcut operns the lens. For the IMDB lens, pressing Super+i opens it.<br />
<strong>ShowGlobal</strong> : This is an optional key and defaults to true. If it is set to false, then searches from the main dash are not sent to the lens. This seems to override the specification inside the lens daemon. ie If the place file specifies ShowGlobal as false and the daemon adds a listener to the event where user enters a query in the main dash, it is not invoked. Most of the time, I think it makes more sense for the lens to set this as false. For eg, it certainly does not make sense for IMDB lens to offer its service in the dash. Most of the time, when the user is searching in the dash, he is looking for some file or application. Polluting those results with IMDB results may not be wise. This is even more true if your lens takes a while to provide all the results.<br />
<strong>ShowEntry</strong> : This is an optional key and defaults to true. If it is set to false, then the entry will not be displayed in the places bar. If this is set to false, then the Shortcut key becomes useless as well. Pressing the shortcut key does not invoke the lens. But if the ShowGlobal is set to true, then the lens will still be searchable via the dash. For eg, if for some reason, I decide that IMDB lens must only provide results to queries from dash I will set ShowGlobal to true and ShowEntry to false.</p>
<h3 align="justify">Desktop Entry Section :</h3>
<p align="justify">This section is mostly optional. The most used key is that X-Ubuntu-Gettext-Domain. This key is used for internationalization purposes. If you want the lens should support internationalization, provide the appropriate entry name. If you are not familiar with internationalization in Ubuntu, there are two broad ways of achieving it : Either put all the translations statically in the file. Or put it in the appropriate .mo file which will then be put in some language-pack file. I included this line because Unity whines as &#8220;** Message: PlaceEntry Entry:IMDBSearch does not contain a translation gettext name: Key file does not have group &#8216;Desktop Entry'&#8221;.</p>
<h3 align="justify">Writing a Lens Daemon</h3>
<p align="justify">The next step is to write a daemon that communicates with Unity over dbus path, does the search and returns annotated search results so that Unity can render them meaningfully. The daemon can be written in any language that support GObject introspection. Most common languages include C, Vala and Python. Vey informally, GOject is a mechanism by which bindings for other languages can be done relatively easily. The actual process needs multiple components. Based on the reference [2], this means that you must verify if the following packages are installed in the system &#8211; gir1.2-unity-3.0 , gir1.2-dee-0.5 and gir1.2-dbusmenu-glib-0.4 . Of course, the actual version numbers may change in the future. If installing does not make it work, look at the reference for additional instructions [2].</p>
<p align="justify">I will use the IMDB Search lens as an example to explain writing a lens daemon in Python. The full source code is in [1]. I will use snippets of the code to make the appropriate points. The first step is of course importing the appropriate libraries. If you see other Python lens file, they used to have a hack that probes Dee before importing Unity. In my tests I found it unnecessary. If you get some error in importing Unity, then take a look at other sample files [3] and do accordingly.</p>
<pre class="brush: python; title: ; notranslate">
from gi.repository import GLib, GObject, Gio, Dee, Unity
</pre>
<p align="justify">The next step is to define the name of the bus that we will use to communicate with Unity. Note that this must exactly match the &#8220;DBusName&#8221; key in the place file.</p>
<pre class="brush: python; title: ; notranslate">
BUS_NAME = &quot;net.launchpad.IMDBSearchLens&quot;
</pre>
<p align="justify">The next step is to define some constants for the sections in your lens. If your lens contain only one section, feel free to ignore the initial lines. Else define section constants appropriately. The only place where you will use these constants is to figure out which section the lens currently is in. The section ids are integers and are ordered from 0. Note that the order given here is the *exact* order in which the sections must be added to our lens in Unity.</p>
<p align="justify">Similarly, we can have constants for groups. Instead of creating lot of constants, I created the group ids dynamically. First, I create a list of all IMDB genres and use a hash to map the name to an integer (group id). Also note that I also have constants for groups &#8211; GROUP_EMPTY, GROUP_MOVIE_NAMES_ONLY and GROUP_OTHER.</p>
<p align="justify">One thing to notice is that if different sections have non overlapping groups, all of them must be defined here. Then based on current section, use the appropriate group. As an example, GROUP_MOVIE_NAMES_ONLY is used only with SECTION_NAME_ONLY. But we define it alongside other groups. Also note the group for empty, GROUP_EMPTY. This will be used if the search query returns no results.  As with sections, the order of groups defined here must exactly match the order in which they are added to group model. If you have reasonably small number of sections and groups, you can avoid the elaborate setup of writing constants.</p>
<pre class="brush: python; title: ; notranslate">
SECTION_NAME_ONLY = 0
SECTION_GENRE_INFO = 1

allGenreGroupNames = [ &quot;Action&quot;, &quot;Adventure&quot;, &quot;Animation&quot;, &quot;Biography&quot;, &quot;Comedy&quot;, &quot;Crime&quot;, &quot;Documentary&quot;, &quot;Drama&quot;, &quot;Family&quot;,    &quot;Fantasy&quot;, &quot;Film-Noir&quot;, &quot;Game-Show&quot;, &quot;History&quot;, &quot;Horror&quot;, &quot;Music&quot;,  &quot;Musical&quot;, &quot;Mystery&quot;, &quot;News&quot;, &quot;Reality-TV&quot;, &quot;Romance&quot;, &quot;Sci-Fi&quot;, &quot;Sport&quot;, &quot;Talk-Show&quot;, &quot;Thriller&quot;, &quot;War&quot;, &quot;Western&quot;]

numGenreGroups = len(allGenreGroupNames)
GROUP_EMPTY = numGenreGroups
GROUP_MOVIE_NAMES_ONLY = numGenreGroups + 1
GROUP_OTHER = numGenreGroups + 2

groupNameTogroupId = {}
#We create a hash which allows to find the group name from genre.
for i in range(len(allGenreGroupNames)):
        groupName = allGenreGroupNames[i]
        groupID = i
        groupNameTogroupId[groupName] = groupID
</pre>
<p align="justify">The next step is to define the skeleton of the lens daemon. It is a simple Python class. In the constructor, you define the entire model corresponding to each entry in the Lens. If you place file contains n entries, you will be defining n different PlaceEntryInfo. In our case we have only one. Hence we create a variable that points to the corresponding DBusObjectPath. It is impertative that the path exactly match DBusObjectPath of the entry.</p>
<p align="justify">Each PlaceEntryInfo consists of different models : sections_model, groups_model and results_model.  If you want the lens to respond to searches in dash, you will also need to setup global_groups_model and global_results_model . The code below contains information about the schema of the different models. You can consider them as more or less a code that can be blindly copied. If you want additional information about things that you can tweak in PlaceEntryInfo, take a look <strong><a title="this url" href="https://wiki.ubuntu.com/Unity/Lenses#PlaceEntryInfo">this url</a></strong> .</p>
<p align="justify"><strong>Note on Property based access :</strong><br />
One important thing to notice is that all the access is done using properties. There are two methods to do that :<br />
(1) x.props.y<br />
(2) x.get_property(&#8220;y&#8221;)</p>
<p align="justify">Choosing one of them is mostly based on your coding style. Choose one use it consistently.</p>
<pre class="brush: python; title: ; notranslate">
class Daemon:
        def __init__ (self):
                self._entry = Unity.PlaceEntryInfo.new (&quot;/net/launchpad/imdbsearchlens/mainentry&quot;)

                #set_schema(&quot;s&quot;,&quot;s&quot;) corresponds to display name for section , the icon used to display
                sections_model = Dee.SharedModel.new (BUS_NAME + &quot;.SectionsModel&quot;);
                sections_model.set_schema (&quot;s&quot;, &quot;s&quot;);
                self._entry.props.sections_model = sections_model

                #set_schema(&quot;s&quot;,&quot;s&quot;) corresponds to renderer used to display group, display name for group , the icon used to display
                groups_model = Dee.SharedModel.new (BUS_NAME + &quot;.GroupsModel&quot;);
                groups_model.set_schema (&quot;s&quot;, &quot;s&quot;, &quot;s&quot;);
                self._entry.props.entry_renderer_info.props.groups_model = groups_model

                #Same as above
                global_groups_model = Dee.SharedModel.new (BUS_NAME + &quot;.GlobalGroupsModel&quot;);
                global_groups_model.set_schema (&quot;s&quot;, &quot;s&quot;, &quot;s&quot;);
                self._entry.props.global_renderer_info.props.groups_model = global_groups_model

                #set_schema(s,s,u,s,s,s) corresponds to URI, Icon name, Group id, MIME type, display name for entry, comment
                results_model = Dee.SharedModel.new (BUS_NAME + &quot;.ResultsModel&quot;);
                results_model.set_schema (&quot;s&quot;, &quot;s&quot;, &quot;u&quot;, &quot;s&quot;, &quot;s&quot;, &quot;s&quot;);
                self._entry.props.entry_renderer_info.props.results_model = results_model

                #Same as above
                global_results_model = Dee.SharedModel.new (BUS_NAME + &quot;.GlobalResultsModel&quot;);
                global_results_model.set_schema (&quot;s&quot;, &quot;s&quot;, &quot;u&quot;, &quot;s&quot;, &quot;s&quot;, &quot;s&quot;);
                self._entry.props.global_renderer_info.props.results_model = global_results_model
</pre>
<p align="justify">Once you have setup the models, you need to add listeners to events that you want to catch. The most important one is &#8220;notify::synchronized&#8221;. This called when Unity sets up your lens and wants to know the various groups and sections in your entry. In the code below, we add three different functions to that event. One gives the sections in the lens, other gives the groups and last gives the groups in dash.</p>
<p align="justify">Next we catch two events that are core to Lens &#8211; notify::active-search and notify::active-global-search . The first is triggered when the user searches something in the search textbox of the place while the second is triggered when user searches something in the dash. It is important to notice that the same search can trigger the events multiple times. By default, Unity does a decent job of batching calls providing search strings, but it is an important thing to consider if your lens does expensive operations. Take a look at the section of caching for additional details.</p>
<p align="justify">The event notify::active-section is triggered when the user changes the section of the lens by using the dropdown. You can then use your section constants to decide which section is currently selected.</p>
<p align="justify">notify::active is triggered when the user selects and leaves your place. Hence its an indirect way for your daemon to know if the lens is being visible to the user.</p>
<pre class="brush: python; title: ; notranslate">
                # Populate the sections and groups once we are in sync with Unity
                sections_model.connect (&quot;notify::synchronized&quot;, self._on_sections_synchronized)
                groups_model.connect (&quot;notify::synchronized&quot;, self._on_groups_synchronized)

                #Comment the next line if you do not want your lens to be searched in dash
                global_groups_model.connect (&quot;notify::synchronized&quot;, self._on_global_groups_synchronized)

                # Set up the signals we'll receive when Unity starts to talk to us

                # The 'active-search' property is changed when the users searches within this particular place
                self._entry.connect (&quot;notify::active-search&quot;, self._on_search_changed)

                # The 'active-global-search' property is changed when the users searches from the Dash aka Home Screen
                #       Every place can provide results for the search query.

                #Comment the next line if you do not want your lens to be searched in dash
                self._entry.connect (&quot;notify::active-global-search&quot;, self._on_global_search_changed)

                # Listen for changes to the section.
                self._entry.connect(&quot;notify::active-section&quot;, self._on_section_change)

                # Listen for changes to the status - Is our place active or hidden?
                self._entry.connect(&quot;notify::active&quot;, self._on_active_change)
</pre>
<p align="justify">Once you have set up the different models and functions to populate them, you need to add the entry to a PlaceController. If your lens has multiple entries, repeat the above process for each entry and once constructed call the add_entry of PlaceController to set them up. Note that the argument to PlaceController&#8217;s constructor is same as the DBusObjectPath for the Place section in your .place file.</p>
<pre class="brush: python; title: ; notranslate">
                self._ctrl = Unity.PlaceController.new (&quot;/net/launchpad/imdbsearchlens&quot;)
                self._ctrl.add_entry (self._entry)
                self._ctrl.export ()
</pre>
<p align="justify">Once we have setup all the models, the next step is to define the functions that are used.The first is to define the function that populates the entry&#8217;s section model. Note that we call append on the sections_model with two parameters : the name of the genre and an icon for it. You can also observe that when defined the &#8220;schema&#8221; of the sections_model, it accepted two strings. One other thing to note is the lack of use of any section constants &#8211; You must be careful to define the sections in the exact same order as the constants. In our case, it means that SECTION_NAME_ONLY followed by SECTION_GENRE_INFO.</p>
<pre class="brush: python; title: ; notranslate">
        def _on_sections_synchronized (self, sections_model, *args):
                # Column0: display name
                # Column1: GIcon in string format. Or you can pass entire path (or use GIcon).
                sections_model.clear ()
                sections_model.append (&quot;Movie Names&quot;, Gio.ThemedIcon.new (&quot;video&quot;).to_string())
                sections_model.append (&quot;Movie Genre&quot;, Gio.ThemedIcon.new (&quot;video&quot;).to_string())
</pre>
<p align="justify">The next step is to define a set of groups. Due to the nature of this lens, we reuse the list to form it. Notice the use of UnityEmptySearchRenderer for the GROUP_EMPTY. For this example, I have used UnityHorizontalTileRenderer. Your lens might need some other renderer &#8211; Take a look at the renderer section above for a discussion of the different renderers.</p>
<pre class="brush: python; title: ; notranslate">
                # Column0: group renderer, # Column1: display name, # Column2: GIcon in string format Or you can pass entire path (or use GIcon).
                groups_model.clear ()

                #Remember to add groups in the order you defined above (ie when defining constants)
                for groupName in allGenreGroupNames:
                        groups_model.append (&quot;UnityHorizontalTileRenderer&quot;, groupName, Gio.ThemedIcon.new (&quot;sound&quot;).to_string())
                #GROUP_EMPTY
                groups_model.append (&quot;UnityEmptySearchRenderer&quot;, &quot;No results found from IMDB&quot;, Gio.ThemedIcon.new (&quot;sound&quot;).to_string())
                #GROUP_MOVIE_NAMES_ONLY
                groups_model.append (&quot;UnityHorizontalTileRenderer&quot;, &quot;Movie Names&quot;, Gio.ThemedIcon.new (&quot;sound&quot;).to_string())
                #GROUP_OTHER
                groups_model.append (&quot;UnityHorizontalTileRenderer&quot;, &quot;Other&quot;, Gio.ThemedIcon.new (&quot;sound&quot;).to_string())
</pre>
<p align="justify">The next step is to define the function that is called when the search changes in the place search box. Notice how we use the entry object to get the current section, the current results, current search query and so on. Finally, we invoke search_finished function which calls search.finished() which signals to Unity that we are done processing the query.</p>
<pre class="brush: python; title: ; notranslate">
        def _on_search_changed (self, *args):
                entry = self._entry
                self.active_section = entry.get_property(&quot;active-section&quot;)
                search = self.get_search_string()
                results = self._entry.props.entry_renderer_info.props.results_model
                print &quot;Search changed to: '%s'&quot; % search
                self._update_results_model (search, results)
                #Signal completion of search
                self.search_finished()
</pre>
<p align="justify">There are other functions which we can detail. But mostly, they are very similar to code shown here. You can take a look at the python file linked below [1] for full glory.</p>
<p align="justify">So far all we have done is using some boilerplate code. From now on though, the code starts to differ based on the application. So I have not included the code that actually produces the results here. Take a look at the python file for details.</p>
<h3 align="justify">Testing Instructions</h3>
<p align="justify">Once you have written the python file , you want to test your baby. Here are the steps :</p>
<p align="justify">(1) Include the following code in your daemon. This code was literaly copied from Unity Sample Place [3] code.</p>
<pre class="brush: python; title: ; notranslate">
if __name__ == &quot;__main__&quot;:
        session_bus_connection = Gio.bus_get_sync (Gio.BusType.SESSION, None)
        session_bus = Gio.DBusProxy.new_sync (session_bus_connection, 0, None, 'org.freedesktop.DBus', '/org/freedesktop/DBus', 'org.freedesktop.DBus', None)
        result = session_bus.call_sync('RequestName', GLib.Variant (&quot;(su)&quot;, (BUS_NAME, 0x4)), 0, -1, None)

        # Unpack variant response with signature &quot;(u)&quot;. 1 means we got it.
        result = result.unpack()[0]

        if result != 1 :
                print &gt;&gt; sys.stderr, &quot;Failed to own name %s. Bailing out.&quot; % BUS_NAME
                raise SystemExit (1)

        daemon = Daemon()
        GObject.MainLoop().run()
</pre>
<p align="justify">(2) Copy your place file to<strong> /usr/share/unity/places/</strong> . If you have any custom icons, copy them to the location specified in your .place file.<br />
(3) Open up a new terminal and invoke your daemon file. Also remember to set the executable bit on.<br />
(4) Open up another terminal and type <strong>&#8220;setsid unity&#8221;</strong>. Using setsid is a very neat trick I learnt from the sample place file. Basically, it starts Unity in a new session. This forces it to read all the place files again. Also if you make any change to the daemon code, you can kill it and restart it. Unity will start communicating with your new daemon. Additionally, setsid will also ensure that Unity does not have controlling terminal but will still dump all debug information in the terminal it was opened.<br />
(5) Open the lens and type your query and watch the results appear !</p>
<h3 align="justify">Debugging Checklist</h3>
<p align="justify">If your lens is not working as expected, here are somethings to verify :</p>
<p align="justify">(1) Make sure that your place file is copied to /usr/share/unity/places .<br />
(2) Make sure that busname and object path match exactly in your place file and your daemon file.<br />
(3) Make sure that Python file has its executable bit on. This is important if you want to run it a service/daemon that runs automatically. More on that below.<br />
(4) If you are simply testing it, then is your Daemon file running ? If you are running it as a service, is your service file contain information that match your place and daemon file ?<br />
(5) Did you restart Unity  &#8211; preferably as &#8220;setsid unity&#8221;<br />
(6) Did you make any recent changes to place file ? If so make sure it is copied and restart Unity.<br />
(7) Check for any exception information in the terminal that you started your daemon. If its a service, enable logging and examing the log files.<br />
(8) Check if your lens is actually started. If you used &#8220;setsid unity&#8221; in a terminal, it would be logging the various steps it does. One sample invocation showing successful starting of lenses is this :</p>
<blockquote>
<p align="left">    ** (&lt;unknown&gt;:6114): DEBUG: PlaceEntry: Applications<br />
** (&lt;unknown&gt;:6114): DEBUG: PlaceEntry: Commands<br />
** (&lt;unknown&gt;:6114): DEBUG: PlaceEntry: Files &amp; Folders<br />
** (&lt;unknown&gt;:6114): DEBUG: PlaceEntry: IMDB Search<br />
** (&lt;unknown&gt;:6114): DEBUG: /com/canonical/unity/ applicationsplace<br />
** (&lt;unknown&gt;:6114): DEBUG: /com/canonical/unity/filesplace<br />
** (&lt;unknown&gt;:6114): DEBUG: /net/launchpad/imdbsearchlens</p>
</blockquote>
<p align="justify">(9) If your lens works but puts results in strange groups, make sure that order of groups in synchronize and the constants are same.<br />
(10) <strong>Common errors and fixes :</strong></p>
<blockquote>
<p align="justify"><strong></strong><br />
** Message: PlaceEntry Entry:IMDBSearch does not contain a translation gettext name: Key file does not have group &#8216;Desktop Entry&#8217; :</p>
</blockquote>
<p align="justify">This is not an error. If this annoys you add a [Desktop Entry] section. See above for details.</p>
<blockquote>
<p align="justify">** (&lt;unknown&gt;:6114): WARNING **: Unable to connect to PlaceEntryRemote net.launchpad.IMDBSearchLens: No name owner</p>
</blockquote>
<p align="justify">This is again not an error but indicates that either your daemon program is not running or is not able to own the bus specified in the .place file.</p>
<h3 align="justify">Installation Instructions</h3>
<p align="justify">Since I primarily wrote the IMDB lens for learning Unity, I do not know much about the actual installation process. Here are some generic tips :</p>
<p align="justify">(1) If you want a simple method of installation, use distutils. It accepts a list of files and the location where they must be copied. This is the cleanest way.<br />
(2) If you are going to install it and expect the lens to start automatically, then you should write a .service file that knows which bus start and which file to invoke to make that happen. The service file is usually put in &#8220;/usr/share/dbus-1/services/&#8221; . In this case, also make sure that the daemon file is actually executable !</p>
<pre class="brush: plain; title: ; notranslate">
[D-BUS Service]
Name=net.launchpad.IMDBSearchLens
Exec=/path/to/daemom/file
</pre>
<h3 align="justify">Various Tips On Writing Lenses</h3>
<p align="justify">(1) Most of the simple daemons use constants for sections and groups. If you want them to setup them up dynamically, take a look how it is done in the IMDB lens.<br />
(2) <strong>Avoiding repeated searches :</strong><br />
When the user enters a query in the lens, it is not necessary that the search function will be called only once. Remember that there is no concept of return key to indicate that query is complete. In fact, if the user presses the return key, it selects (and opens) the first result. The way Unity handles this scenario is by batching the calls. Lets say the user wants to search &#8220;blah blah2&#8221;. If the user continuously types the query with minimal interruption of the keyword, then the entire query will be sent to lens in one shot. On the other hand, if the user is a slow typer and enters &#8220;blah b&#8221; before waiting for a second or so, Unity will call the search function with partial query. When the user completes the query , the lens will be called with the entire query.</p>
<p align="justify">This behavior complicates your lens behavior, especially if the processing is time consuming. There are different techniques to avoid running calculations repeatedly (IMDB lens uses the first two techniques):<br />
(a) Have a cutoff : Your code might decide not to search if the length of string is less that say 4 characters.<br />
(b) Have a cache with timeout : You can use a local cache that stores your previous results . When you get a new query, check your cache first and return the results from it. For added benefit, have a cache timeout that removes elements that were added long time ago.<br />
(c) Memoize using functools : A very cool technique that is used in other lenses is to memoize the function call. This technique works if the function is reasonably simple &#8211; the key is the function argument and the value is the return value of the function. One simple example is shown below. In the example, we defined a decorator called memoize_infinitely and apply it to function foo. Foo accepts an input and returns an output. This decorator, automagically creates a cache for each function. When the function is called with some argument, it is first checked in the cache. If it is found, then it is returned without invoking the function. Else the function is called and the result is stored in the cache. This technique is widely used. For a specific example,take a look at the AskUbuntu lens [3] .</p>
<pre class="brush: python; title: ; notranslate">
import functools
def memoize_infinitely(function):
    cache = {}
    @functools.wraps(function)
    def wrapped(*args):
        if not args in cache:
            cache[args] = function(*args)
        return cache[args]
    return wrapped

@memoize_infinitely
def foo(input):
	return input + 1
</pre>
<p align="justify">(3) <strong>Handling long computations :</strong><br />
If your lens returns lot of results or the processing for each result entry takes a lot of time, then its a good idea to display the information as it arrives. The results model has a function called <strong>flush_revision_queue</strong> which periodically sends the data so far to Unity. For eg, in the case of IMDB lens , fetching the genre information is expensive. So, the code flushes the results every time five movies were processed. This allows the user to see some results without waiting for eternity to get complete results.</p>
<h3 align="justify">Links</h3>
<p align="justify">    [1] The code for IMDB lens which I have tried to comment as much as possible is <a href="https://github.com/saravanan-thirumuruganathan/unity-lens-python-imdb"><strong>my github page</strong></a>. For convenience, I have also given the individual files : <strong><a href="https://github.com/saravanan-thirumuruganathan/unity-lens-python-imdb/blob/master/README">Readme</a></strong>, <a href="https://github.com/saravanan-thirumuruganathan/unity-lens-python-imdb/blob/master/imdb-search.py"><strong>imdb-search.py</strong></a>, <a href="https://github.com/saravanan-thirumuruganathan/unity-lens-python-imdb/blob/master/imdbSearch.place"><strong>imdbSearch.place</strong></a> .</p>
<p align="justify">    [2] <a href="http://www.grillbar.org/wordpress/?p=544"><strong>Unity Places – now with 100% More Python</strong></a> : This blog post contains a brief discussion on developing Unity lenses in Python. It has some info on installation and links to a sample lens in Python. I would advise you to start with the <a href="http://bazaar.launchpad.net/~unity-team/unity-lens-sample/unity-place-python/files"><strong>sample lens code</strong></a> given in this blog post before making drastic changes. This way you can make sure that the initial lens setup is fine and the issue is really in your code <img src="https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /><br />
[3] <strong>Link to some Good Unity Python Lens codes :</strong> As of now there are few other good Unity lenses written in Python. Here are some of  my favorites – <a href="http://bazaar.launchpad.net/~stefano-palazzo/askubuntu-lens/trunk/files"><strong>AskUbuntu</strong></a> lens, <a href="http://bazaar.launchpad.net/~ajmitch/unity-lens-launchpad/trunk/files"><strong>Launchpad</strong></a> lens, <a href="http://bazaar.launchpad.net/~alexandr-domrachev/unity-evernote-lens/unity-evernote-lens/files"><strong>Evernote</strong></a> lens, <a href="http://bazaar.launchpad.net/~davidc3/unity-books-lens/trunk/files"><strong>Book-Search</strong></a> lens, <a href="http://bazaar.launchpad.net/~markjtully/unity-music-lens/trunk/files"><strong>Music search</strong></a> lens , <a href="http://xn--sahlstrm-t4a.eu/unity-lens-spotify"><strong>Spotify</strong></a> lens . I have tried to incorporate all cool features in IMDB Search lens , so that it will become a one stop place for most important Lens features <img src="https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p align="justify">    [4] <a href="https://wiki.ubuntu.com/Unity/Lenses"><strong>Ubuntu Unity Architecture</strong></a> : This contains some language independent discussion of Unity architecture and additional information of the data structure it uses.<br />
[4] <a href="http://imdbpy.sourceforge.net/"><strong>IMDbPy</strong></a><strong> :</strong> The IMDB search lens was developed using the excellent IMDbPy library. It has very convenient API that allows development of IMDB based applications a snap.</p>
<p align="justify">  [5] <strong>Ubuntu Unity Lens Python API Links</strong> : Here are some links for documentation that I found  &#8211; <a href="http://developer.ubuntu.com/api/ubuntu-11.04/GIR/python/Dee-0.5.html"><strong>Dee API</strong></a>, <a href="http://developer.ubuntu.com/api/ubuntu-11.04/GIR/python/Unity-3.0.html"><strong>Unity API</strong></a> . The document for Unity also encompasses the documentation for Places.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://saravananthirumuruganathan.wordpress.com/2011/08/05/tutorial-on-writing-ubuntu-lensesplaces-in-python/feed/</wfw:commentRss>
			<slash:comments>17</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">605</post-id>
		<media:content url="https://2.gravatar.com/avatar/e2957c9d69690d3fef33fa2bb570d139abce0df84a663fcc30c2ffc3a77b2583?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">saravananthirumuruganathan</media:title>
		</media:content>

		<media:content url="https://saravananthirumuruganathan.wordpress.com/wp-content/uploads/2011/08/defaultrenderer.png" medium="image">
			<media:title type="html">Ubuntu Unity Lens results under UnityDefaultRenderer</media:title>
		</media:content>

		<media:content url="https://saravananthirumuruganathan.wordpress.com/wp-content/uploads/2011/08/horizontaltilerenderer.png" medium="image">
			<media:title type="html">Ubuntu Unity Lens results under UnityHorizontalTileRenderer</media:title>
		</media:content>

		<media:content url="https://saravananthirumuruganathan.wordpress.com/wp-content/uploads/2011/08/emptysearchrenderer.png" medium="image">
			<media:title type="html">Ubuntu Unity Lens results under UnityEmptySearchRenderer</media:title>
		</media:content>

		<media:content url="https://saravananthirumuruganathan.wordpress.com/wp-content/uploads/2011/08/showcaserenderer.png" medium="image">
			<media:title type="html">Ubuntu Unity Lens results under UnityShowcaseRenderer</media:title>
		</media:content>

		<media:content url="https://saravananthirumuruganathan.wordpress.com/wp-content/uploads/2011/08/lensinitialstate.png" medium="image">
			<media:title type="html">Ubuntu Unity Lens Initial state</media:title>
		</media:content>

		<media:content url="https://saravananthirumuruganathan.wordpress.com/wp-content/uploads/2011/08/horizontaltilerenderer.png" medium="image">
			<media:title type="html">Ubuntu Unity Lens Search for Movie Names</media:title>
		</media:content>

		<media:content url="https://saravananthirumuruganathan.wordpress.com/wp-content/uploads/2011/08/genresection.png" medium="image">
			<media:title type="html">Ubuntu Unity Lens Search for Movie Genre</media:title>
		</media:content>
	</item>
		<item>
		<title>New Version of Chrome Nanny Released</title>
		<link>https://saravananthirumuruganathan.wordpress.com/2011/07/24/new-version-of-chrome-nanny-released-3/</link>
					<comments>https://saravananthirumuruganathan.wordpress.com/2011/07/24/new-version-of-chrome-nanny-released-3/#comments</comments>
		
		<dc:creator><![CDATA[Saravanan Thirumuruganathan]]></dc:creator>
		<pubDate>Mon, 25 Jul 2011 01:47:56 +0000</pubDate>
				<category><![CDATA[Google Chrome]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[chrome]]></category>
		<category><![CDATA[chrome nanny]]></category>
		<category><![CDATA[google]]></category>
		<guid isPermaLink="false">http://saravananthirumuruganathan.wordpress.com/2011/07/24/new-version-of-chrome-nanny-released-3/</guid>

					<description><![CDATA[I released a new version of Chrome Nanny that incorporated few requested features. They are: &#160; 1. Validate regular expressions when they are entered in blockset. 2. More robust error handling inside the extension. 3. Always allow access to Chrome extension links even if you block *.* 4. Disallow *.* from White list. 5. Do [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>I released a new version of Chrome Nanny that incorporated few requested features. They are:</p>
<p>&#160;</p>
<p>1. Validate regular expressions when they are entered in blockset.</p>
<p>2. More robust error handling inside the extension.</p>
<p>3. Always allow access to Chrome extension links even if you block *.*</p>
<p>4. Disallow *.* from White list.</p>
<p>5. Do not strip www/http from the blocksite. So a block url of <a href="http://www.ft.com">www.ft.com</a> will not block <a href="http://www.microsoft.com">www.microsoft.com</a> unless the url is ft.com. </p>
<p>6. Multiple internal changes that will allow implementation of other Chrome Nanny features faster.</p>
<p>&#160;</p>
<p>Let me know how this version works !</p>
]]></content:encoded>
					
					<wfw:commentRss>https://saravananthirumuruganathan.wordpress.com/2011/07/24/new-version-of-chrome-nanny-released-3/feed/</wfw:commentRss>
			<slash:comments>66</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">595</post-id>
		<media:content url="https://2.gravatar.com/avatar/e2957c9d69690d3fef33fa2bb570d139abce0df84a663fcc30c2ffc3a77b2583?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">saravananthirumuruganathan</media:title>
		</media:content>
	</item>
		<item>
		<title>Detailed Tutorial on Markov and Chebyshev Inequalities</title>
		<link>https://saravananthirumuruganathan.wordpress.com/2011/07/02/detailed-tutorial-on-markov-and-chebyshev-inequalities/</link>
					<comments>https://saravananthirumuruganathan.wordpress.com/2011/07/02/detailed-tutorial-on-markov-and-chebyshev-inequalities/#comments</comments>
		
		<dc:creator><![CDATA[Saravanan Thirumuruganathan]]></dc:creator>
		<pubDate>Sun, 03 Jul 2011 04:25:49 +0000</pubDate>
				<category><![CDATA[math]]></category>
		<category><![CDATA[chebyshev]]></category>
		<category><![CDATA[inequalities]]></category>
		<category><![CDATA[markov]]></category>
		<category><![CDATA[probability]]></category>
		<guid isPermaLink="false">http://saravananthirumuruganathan.wordpress.com/2011/07/02/detailed-tutorial-on-markov-and-chebyshev-inequalities/</guid>

					<description><![CDATA[In this post, I plan to discuss about two very simple inequalities &#8211; Markov and Chebyshev. These are topics that are covered in any elementary probability course. In this post, I plan to give some intuitive explanation about them and also try to show them in different perspectives. Also, the following discussion is closer to [&#8230;]]]></description>
										<content:encoded><![CDATA[<p align="justify">In this post, I plan to discuss about two very simple inequalities &#8211; Markov and Chebyshev. These are topics that are covered in any elementary probability course. In this post, I plan to give some intuitive explanation about them and also try to show them in different perspectives. Also, the following discussion is closer to discrete random variables even though most of them can be extended to continuous ones.</p>
<h3 align="justify">Inequalities from an Adversarial Perspective</h3>
<p align="justify">One interesting way of looking at the inequalities is from an adversarial perspective. The adversary has given you some limited information and you are expected to come up with some bound on the probability of an event. For eg, in the case of Markov inequality, all you know is that the random variable is non negative and its (finite) expected value. Based on this information, Markov inequality allows you to provide some bound on the tail inequalities. Similarly, in the case of Chebyshev inequality, you know that the random variable has a finite expected value and variance. Armed with this information Chebyshev inequality allows you to provide some bound on the tail inequalities. The most fascinating this about these inequalities is that you do not have to know the probabilistic mass function(pmf). For any arbitrary pmf satisfying some mild conditions, Markov and Chebyshev inequalities allow you to make intelligent guesses about the tail probability.</p>
<h3 align="justify">A Tail Inequality perspective</h3>
<p align="justify">Another way of looking at these inequalities is this. Supposed we do not know anything about the pmf of a random variable and we are forced to make some prediction about the value it takes. If the expected value is known, a reasonable strategy is to use it. But then the actual value might deviate from our prediction. Markov and Chebyshev inequalities are very useful tools that allow us to estimate how likely or unlikely that the actual value varies from our prediction. For eg, we can use Markov inequality to bound the probability that the actual varies by some multiple of the expected value from the mean. Similarly, using Chebyshev we can bound the probability that the difference from mean is some multiple of its standard deviation.</p>
<p align="justify">One thing to notice is that you really do not need the pmf of the random variable to bound the probability of the deviations. Both these inequalities allow you to make deterministic statements of probabilistic bounds without knowing much about the pmf.</p>
<h3 align="justify">Markov Inequality</h3>
<p align="justify">Let us first take a look at the Markov Inequality. Even though the statement looks very simple, clever application of the inequality is at the heart of more powerful inequalities like Chebyshev or Chernoff. Initially, we will see the simplest version of the inequality and then we will discuss the more general version. The basic Markov inequality states that given a random variable X that can only take non negative values, then</p>
<p align="center">    <img src="https://s0.wp.com/latex.php?latex=Pr%28X+%5Cgeq+k+E%5BX%5D%29+%5Cleq+%5Cfrac%7B1%7D%7Bk%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=Pr%28X+%5Cgeq+k+E%5BX%5D%29+%5Cleq+%5Cfrac%7B1%7D%7Bk%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=Pr%28X+%5Cgeq+k+E%5BX%5D%29+%5Cleq+%5Cfrac%7B1%7D%7Bk%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="Pr(X &#92;geq k E[X]) &#92;leq &#92;frac{1}{k}" class="latex" /></p>
<p align="justify">There are some basic things to note here. First the term P(X &gt;= k E(X)) estimates the probability that the random variable will take the value that exceeds k times the expected value. The term P(X &gt;= E(X)) is related to the cumulative density function as 1 &#8211; P(X &lt; E(X)). Since the variable is non negative, this estimates the deviation on one side of the error.</p>
<h3 align="justify">Intuitive Explanation of Markov Inequality</h3>
<p align="justify">Intuitively, what this means is that , given a non negative random variable and its expected value E(X)<br />
(1) The probability that X takes a value that is greater than twice the expected value is atmost half. In other words, if you consider the pmf curve, the area under the curve for values that are beyond 2*E(X) is atmost half.<br />
(2) The probability that X takes a value that is greater than thrice the expected value is atmost one third.<br />
and so on.</p>
<p align="justify">Let us see why that makes sense. Let X be a random variable corresponding to the scores of 100 students in an exam. The variable is clearly non negative as the lowest score is 0. Tentatively lets assume the highest value is 100 (even though we will not need it). Let us see how we can derive the bounds given by Markov inequality in this scenario. Let us also assume that the average score is 20 (must be a lousy class!). By definition, we know that the combined score of all students is 2000 (20*100).</p>
<p align="justify">Let us take the first claim &#8211; The probability that X takes a value that is greater than twice the expected value is atmost half. In this example, it means the fraction of students who have score greater than 40 (2*20) is atmost 0.5. In other words atmost 50 students could have scored 40 or more. It is very clear that it must be the case. If 50 students got <strong>exactly</strong> 40 and the remaining students all got 0, then the average of the whole class is 20. Now , if even one additional student got a score greater than 40, then the total score of 100 students become 2040 and the average becomes 20.4 which is a contradiction to our original information. Note that the scores of other students that we assumed to be 0 is an over simplification and we can do without that. For eg, we can argue that if 50 students got 40 then the total score is <strong>atleast </strong>2000 and hence the mean is <strong>atleast</strong> 20.</p>
<p align="justify">We can also see how the second claim is true. The probability that X takes a value that is greater than thrice the expected value is atmost one third. If 33.3 students got 60 and others got 0 , then we get the total score as around 2000 and the average remains the same. Similarly, regardless of the scores of other 66.6 students, we know that the mean is atleast 20 now.</p>
<p align="justify">This also must have made clear why the variable must be non negative. If some of the values are negative, then we cannot claim that mean is atleast some constant C. The values that do not exceed the threshold may well be negative and hence can pull the mean below the estimated value.</p>
<p align="justify">Let us look at it from the other perspective : Let p be the fraction of students who have a score of <strong>atleast</strong> a . Then it is very clear to us that the mean is atleast <strong>a*p</strong>. What Markov inequality does is to turn this around. It says, if the mean is <strong>a*p</strong>  then the fraction of students with a score greater than <strong>a</strong> is atmost <strong>p</strong>. That is, <em>we know the mean here and hence use the threshold to estimate the fraction</em> .</p>
<h3 align="justify">Generalized Markov Inequality</h3>
<p align="justify">The probability that the random variable takes a value thats greater than k*E(X) is at most 1/k. The fraction 1/k act as some kind of a limit. Taking this further, you can observe that given an arbitrary constant a, the probability that the random variable X takes a value &gt;= a ie P(X &gt;= a) is atmost 1/a times the expected value. This gives the general version of Markov inequality.</p>
<p align="center">    <img src="https://s0.wp.com/latex.php?latex=Pr%28X+%5Cgeq+a%29+%5Cleq+%5Cfrac%7B1%7D%7Ba%7D+E%5BX%5D+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=Pr%28X+%5Cgeq+a%29+%5Cleq+%5Cfrac%7B1%7D%7Ba%7D+E%5BX%5D+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=Pr%28X+%5Cgeq+a%29+%5Cleq+%5Cfrac%7B1%7D%7Ba%7D+E%5BX%5D+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="Pr(X &#92;geq a) &#92;leq &#92;frac{1}{a} E[X] " class="latex" /></p>
<p align="justify">In the equation above, I seperated the fraction 1/a because that is the only varying part. We will later see that for Chebyshev we get a similar fraction. The proof of this inequality is straightforward. There are multiple proofs even though we will use the follow proof as it allows us to show Markov inequality graphically.This proof is partly taken from Mitzenmacher and Upfal&#8217;s exceptional book on Randomized Algorithms.</p>
<p align="justify">Consider a constant a &gt;= 0. Then define an indicator random variable I which takes value of 1 is X &gt;=a . ie</p>
<p align="center">    <img src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+I+%3D+%5Cbegin%7Bcases%7D+1%2C+%26+%5Cmbox%7Bif+%7D+%5Cmbox%7B+X%7D+%5Cgeq+%5Cmbox%7Ba%7D+%5C%5C+0%2C+%26+%5Cmbox%7Botherwise+%7D+%5Cend%7Bcases%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+I+%3D+%5Cbegin%7Bcases%7D+1%2C+%26+%5Cmbox%7Bif+%7D+%5Cmbox%7B+X%7D+%5Cgeq+%5Cmbox%7Ba%7D+%5C%5C+0%2C+%26+%5Cmbox%7Botherwise+%7D+%5Cend%7Bcases%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+I+%3D+%5Cbegin%7Bcases%7D+1%2C+%26+%5Cmbox%7Bif+%7D+%5Cmbox%7B+X%7D+%5Cgeq+%5Cmbox%7Ba%7D+%5C%5C+0%2C+%26+%5Cmbox%7Botherwise+%7D+%5Cend%7Bcases%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;displaystyle I = &#92;begin{cases} 1, &amp; &#92;mbox{if } &#92;mbox{ X} &#92;geq &#92;mbox{a} &#92;&#92; 0, &amp; &#92;mbox{otherwise } &#92;end{cases}" class="latex" /></p>
<p align="justify">  Now we make a clever observation. We know that X is non negative. ie X &gt;= 0. This means that the fraction X/a is atleast 0 and atmost can be infinty. Also, if X &lt; a, then X/a &lt; 1. When X &gt; a, X/a &gt; 1. Using these facts,</p>
<p align="center">     <img src="https://s0.wp.com/latex.php?latex=I+%5Cleq+%5Cfrac%7BX%7D%7Ba%7D+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=I+%5Cleq+%5Cfrac%7BX%7D%7Ba%7D+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=I+%5Cleq+%5Cfrac%7BX%7D%7Ba%7D+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="I &#92;leq &#92;frac{X}{a} " class="latex" /></p>
<p align="justify">If we take expectation on both sides, we get</p>
<p align="center">     <img src="https://s0.wp.com/latex.php?latex=E%5BI%5D+%5Cleq+%5Cfrac%7B1%7D%7Ba%7D+E%5BX%5D+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=E%5BI%5D+%5Cleq+%5Cfrac%7B1%7D%7Ba%7D+E%5BX%5D+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=E%5BI%5D+%5Cleq+%5Cfrac%7B1%7D%7Ba%7D+E%5BX%5D+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="E[I] &#92;leq &#92;frac{1}{a} E[X] " class="latex" /></p>
<p align="justify">But we also know that the expectation of indicator random variable is also the probability that it takes the value 1. This means E[I] = Pr(X&gt;=a). Putting it all together, we get the Markov inequality.</p>
<p align="center">    <img src="https://s0.wp.com/latex.php?latex=Pr%28X+%5Cgeq+a%29+%5Cleq+%5Cfrac%7B1%7D%7Ba%7D+E%5BX%5D+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=Pr%28X+%5Cgeq+a%29+%5Cleq+%5Cfrac%7B1%7D%7Ba%7D+E%5BX%5D+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=Pr%28X+%5Cgeq+a%29+%5Cleq+%5Cfrac%7B1%7D%7Ba%7D+E%5BX%5D+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="Pr(X &#92;geq a) &#92;leq &#92;frac{1}{a} E[X] " class="latex" /></p>
<h3 align="justify">Even more generalized Markov Inequality</h3>
<p align="justify">Sometimes, it might happen that the random variable is not non-negative. In cases like this, a clever hack helps. Design a function f(x) such that f(x) is non negative. Then we can apply Markov inequality on the modified random variable f(X). The Markov inequality for this special case is :</p>
<p align="center"><img src="https://s0.wp.com/latex.php?latex=Pr%28f%28X%29+%5Cgeq+a%29+%5Cleq+%5Cfrac%7B1%7D%7Ba%7D+E%5Bf%28X%29%5D+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=Pr%28f%28X%29+%5Cgeq+a%29+%5Cleq+%5Cfrac%7B1%7D%7Ba%7D+E%5Bf%28X%29%5D+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=Pr%28f%28X%29+%5Cgeq+a%29+%5Cleq+%5Cfrac%7B1%7D%7Ba%7D+E%5Bf%28X%29%5D+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="Pr(f(X) &#92;geq a) &#92;leq &#92;frac{1}{a} E[f(X)] " class="latex" /></p>
<p align="justify">This is a very powerful technique. Careful selection of f(X) allows you to derive more powerful bounds.<br />
(1) One of the simplest examples is f(X) = |X| which guarantees f(X) to be non negative.<br />
(2) Later we will show that Chebyshev inequality is nothing but Markov inequality that uses <img src="https://s0.wp.com/latex.php?latex=f%28X%29+%3D+%7CX-E%28X%29%7C%5E2+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=f%28X%29+%3D+%7CX-E%28X%29%7C%5E2+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=f%28X%29+%3D+%7CX-E%28X%29%7C%5E2+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="f(X) = |X-E(X)|^2 " class="latex" /><br />
(3) Under some additional constraints, Chernoff inequality uses <img src="https://s0.wp.com/latex.php?latex=f%28X%29+%3D+e%5E%7BtX%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=f%28X%29+%3D+e%5E%7BtX%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=f%28X%29+%3D+e%5E%7BtX%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="f(X) = e^{tX}" class="latex" /> .</p>
<h3 align="justify">Simple Examples</h3>
<p align="justify">Let us consider a simple example where it provides a decent bound and one where it does not. A typical example where Markov inequality works well is when the expected value is small but the threshold to test is very large.</p>
<p align="justify"><strong>Example 1: </strong><br />
Consider a coin that comes up with head with probability 0.2 . Let us toss it n times. Now we can use Markov inequality to bound the probability that we got atleast 80% of heads.</p>
<p align="justify">Let X be the random variable indicating the number of heads we got in n tosses. Clearly, X is non negative. Using linearity of expectation, we know that E[X] is 0.2n.We want to bound the probability P(X &gt;= 0.8n). Using Markov inequality , we get</p>
<p align="center">    <img src="https://s0.wp.com/latex.php?latex=P%28X+%5Cgeq+0.8n%29+%5Cleq+%5Cfrac%7B0.2n%7D%7B0.8n%7D+%3D+0.25&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=P%28X+%5Cgeq+0.8n%29+%5Cleq+%5Cfrac%7B0.2n%7D%7B0.8n%7D+%3D+0.25&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=P%28X+%5Cgeq+0.8n%29+%5Cleq+%5Cfrac%7B0.2n%7D%7B0.8n%7D+%3D+0.25&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="P(X &#92;geq 0.8n) &#92;leq &#92;frac{0.2n}{0.8n} = 0.25" class="latex" /></p>
<p align="justify">Of course we can estimate a finer value using the Binomial distribution, but the core idea here is that we do not need to know it !</p>
<p align="justify"><strong>Example 2:<br />
</strong>For an example where Markov inequality gives a bad result, let us the example of a dice. Let X be the face that shows up when we toss it. We know that E[X] is 7/2 = 3.5. Now lets say we want to find the probability that X &gt;= 5. By Markov inequality,</p>
<p align="center"><img src="https://s0.wp.com/latex.php?latex=P%28X+%5Cgeq+5%29+%5Cleq+%5Cfrac%7B3.5%7D%7B5%7D+%3D+0.7&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=P%28X+%5Cgeq+5%29+%5Cleq+%5Cfrac%7B3.5%7D%7B5%7D+%3D+0.7&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=P%28X+%5Cgeq+5%29+%5Cleq+%5Cfrac%7B3.5%7D%7B5%7D+%3D+0.7&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="P(X &#92;geq 5) &#92;leq &#92;frac{3.5}{5} = 0.7" class="latex" /></p>
<p align="justify">The actual answer of course is 2/6 and the answer is quite off. This becomes even more bizarre , for example, if we find P(X &gt;= 3) . By Markov inequality,</p>
<p align="center"><img src="https://s0.wp.com/latex.php?latex=P%28X+%5Cgeq+3%29+%5Cleq+%5Cfrac%7B3.5%7D%7B3%7D+%3D+%5Cfrac%7B7%7D%7B6%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=P%28X+%5Cgeq+3%29+%5Cleq+%5Cfrac%7B3.5%7D%7B3%7D+%3D+%5Cfrac%7B7%7D%7B6%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=P%28X+%5Cgeq+3%29+%5Cleq+%5Cfrac%7B3.5%7D%7B3%7D+%3D+%5Cfrac%7B7%7D%7B6%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="P(X &#92;geq 3) &#92;leq &#92;frac{3.5}{3} = &#92;frac{7}{6}" class="latex" /></p>
<p align="justify">The upper bound is greater than 1 ! Of course using axioms of probability, we can set it to 1 while the actual probability is closer to 0.66 . You can play around with the coin example or the score example to find cases where Markov inequality provides really weak results.</p>
<h3 align="justify">Tightness of Markov</h3>
<p align="justify">The last example might have made you think that the Markov inequality is useless. On the contrary, it provided a weak bound because the amount of information we provided to it is limited. All we provided to it were that the variable is non negative and that the expected value is known and finite. In this section, we will show that it is indeed tight &#8211; that is Markov inequality is already doing as much as it can.</p>
<p align="justify">From the previous example, we can see an example where Markov inequality is tight. If the mean of 100 students is 20 and if 50 students got a score of exactly 0, then Markov implies that atmost 50 students can get a score of atleast 40.</p>
<p align="justify">Note : I am not 100% sure if the following argument is fully valid &#8211; But atleast it seems to me <img src="https://s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p align="justify">Consider a random variable X such that</p>
<p align="center">    <img src="https://s0.wp.com/latex.php?latex=X+%3D+%5Cdisplaystyle+%5Cbegin%7Bcases%7D+k+%26+%5Cmbox%7Bwith+probability+%7D+%5Cfrac%7B1%7D%7Bk%7D+%5C%5C+0+%26+%5Cmbox%7Belse%7D+%5Cend%7Bcases%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=X+%3D+%5Cdisplaystyle+%5Cbegin%7Bcases%7D+k+%26+%5Cmbox%7Bwith+probability+%7D+%5Cfrac%7B1%7D%7Bk%7D+%5C%5C+0+%26+%5Cmbox%7Belse%7D+%5Cend%7Bcases%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=X+%3D+%5Cdisplaystyle+%5Cbegin%7Bcases%7D+k+%26+%5Cmbox%7Bwith+probability+%7D+%5Cfrac%7B1%7D%7Bk%7D+%5C%5C+0+%26+%5Cmbox%7Belse%7D+%5Cend%7Bcases%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="X = &#92;displaystyle &#92;begin{cases} k &amp; &#92;mbox{with probability } &#92;frac{1}{k} &#92;&#92; 0 &amp; &#92;mbox{else} &#92;end{cases}" class="latex" /></p>
<p align="justify">We can estimate its expected value as</p>
<p align="center">    <img src="https://s0.wp.com/latex.php?latex=E%5BX%5D+%3D+%5Cfrac%7B1%7D%7Bk%7D+%5Ctimes+k+%2B+%5Cfrac%7Bk-1%7D%7Bk%7D+%5Ctimes+0+%3D+1&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=E%5BX%5D+%3D+%5Cfrac%7B1%7D%7Bk%7D+%5Ctimes+k+%2B+%5Cfrac%7Bk-1%7D%7Bk%7D+%5Ctimes+0+%3D+1&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=E%5BX%5D+%3D+%5Cfrac%7B1%7D%7Bk%7D+%5Ctimes+k+%2B+%5Cfrac%7Bk-1%7D%7Bk%7D+%5Ctimes+0+%3D+1&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="E[X] = &#92;frac{1}{k} &#92;times k + &#92;frac{k-1}{k} &#92;times 0 = 1" class="latex" /></p>
<p>We can see that , <img src="https://s0.wp.com/latex.php?latex=Pr%28X+%5Cgeq+k+E%5BX%5D%29+%3D+Pr%28X+%5Cgeq+k%29+%3D+%5Cfrac%7B1%7D%7Bk%7D+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=Pr%28X+%5Cgeq+k+E%5BX%5D%29+%3D+Pr%28X+%5Cgeq+k%29+%3D+%5Cfrac%7B1%7D%7Bk%7D+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=Pr%28X+%5Cgeq+k+E%5BX%5D%29+%3D+Pr%28X+%5Cgeq+k%29+%3D+%5Cfrac%7B1%7D%7Bk%7D+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="Pr(X &#92;geq k E[X]) = Pr(X &#92;geq k) = &#92;frac{1}{k} " class="latex" /></p>
<p align="justify">This implies that the bound is actually tight ! Of course one of the reasons why it was tight is that the other value is 0 and the value of the random variable is exactly k. This is consistent with the score example we saw above.</p>
<h3 align="justify">Chebyshev Inequality</h3>
<p align="justify">Chebyshev inequality is another powerful tool that we can use. In this inequality, we remove the restriction that the random variable has to be non negative. As a price, we now need to know additional information about the variable &#8211; (finite) expected value and (finite) variance. In contrast to Markov, Chebyshev allows you to estimate the deviation of the random variable from its mean. A common use of it estimates the probability of the deviation from its mean in terms of its standard deviation.</p>
<p align="justify">Similar to Markov inequality, we can state two variants of Chebyshev. Let us first take a look at the simplest version. Given a random variable X and its finite mean and variance, we can bound the deviation as</p>
<p align="center">    <img src="https://s0.wp.com/latex.php?latex=P%28%7CX-E%5BX%5D%7C+%5Cgeq+k+%5Csigma+%29+%5Cleq+%5Cfrac%7B1%7D%7Bk%5E2%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=P%28%7CX-E%5BX%5D%7C+%5Cgeq+k+%5Csigma+%29+%5Cleq+%5Cfrac%7B1%7D%7Bk%5E2%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=P%28%7CX-E%5BX%5D%7C+%5Cgeq+k+%5Csigma+%29+%5Cleq+%5Cfrac%7B1%7D%7Bk%5E2%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="P(|X-E[X]| &#92;geq k &#92;sigma ) &#92;leq &#92;frac{1}{k^2}" class="latex" /></p>
<p align="justify">  There are few interesting things to observe here :<br />
(1) In contrast to Markov inequality, Chebyshev inequality allows you to bound the deviation on both sides of the mean.<br />
(2) The length of the deviation is <img src="https://s0.wp.com/latex.php?latex=k+%5Csigma+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=k+%5Csigma+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=k+%5Csigma+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="k &#92;sigma " class="latex" /> on both sides which is usually (but not always) tighter than the bound k E[X]. Similarly, the fraction 1/k^2 is much more tighter than 1/k that we got from Markov inequality.<br />
(3) Intuitively, if the variance of X is small, then Chebyshev inequality tells us that X is close to its expected value with high probability.<br />
(4) Using Chebyshev inequality, we can claim that atmost one fourth of the values that X can take is beyond 2 standard deviation of the mean.</p>
<h3 align="justify">Generalized Chebyshev Inequality</h3>
<p align="justify">A more general Chebyshev inequality bounds the deviation from mean to any constant a . Given a positive constant a ,</p>
<p align="center">    <img src="https://s0.wp.com/latex.php?latex=Pr%28%7CX-E%5BX%5D%7C+%5Cgeq+a%29+%5Cleq+%5Cfrac%7B1%7D%7Ba%5E2%7D%5C%3BVar%5BX%5D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=Pr%28%7CX-E%5BX%5D%7C+%5Cgeq+a%29+%5Cleq+%5Cfrac%7B1%7D%7Ba%5E2%7D%5C%3BVar%5BX%5D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=Pr%28%7CX-E%5BX%5D%7C+%5Cgeq+a%29+%5Cleq+%5Cfrac%7B1%7D%7Ba%5E2%7D%5C%3BVar%5BX%5D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="Pr(|X-E[X]| &#92;geq a) &#92;leq &#92;frac{1}{a^2}&#92;;Var[X]" class="latex" /></p>
<h3 align="justify">Proof of Chebyshev Inequality</h3>
<p align="justify">The proof of this inequality is straightforward and comes from a clever application of Markov inequality. As discussed above we select <img src="https://s0.wp.com/latex.php?latex=f%28x%29+%3D+%7CX-E%5BX%5D%7C%5E2&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=f%28x%29+%3D+%7CX-E%5BX%5D%7C%5E2&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=f%28x%29+%3D+%7CX-E%5BX%5D%7C%5E2&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="f(x) = |X-E[X]|^2" class="latex" />. Using it we get ,</p>
<p align="center">    <img src="https://s0.wp.com/latex.php?latex=Pr%28%7CX-E%5BX%5D%7C+%5Cgeq+a%29+%3D+Pr%28+%28X-E%5BX%5D%29%5E2+%5Cgeq+a%5E2%29&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=Pr%28%7CX-E%5BX%5D%7C+%5Cgeq+a%29+%3D+Pr%28+%28X-E%5BX%5D%29%5E2+%5Cgeq+a%5E2%29&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=Pr%28%7CX-E%5BX%5D%7C+%5Cgeq+a%29+%3D+Pr%28+%28X-E%5BX%5D%29%5E2+%5Cgeq+a%5E2%29&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="Pr(|X-E[X]| &#92;geq a) = Pr( (X-E[X])^2 &#92;geq a^2)" class="latex" /><br />
<img src="https://s0.wp.com/latex.php?latex=Pr%28+%28X-E%5BX%5D%29%5E2+%5Cgeq+a%5E2%29+%5Cleq+%5Cfrac%7B1%7D%7Ba%5E2%7D+E%5B%28X-E%5BX%5D%29%5E2%5D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=Pr%28+%28X-E%5BX%5D%29%5E2+%5Cgeq+a%5E2%29+%5Cleq+%5Cfrac%7B1%7D%7Ba%5E2%7D+E%5B%28X-E%5BX%5D%29%5E2%5D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=Pr%28+%28X-E%5BX%5D%29%5E2+%5Cgeq+a%5E2%29+%5Cleq+%5Cfrac%7B1%7D%7Ba%5E2%7D+E%5B%28X-E%5BX%5D%29%5E2%5D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="Pr( (X-E[X])^2 &#92;geq a^2) &#92;leq &#92;frac{1}{a^2} E[(X-E[X])^2]" class="latex" /></p>
<p align="justify">We used the Markov inequality in the second line and used the fact that <img src="https://s0.wp.com/latex.php?latex=Var%5BX%5D+%3D+E%5B%28X-E%5BX%5D%29%5E2%5D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=Var%5BX%5D+%3D+E%5B%28X-E%5BX%5D%29%5E2%5D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=Var%5BX%5D+%3D+E%5B%28X-E%5BX%5D%29%5E2%5D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="Var[X] = E[(X-E[X])^2]" class="latex" />.</p>
<h3 align="justify">Common Pitfalls</h3>
<p align="justify">It is important to notice that Chebyshev provides bound on both sides of the error. One common mistake to do when applying Chebyshev is to divide the resulting probabilistic bound by 2 to get one sided error. This is valid only if the distribution is symmetric. Else it will give incorrect results. You can refer Wikipedia to see one sided Chebyshev inequalities.</p>
<h3 align="justify">Chebyshev Inequality for higher moments</h3>
<p align="justify">One of the neat applications of Chebyshev inequality is to use it for higher moments. As you would have observed, in Markov inequality, we used only the first moment. In the Chebyshev inequality, we use the second moment (and first). We can use the proof above to adapt Chebyshev inequality for higher moments. In this post, I will give a simple argument for even moments only. For general argument (odd and even) look at this <a href="http://mathoverflow.net/questions/28296/analog-of-chebyshevs-inequality-for-higher-moments"><strong>Math Overflow post</strong></a>.</p>
<p align="justify">The proof of Chebyshev for higher moments is almost exactly the same as the one above. The only observation we make is that <img src="https://s0.wp.com/latex.php?latex=%28X-E%5BX%5D%29%5E%7B2k%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%28X-E%5BX%5D%29%5E%7B2k%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%28X-E%5BX%5D%29%5E%7B2k%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="(X-E[X])^{2k}" class="latex" /> is always non negative for any k. Of course the next observation is <img src="https://s0.wp.com/latex.php?latex=E%5B%28X-E%5BX%5D%29%5E%7B2k%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=E%5B%28X-E%5BX%5D%29%5E%7B2k%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=E%5B%28X-E%5BX%5D%29%5E%7B2k%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="E[(X-E[X])^{2k}" class="latex" /> gives the 2k^th central moment . Using the statement from Mitzenmacher and Upfal&#8217;s book we get ,</p>
<p align="center">    <img src="https://s0.wp.com/latex.php?latex=Pr%28%7CX-E%5BX%5D%7C+%3E+t+%5Csqrt%5B2k%5D+%7BE%5B%28X-E%5BX%5D%29%5E%7B2k%7D%5D%7D%29+%5Cleq+%5Cfrac%7B1%7D%7Bt%5E%7B2k%7D%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=Pr%28%7CX-E%5BX%5D%7C+%3E+t+%5Csqrt%5B2k%5D+%7BE%5B%28X-E%5BX%5D%29%5E%7B2k%7D%5D%7D%29+%5Cleq+%5Cfrac%7B1%7D%7Bt%5E%7B2k%7D%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=Pr%28%7CX-E%5BX%5D%7C+%3E+t+%5Csqrt%5B2k%5D+%7BE%5B%28X-E%5BX%5D%29%5E%7B2k%7D%5D%7D%29+%5Cleq+%5Cfrac%7B1%7D%7Bt%5E%7B2k%7D%7D&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="Pr(|X-E[X]| &gt; t &#92;sqrt[2k] {E[(X-E[X])^{2k}]}) &#92;leq &#92;frac{1}{t^{2k}}" class="latex" /></p>
<p align="justify">It should be intuitive to note that the more information we get the tighter the bound is. For Markov we got 1/t as the fraction. It was 1/a^2 for second order Chebyshev and 1/a^k for k^th order Chebyshev inequality.</p>
<h3 align="justify">Chebyshev Inequality and Confidence Interval</h3>
<p align="justify">Using Chebyshev inequality, we previously claimed that atmost one fourth of the values that X can take is beyond 2 standard deviation of the mean. It is possible to turn this statement around to get a confidence interval.</p>
<p align="justify">If atmost 25% of the population are beyond 2 standard deviations away from mean, then we can be confident that atleast 75% of the population lie in the interval <img src="https://s0.wp.com/latex.php?latex=%28E%5BX%5D-2+%5Csigma%2C+E%5BX%5D%2B2+%5Csigma%29&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%28E%5BX%5D-2+%5Csigma%2C+E%5BX%5D%2B2+%5Csigma%29&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%28E%5BX%5D-2+%5Csigma%2C+E%5BX%5D%2B2+%5Csigma%29&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="(E[X]-2 &#92;sigma, E[X]+2 &#92;sigma)" class="latex" />. More generally, we can claim that, <img src="https://s0.wp.com/latex.php?latex=100+%2A+%281-%5Cfrac%7B1%7D%7Bk%7D%29&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=100+%2A+%281-%5Cfrac%7B1%7D%7Bk%7D%29&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=100+%2A+%281-%5Cfrac%7B1%7D%7Bk%7D%29&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="100 * (1-&#92;frac{1}{k})" class="latex" /> percentage of the population lies in the interval <img src="https://s0.wp.com/latex.php?latex=%28E%5BX%5D-k.+%5Csigma%2C+E%5BX%5D%2Bk+%5Csigma%29&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%28E%5BX%5D-k.+%5Csigma%2C+E%5BX%5D%2Bk+%5Csigma%29&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%28E%5BX%5D-k.+%5Csigma%2C+E%5BX%5D%2Bk+%5Csigma%29&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="(E[X]-k. &#92;sigma, E[X]+k &#92;sigma)" class="latex" /> . We can similarly derive that 94% of the population lie within 4 standard deviations away from mean.</p>
<h3 align="justify">Applications of Chebyshev Inequality</h3>
<p align="justify">We previously saw two applications of Chebyshev inequality &#8211; One to get tighter bounds using higher moments without using complex inequalities. The other is to estimate confidence interval. There are some other cool applications that we will state without providing the proof. For proofs refer the Wikipedia entry on <a href="http://en.wikipedia.org/wiki/Chebyshev%27s_inequality"><strong>Chebyshev inequality</strong></a>.</p>
<p align="justify">(1) Using Chebyshev inequality, we can prove that the median is atmost one standard deviation away from the mean.<br />
(2) Chebyshev inequality also provides the simplest proof for weak law of large numbers.</p>
<h3 align="justify">Tightness of Chebyshev Inequality</h3>
<p align="justify">Similar to Markov inequality, we can prove the tightness of Chebyshev inequality. I had fun deriving this proof and hopefully some one will find it useful. Define a random variable X as ,</p>
<p align="justify">[<strong>Note</strong>: I could not make the case statement work in WordPress Latex and hence the crude work around]</p>
<p align="justify">X = { <img src="https://s0.wp.com/latex.php?latex=%5Cmu+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cmu+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cmu+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;mu " class="latex" /> + C  with probability p</p>
<p align="justify">      { <img src="https://s0.wp.com/latex.php?latex=%5Cmu+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cmu+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cmu+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;mu " class="latex" /> &#8211; C  with probability p</p>
<p align="justify">      { <img src="https://s0.wp.com/latex.php?latex=%5Cmu+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5Cmu+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5Cmu+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;mu " class="latex" /> with probability 1-2p</p>
<p align="center">    <img src="https://s0.wp.com/latex.php?latex=E%5BX%5D+%3D+p%28%5Cmu+%2BC%29+%2B+p%28%5Cmu+-C%29+%2B+%281-2p%29+%5Cmu+%3D+%5Cmu+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=E%5BX%5D+%3D+p%28%5Cmu+%2BC%29+%2B+p%28%5Cmu+-C%29+%2B+%281-2p%29+%5Cmu+%3D+%5Cmu+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=E%5BX%5D+%3D+p%28%5Cmu+%2BC%29+%2B+p%28%5Cmu+-C%29+%2B+%281-2p%29+%5Cmu+%3D+%5Cmu+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="E[X] = p(&#92;mu +C) + p(&#92;mu -C) + (1-2p) &#92;mu = &#92;mu " class="latex" /><br />
<img src="https://s0.wp.com/latex.php?latex=Var%5BX%5D+%3D+E%5B%28X-%5Cmu%29%5E2%5D+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=Var%5BX%5D+%3D+E%5B%28X-%5Cmu%29%5E2%5D+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=Var%5BX%5D+%3D+E%5B%28X-%5Cmu%29%5E2%5D+&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="Var[X] = E[(X-&#92;mu)^2] " class="latex" /><br />
<img src="https://s0.wp.com/latex.php?latex=%3D+p+%28%5Cmu%2BC-%5Cmu%29%5E2+%2B+p+%28%5Cmu-C-%5Cmu%29%5E2+%2B+%281-2p%29%28%5Cmu-%5Cmu%29%5E2&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%3D+p+%28%5Cmu%2BC-%5Cmu%29%5E2+%2B+p+%28%5Cmu-C-%5Cmu%29%5E2+%2B+%281-2p%29%28%5Cmu-%5Cmu%29%5E2&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%3D+p+%28%5Cmu%2BC-%5Cmu%29%5E2+%2B+p+%28%5Cmu-C-%5Cmu%29%5E2+%2B+%281-2p%29%28%5Cmu-%5Cmu%29%5E2&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="= p (&#92;mu+C-&#92;mu)^2 + p (&#92;mu-C-&#92;mu)^2 + (1-2p)(&#92;mu-&#92;mu)^2" class="latex" /><br />
<img src="https://s0.wp.com/latex.php?latex=%5CRightarrow+Var%5BX%5D+%3D+2pC%5E2&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=%5CRightarrow+Var%5BX%5D+%3D+2pC%5E2&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=%5CRightarrow+Var%5BX%5D+%3D+2pC%5E2&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="&#92;Rightarrow Var[X] = 2pC^2" class="latex" /></p>
<p align="justify">If we want to find the probability that the variable deviates from mean by constant C, the bound provided by Chebyshev is ,</p>
<p align="center">    <img src="https://s0.wp.com/latex.php?latex=Pr%28%7CX-%5Cmu%7C+%5Cgeq+C%29+%5Cleq+%5Cfrac%7BVar%5BX%5D%7D%7BC%5E2%7D+%3D+%5Cfrac%7B2pC%5E2%7D%7BC%5E2%7D%3D2p&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002" srcset="https://s0.wp.com/latex.php?latex=Pr%28%7CX-%5Cmu%7C+%5Cgeq+C%29+%5Cleq+%5Cfrac%7BVar%5BX%5D%7D%7BC%5E2%7D+%3D+%5Cfrac%7B2pC%5E2%7D%7BC%5E2%7D%3D2p&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002 1x, https://s0.wp.com/latex.php?latex=Pr%28%7CX-%5Cmu%7C+%5Cgeq+C%29+%5Cleq+%5Cfrac%7BVar%5BX%5D%7D%7BC%5E2%7D+%3D+%5Cfrac%7B2pC%5E2%7D%7BC%5E2%7D%3D2p&#038;bg=ffffff&#038;fg=333333&#038;s=0&#038;c=20201002&#038;zoom=4.5 4x" alt="Pr(|X-&#92;mu| &#92;geq C) &#92;leq &#92;frac{Var[X]}{C^2} = &#92;frac{2pC^2}{C^2}=2p" class="latex" /></p>
<p align="justify">which is tight !</p>
<h3 align="justify">Conclusion</h3>
<p align="justify">Markov and Chebyshev inequalities are two of the simplest , yet very powerful inequalities. Clever application of them provide very useful bounds without knowing anything about the distribution of the random variable. Markov inequality bounds the probability that a nonnegative random variable exceeds any multiple of its expected value (or any constant). Chebyshev&#8217;s inequality , on the other hand, bounds the probability that a random variable deviates from its expected value by any multiple of its standard deviation. Chebyshev does not expect the variable to non negative but needs additional information to provide a tighter bound. Both Markov and Chebyshev inequalities are tight &#8211; This means with the information provided, the inequalities provide the most information they can provide.</p>
<p align="justify">Hope this post was useful ! Let me know if there is any insight I had missed !</p>
<h3 align="justify">References</h3>
<p align="justify">(1) Probability and Computing by Mitzenmacher and Upfal.<br />
(2) An interactive lesson plan on Markov&#8217;s inequality &#8211; An extremely good discussion on how to teach Markov inequality to students.<br />
(3) This <a href="http://junction.stanford.edu/~lall/svn/engr207b_2010_to_2011_winter/data/estimation_and_prediction_2011_01_11_01.pdf"><strong>lecture note</strong></a> from Stanford &#8211; Treats the inequalities from a prediction perspective.<br />
(4) Found this <a href="http://www.stat.berkeley.edu/~stark/SticiGui/Text/location.htm"><strong>interesting link</strong></a> from Berkeley recently.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://saravananthirumuruganathan.wordpress.com/2011/07/02/detailed-tutorial-on-markov-and-chebyshev-inequalities/feed/</wfw:commentRss>
			<slash:comments>18</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">554</post-id>
		<media:content url="https://2.gravatar.com/avatar/e2957c9d69690d3fef33fa2bb570d139abce0df84a663fcc30c2ffc3a77b2583?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">saravananthirumuruganathan</media:title>
		</media:content>
	</item>
	</channel>
</rss>
