<?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/"
	>

<channel>
	<title>Hong, LiangJie</title>
	<atom:link href="http://www.hongliangjie.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hongliangjie.com</link>
	<description>Dept. of Computer Science and Engineering at Lehigh University</description>
	<lastBuildDate>Sun, 06 May 2012 17:26:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Notes on &#8220;A comparison of logistic regression and naive Bayes&#8221;</title>
		<link>http://www.hongliangjie.com/2012/05/06/notes-on-a-comparison-of-logistic-regression-and-naive-bayes/</link>
		<comments>http://www.hongliangjie.com/2012/05/06/notes-on-a-comparison-of-logistic-regression-and-naive-bayes/#comments</comments>
		<pubDate>Sun, 06 May 2012 17:25:29 +0000</pubDate>
		<dc:creator>Liangjie Hong</dc:creator>
				<category><![CDATA[Classic Papers]]></category>
		<category><![CDATA[Research in General]]></category>

		<guid isPermaLink="false">http://www.hongliangjie.com/?p=886</guid>
		<description><![CDATA[Andrew Y. Ng and Michael I. Jordan had a classic paper on the comparison between logistic regression and naive Bayes. The main contribution of the paper is a theoretical analysis of how logistic regression and naive Bayes might perform and &#8230;<p class="read-more"><a href="http://www.hongliangjie.com/2012/05/06/notes-on-a-comparison-of-logistic-regression-and-naive-bayes/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://ai.stanford.edu/~ang/" target="_blank">Andrew Y. Ng</a> and <a href="http://www.cs.berkeley.edu/~jordan/" target="_blank">Michael I. Jordan</a> had a classic paper on the comparison between logistic regression and naive Bayes. The main contribution of the paper is a theoretical analysis of how logistic regression and naive Bayes might perform and an experimental comparison to support this idea.</p>
<p>Several important points made by the paper. The first is that:</p>
<ul>
<li><strong>The asymptotic error made by logistic regression is no more than that made by naive Bayes (Proposition 1 in the paper).</strong></li>
</ul>
<p style="text-align: justify;">This conclusion provides a basis for what seems to be the widely held belief that discriminative classifiers are better than generative ones. The main conclusions of the paper is about the sample complexity of both classifiers. Sample complexity is the number of examples needed to approach the asymptotic error. For logistic regression, such sample complexity is:<br />
\[<br />
m = \Omega(n)<br />
\]which means that the sample complexity is linear in \( n \) (<strong>Proposition 2 in the paper</strong>). For naive Bayes, we have:<br />
\[<br />
m = O(\log n)<br />
\]which means that the sample complexity is logarithmic in \( n \) (<strong>Lemma 3 and Corollary 6 in the paper</strong>). All these conclusions imply that even though naive Bayes converges to a higher asymptotic error compared to logistic regression, it may also approach it significantly faster &#8212; after \( O(\log n) \), rather than \( O(n) \), training examples.</p>
<p style="text-align: justify;">These ideas are discussed in [1]. Are these theoretical analysis always hold in practice? Not very reliable as far as [2] suggested. Note that these conclusions coming after strong assumptions. Nevertheless, they provide some general idea between logistic regression and naive Bayes.</p>
<p style="text-align: justify;"><strong>References:</strong></p>
<p style="text-align: justify;">[1] <strong>On Discriminative vs. Generative Classifiers: A comparison of logistic regression and Naive Bayes</strong>, Andrew Y. Ng and Michael Jordan. In <em>NIPS 14</em>, 2002. [<a href="http://ai.stanford.edu/~ang/papers/nips01-discriminativegenerative.ps">ps</a>, <a href="http://ai.stanford.edu/~ang/papers/nips01-discriminativegenerative.pdf">pdf</a>]<br />
[2] <strong>Comment on &#8220;On Discriminative vs. Generative Classifiers: A Comparison of Logistic Regression and Naive Bayes&#8221;</strong>. Jing-Hao Xue and D. Michael Titterington. 2008. <em>Neural Processing Letters</em> 28, 3 (December 2008), 169-187. [<a href="http://www.newton.ac.uk/preprints/NI09001.pdf" target="_blank">pdf</a>]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hongliangjie.com/2012/05/06/notes-on-a-comparison-of-logistic-regression-and-naive-bayes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quantum Mechanics and Information Retrieval</title>
		<link>http://www.hongliangjie.com/2012/04/15/quantum-mechanics-and-information-retrieval/</link>
		<comments>http://www.hongliangjie.com/2012/04/15/quantum-mechanics-and-information-retrieval/#comments</comments>
		<pubDate>Mon, 16 Apr 2012 03:31:43 +0000</pubDate>
		<dc:creator>Liangjie Hong</dc:creator>
				<category><![CDATA[IR]]></category>

		<guid isPermaLink="false">http://www.hongliangjie.com/?p=867</guid>
		<description><![CDATA[How quantum mechanics can be related to information retrieval? In this year&#8217;s ECIR 2012, Dr. Benjamin Piwowarski and Dr. Massino Melucci gave a tutorial on Quantum Information Access and Retrieval. It is still a little bit difficult to follow up the ideas introduced &#8230;<p class="read-more"><a href="http://www.hongliangjie.com/2012/04/15/quantum-mechanics-and-information-retrieval/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">How quantum mechanics can be related to information retrieval? In this year&#8217;s <a href="http://ecir2012.upf.edu/" target="_blank">ECIR 2012</a>, Dr. <a href="http://www.bpiwowar.net/" target="_blank">Benjamin Piwowarski</a> and Dr. <a href="http://ims.dei.unipd.it/websites/massimo.melucci/index.php/" target="_blank">Massino Melucci</a> gave a tutorial on <a href="http://www.bpiwowar.net/wp-content/uploads/2012/04/tutorial-handout.pdf" target="_blank">Quantum Information Access and Retrieval</a>.</p>
<p style="text-align: justify;">It is still a little bit difficult to follow up the ideas introduced in the slides. However, it has been demonstrated in the tutorial that many aspects of IR are developed or re-phrased under QT framework, such as relevance theory, mixture of models, multiple document summarization and relevance feedback.</p>
<p style="text-align: justify;">The current biggest initiative of QM is <a href="http://www.quantuminteraction.org/conferences" target="_blank">Quantum Interaction conferences</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hongliangjie.com/2012/04/15/quantum-mechanics-and-information-retrieval/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New MAP Estimation of latent Dirichlet allocation</title>
		<link>http://www.hongliangjie.com/2012/04/11/new-map-estimation-of-latent-dirichlet-allocation/</link>
		<comments>http://www.hongliangjie.com/2012/04/11/new-map-estimation-of-latent-dirichlet-allocation/#comments</comments>
		<pubDate>Wed, 11 Apr 2012 20:54:19 +0000</pubDate>
		<dc:creator>Liangjie Hong</dc:creator>
				<category><![CDATA[Topic Model]]></category>

		<guid isPermaLink="false">http://www.hongliangjie.com/?p=847</guid>
		<description><![CDATA[A recent published paper entitled &#8220;On Estimation and Selection for Topic Models&#8221; by Matthew A. Taddy on AISTATS 2012 is interesting. Firstly, this paper tries to introduce a new way to do inference for latent Dirichlet allocation (LDA). Traditionally, variational &#8230;<p class="read-more"><a href="http://www.hongliangjie.com/2012/04/11/new-map-estimation-of-latent-dirichlet-allocation/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>A recent published paper entitled &#8220;<a href="http://jmlr.csail.mit.edu/proceedings/papers/v22/taddy12/taddy12.pdf" target="_blank">On Estimation and Selection for Topic Models</a>&#8221; by <a href="http://faculty.chicagobooth.edu/matt.taddy/" target="_blank">Matthew A. Taddy</a> on <a href="http://www.aistats.org/" target="_blank">AISTATS 2012</a> is interesting.</p>
<p style="text-align: justify;">Firstly, this paper tries to introduce a new way to do inference for latent Dirichlet allocation (LDA). Traditionally, variational inference and Gibbs sampling are widely used to perform Bayesian inference for LDA. For a simpler version of LDA, Probabilistic Latent Semantic Analysis (PLSA), a Maximum Likelihood Estimation (MLE) via EM algorithm is usually used where no prior distributions are used at all. Both Bayesian inference and MLE can be treated as two extremes to learning a LDA or PLSA as MLE may be prone to over-fitting but Bayesian inference is a little bit overly complicated. The parameterization of the model is slightly different from normal settings. Let \( \mathbf{x}_{i} \) be a vector of counts in \( V \) categories (terms) for document \( i \). Basically, each element in this vector is the number of times term \( v \) appearing in document \( i \). We also have the total term count \( m_{i} = \sum_{j}^{V} x_{i,j} \). Thus, the \( K \)-topic model has the following generative story:</p>
<p>\[<br />
\begin{aligned}<br />
\mathbf{x}_{i} \sim \mbox{Multinomial}(\boldsymbol{\theta}_{i,1} \boldsymbol{\phi}_{1} + \cdots + \boldsymbol{\theta}_{i,K} \boldsymbol{\phi}_{K}, m_{i})<br />
\end{aligned}<br />
\]</p>
<p style="text-align: justify;">where \( \boldsymbol{\theta}_{i} \) is a per-document distribution over topics and \( \boldsymbol{\phi}_{k} \) is a distribution over words. Of course, this setting is indeed exactly same as previously published settings. However, we can easily view the term counts as a function of total counts explicitly. Again, Dirichlet distributions are put on \( \boldsymbol{\theta} \) and \( \boldsymbol{\phi} \). The paper discusses a joint posterior maximization by using EM algorithm. The author treats the term count vector \( \mathbf{x}_{i} \) as:</p>
<p>\[<br />
\begin{aligned}<br />
\mathbf{x}_{i} \sim \mbox{Multinomial}(\boldsymbol{\phi}_{1}, t_{i,1}) + \cdots + \mbox{Multinomial}(\boldsymbol{\phi}_{K}, t_{i,K})<br />
\end{aligned}<br />
\]</p>
<p style="text-align: justify;">where \( \mathbf{t}_{i} \sim \mbox{Multinomial}(\boldsymbol{\theta}_{i}, m_{i}) \) and these \( \mathbf{t}_{i} \) are treated as missing-data for EM algorithm. Thus, missing-data is no longer single topic assignments for each term but aggregated topic counts for the whole documents. The detailed derivations of the corresponding EM algorithm is in the paper.</p>
<p style="text-align: justify;">The second point of the paper is to propose some new method to perform model selection, which is always difficult for topic models in some sense. The whole idea is to find \( K \) that can maximize \( P(\mathbf{X} \, | \, K) \), which is usually approximated through MCMC. In this paper, the author details Laplace&#8217;s method to approximate this probability. Again, details are in the paper. The last point made by the paper is to use multinomial dispersion to evaluate whether the model fits the data, which is standard in statistics but never used in topic modeling literature.</p>
<p style="text-align: justify;">The experiments are somewhat interesting. The author demonstrated that MAP is almost always comparable to variational EM (VEM) and sometimes slightly better than Gibbs sampling. Sometimes, MAP is even better than VEM.</p>
<p style="text-align: justify;">Note, this is NOT the first paper to propose a MAP learning for topic models. Here&#8217;s one at least:</p>
<p style="text-align: justify;">J. T. Chien, J. T. Chien, M. S. Wu, and M. S. Wu. <strong>Adaptive Bayesian Latent Semantic Analysis</strong>. Audio, Speech, and Language Processing, IEEE Transactions on [seealso Speech and Audio Processing, IEEE Transactions on], 16(1):198–207, 2008.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hongliangjie.com/2012/04/11/new-map-estimation-of-latent-dirichlet-allocation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PAC Learnable and VC Dimension</title>
		<link>http://www.hongliangjie.com/2012/04/10/pac-learnable-and-vc-dimension/</link>
		<comments>http://www.hongliangjie.com/2012/04/10/pac-learnable-and-vc-dimension/#comments</comments>
		<pubDate>Tue, 10 Apr 2012 18:12:10 +0000</pubDate>
		<dc:creator>Liangjie Hong</dc:creator>
				<category><![CDATA[Research in General]]></category>

		<guid isPermaLink="false">http://www.hongliangjie.com/?p=843</guid>
		<description><![CDATA[Recently, I gave a presentation on PAC learnable and VC dimension. The paper is &#8220;Learnability and the Vapnik-Chervonenkis Dimension&#8221; by Blumer et al. Slides for VC Dimension.]]></description>
			<content:encoded><![CDATA[<p>Recently, I gave a presentation on PAC learnable and VC dimension. The paper is &#8220;<a href="http://www.trhvidsten.com/docs/classics/Blumer-1989.pdf" target="_blank">Learnability and the Vapnik-Chervonenkis Dimension</a>&#8221; by Blumer et al.</p>
<p><a href="http://www.hongliangjie.com/wp-content/uploads/2012/04/source.pdf">Slides for VC Dimension</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hongliangjie.com/2012/04/10/pac-learnable-and-vc-dimension/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hack Tweepy to Get Raw JSON</title>
		<link>http://www.hongliangjie.com/2012/04/04/hack-tweepy-to-get-raw-json/</link>
		<comments>http://www.hongliangjie.com/2012/04/04/hack-tweepy-to-get-raw-json/#comments</comments>
		<pubDate>Thu, 05 Apr 2012 01:14:34 +0000</pubDate>
		<dc:creator>Liangjie Hong</dc:creator>
				<category><![CDATA[Research in General]]></category>

		<guid isPermaLink="false">http://www.hongliangjie.com/?p=839</guid>
		<description><![CDATA[Tweepy is a popular Python wrapper for Twitter API. Although it provides a lot of useful features where you don&#8217;t need to deal with JSON or XML directly, sometimes you would like to store all information you have requested. In other &#8230;<p class="read-more"><a href="http://www.hongliangjie.com/2012/04/04/hack-tweepy-to-get-raw-json/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p><a href="https://github.com/tweepy/tweepy" target="_blank">Tweepy</a> is a popular Python wrapper for <a href="https://dev.twitter.com/" target="_blank">Twitter API</a>. Although it provides a lot of useful features where you don&#8217;t need to deal with JSON or XML directly, sometimes you would like to store all information you have requested. In other words, you would like to store the raw JSON returned by Tweepy.</p>
<p>For some reason, this feature is not supported by the current version of Tweepy (as of April 4, 2012). So, we need to hack a little bit to get the result. Indeed, this issue was discussed <a href="https://github.com/tweepy/tweepy/issues/35" target="_blank">here</a>. The current solution is to add the following code into &#8220;parsers.py&#8221; of the source code of Tweepy:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> RawJsonParser<span style="color: black;">&#40;</span>Parser<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> parse<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, method, payload<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> payload</pre></td></tr></table></div>

<p>Therefore, we can call the following code for invoking a new parser with JSON returned:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> tweepy.<span style="color: black;">parsers</span> <span style="color: #ff7700;font-weight:bold;">import</span> RawJsonParser
api = tweepy.<span style="color: black;">API</span><span style="color: black;">&#40;</span>auth_handler=auth, <span style="color: #dc143c;">parser</span>=RawJsonParser<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.hongliangjie.com/2012/04/04/hack-tweepy-to-get-raw-json/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Install Numpy and Scipy on CentOS without Root Privilege</title>
		<link>http://www.hongliangjie.com/2012/04/01/install-numpy-and-scipy-on-centos-without-root-privilege/</link>
		<comments>http://www.hongliangjie.com/2012/04/01/install-numpy-and-scipy-on-centos-without-root-privilege/#comments</comments>
		<pubDate>Mon, 02 Apr 2012 01:18:22 +0000</pubDate>
		<dc:creator>Liangjie Hong</dc:creator>
				<category><![CDATA[Research in General]]></category>

		<guid isPermaLink="false">http://www.hongliangjie.com/?p=810</guid>
		<description><![CDATA[Sometimes you want to install Numpy and Scipy on a remote CentOS machine without root privilege, which is usually true when you are using a university server. Before you proceed to the following instructions, you need to make sure that a &#8230;<p class="read-more"><a href="http://www.hongliangjie.com/2012/04/01/install-numpy-and-scipy-on-centos-without-root-privilege/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>Sometimes you want to install <a href="http://numpy.scipy.org/" target="_blank">Numpy and Scipy</a> on a remote CentOS machine without root privilege, which is usually true when you are using a university server. Before you proceed to the following instructions, you need to make sure that a copy of Python is installed. This is also done without root privilege, meaning that you may install it in an alternative directory, rather than system directory.</p>
<h3>Prerequisite:</h3>
<ol>
<li>Download the latest version of <a href="http://www.netlib.org/lapack/" target="_blank">LAPACK</a> and extracted it into path [LAPACK].</li>
<li>Download the latest version of <a href="http://www.netlib.org/blas/" target="_blank">BLAS</a> and extracted it into path [BLAS].</li>
</ol>
<p>Note that in almost all tutorials on how to install Numpy and Scipy on Linux machines discuss how to install them with ATALS. This is possible only if you have the root privilege where you can turn off CPU threshoding . Since we do not have root privilege, we can only install them with LAPACK and BLAS.</p>
<h3>Step 1: Install Numpy</h3>
<ol>
<li>Edit &#8220;site.cfg&#8221;<br />
a) Enable &#8220;[DEFAULT]&#8221; section and add</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">src_dirs = [BLAS]:[LAPACK]</pre></div></div>

<p>b) Add</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">[blas_opt]
libraries = f77blas, cblas
&nbsp;
[lapack_opt]
libraries = lapack, f77blas, cblas</pre></div></div>

</li>
<li>Type the following command in the shell:

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">python setup.py build <span style="color: #660033;">--fcompiler</span>=gnu95</pre></div></div>

<p>which will compile the package with &#8220;gfortran&#8221;.</li>
<li>Type the following command in the shell:

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">python setup.py <span style="color: #c20cb9; font-weight: bold;">install</span></pre></div></div>

</li>
</ol>
<h3>Step 2: Install Scipy</h3>
<p style="padding-left: 30px;">Once Numpy is installed.  Scipy can be easily built and installed through normal &#8220;python setup.py build&#8221; and &#8220;python setup.py install&#8221; process. Remember that these command should be accompanied with &#8220;&#8211;fcompiler=gnu95&#8243;.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hongliangjie.com/2012/04/01/install-numpy-and-scipy-on-centos-without-root-privilege/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>LaTex in WordPress</title>
		<link>http://www.hongliangjie.com/2012/03/31/latex-in-wordpress/</link>
		<comments>http://www.hongliangjie.com/2012/03/31/latex-in-wordpress/#comments</comments>
		<pubDate>Sun, 01 Apr 2012 03:38:05 +0000</pubDate>
		<dc:creator>Liangjie Hong</dc:creator>
				<category><![CDATA[Research in General]]></category>

		<guid isPermaLink="false">http://www.hongliangjie.com/?p=812</guid>
		<description><![CDATA[QuickLaTex in WordPress is amazing! Well, TiKz in Latex is also amazing!]]></description>
			<content:encoded><![CDATA[<p><a href="http://wordpress.org/extend/plugins/wp-quicklatex/" target="_blank"> QuickLaTex</a> in WordPress is amazing! Well, TiKz in Latex is also amazing!</p>
<p class="ql-center-picture"><img src="http://www.hongliangjie.com/wp-content/ql-cache/quicklatex.com-1dae01a8d6d7fb29d86f72439099409b_l3.png"class="ql-img-picture"alt="Rendered by QuickLaTeX.com" title="Rendered by QuickLaTeX.com"/></p>
]]></content:encoded>
			<wfw:commentRss>http://www.hongliangjie.com/2012/03/31/latex-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sorting Tuples in C++</title>
		<link>http://www.hongliangjie.com/2011/10/10/sortin-tuples-in-c/</link>
		<comments>http://www.hongliangjie.com/2011/10/10/sortin-tuples-in-c/#comments</comments>
		<pubDate>Mon, 10 Oct 2011 22:20:44 +0000</pubDate>
		<dc:creator>Liangjie Hong</dc:creator>
				<category><![CDATA[Research in General]]></category>

		<guid isPermaLink="false">http://www.hongliangjie.com/?p=748</guid>
		<description><![CDATA[In this post, I would like to show how to create a tuple object in C++ 11 and how to sort tuples.<p class="read-more"><a href="http://www.hongliangjie.com/2011/10/10/sortin-tuples-in-c/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>In this post, I would like to show how to create a tuple object in C++ 11 and how to sort tuples.</p>
<p>Here is the code for creating tuples and doing the sort. It is pretty straightforward.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;string&gt;</span>
<span style="color: #339900;">#include &lt;vector&gt;</span>
<span style="color: #339900;">#include &lt;tuple&gt;</span>
<span style="color: #339900;">#include &lt;algorithm&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">typedef</span> tuple<span style="color: #000080;">&lt;</span>string,<span style="color: #0000ff;">double</span>,<span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span> mytuple<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">bool</span> mycompare <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> mytuple <span style="color: #000040;">&amp;</span>lhs, <span style="color: #0000ff;">const</span> mytuple <span style="color: #000040;">&amp;</span>rhs<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> get<span style="color: #000080;">&lt;</span><span style="color: #0000dd;">1</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>lhs<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;</span> get<span style="color: #000080;">&lt;</span><span style="color: #0000dd;">1</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>rhs<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
  vector<span style="color: #000080;">&lt;</span>mytuple<span style="color: #000080;">&gt;</span> data<span style="color: #008080;">;</span>
  data.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span>make_tuple<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;abc&quot;</span>,<span style="color:#800080;">4.5</span>,<span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  data.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span>make_tuple<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;def&quot;</span>,<span style="color:#800080;">5.5</span>,<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  data.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span>make_tuple<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;wolf&quot;</span>,<span style="color: #000040;">-</span><span style="color:#800080;">3.47</span>,<span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  sort<span style="color: #008000;">&#40;</span>data.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,data.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,mycompare<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>vector<span style="color: #000080;">&lt;</span>mytuple<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">iterator</span> iter <span style="color: #000080;">=</span> data.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> iter <span style="color: #000040;">!</span><span style="color: #000080;">=</span> data.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> iter<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> get<span style="color: #000080;">&lt;</span><span style="color: #0000dd;">0</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>iter<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span> <span style="color: #000080;">&lt;&lt;</span> get<span style="color: #000080;">&lt;</span><span style="color: #0000dd;">1</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>iter<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span> <span style="color: #000080;">&lt;&lt;</span> get<span style="color: #000080;">&lt;</span><span style="color: #0000dd;">2</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>iter<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The code is successfully compiled by G++ 4.6.1 with the option &#8220;-std=gnu++0x&#8221;.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hongliangjie.com/2011/10/10/sortin-tuples-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Two Forms of Logistic Regression</title>
		<link>http://www.hongliangjie.com/2011/10/03/two-forms-of-logistic-regression/</link>
		<comments>http://www.hongliangjie.com/2011/10/03/two-forms-of-logistic-regression/#comments</comments>
		<pubDate>Tue, 04 Oct 2011 01:18:29 +0000</pubDate>
		<dc:creator>Liangjie Hong</dc:creator>
				<category><![CDATA[Research in General]]></category>

		<guid isPermaLink="false">http://www.hongliangjie.com/?p=735</guid>
		<description><![CDATA[There are two forms of Logistic Regression used in literature. In this post, I will build a bridge between these two forms and show they are equivalent.<p class="read-more"><a href="http://www.hongliangjie.com/2011/10/03/two-forms-of-logistic-regression/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>There are two forms of Logistic Regression used in literature. In this post, I will build a bridge between these two forms and show they are equivalent.</p>
<h2>Logistic Function &amp; Logistic Regression</h2>
<p>The common definition of Logistic Function is as follows:<br />
\[<br />
P(x) = \frac{1}{1+\exp(-x)} \;\; \qquad (1)<br />
\] where \(x \in \mathbb{R} \) is the variable of the function and \(P(x) \in [0,1]\). One important property of Equation (1) is that:<br />
\[ \begin{eqnarray}<br />
P(-x) &amp;=&amp; \frac{1}{1+\exp(x)} \nonumber \\<br />
&amp;=&amp; \frac{1}{1+\frac{1}{\exp(-x)}} \nonumber \\<br />
&amp;=&amp; \frac{\exp(-x)}{1+\exp(-x)} \nonumber \\<br />
&amp;=&amp; 1 - \frac{1}{1+\exp(-x)} \nonumber \\<br />
&amp;=&amp; 1 - P(x) \; \; \qquad (2)<br />
\end{eqnarray} \]The form of Equation (2) is widely used as the form of Logistic Regression (e.g., [1,2,3]):<br />
\[ \begin{eqnarray}<br />
P(y = 1 \, | \, \boldsymbol{\beta}, \mathbf{x}) &amp;=&amp; \frac{\exp(\boldsymbol{\beta}^{T} \mathbf{x})}{1 + \exp(\boldsymbol{\beta}^{T} \mathbf{x})} \nonumber \\<br />
P(y = 0 \, | \, \boldsymbol{\beta}, \mathbf{x}) &amp;=&amp; \frac{1}{1 + \exp(\boldsymbol{\beta}^{T} \mathbf{x})} \;\; \qquad (3)<br />
\end{eqnarray} \] where \(\mathbf{x}\) is a feature vector and \(\boldsymbol{\beta}\) is a coefficient vector. By using Equation (2), we also have:<br />
\[ \begin{equation}<br />
P(y=1 \, | \, \boldsymbol{\beta}, \mathbf{x}) = 1 - P(y=0 \, | \, \boldsymbol{\beta}, \mathbf{x})<br />
\end{equation} \] This formalism of Logistic Regression is used in [1,2] where labels \( y \in \{0,1\} \) and the functional form of the probability to generate different labels is different. Another formalism introduced in [3] unified the two forms into one single equation by integrating the label and the prediction together:<br />
\[ \begin{equation}<br />
P(g= \pm 1 \, | \, \boldsymbol{\beta}, \mathbf{x}) = \frac{1}{1 + \exp( - g\boldsymbol{\beta}^{T} \mathbf{x})} \;\; \qquad (4)<br />
\end{equation} \]where \( g \in \{\pm 1\} \) is the label for data item \( x \). It is also easily to verify that \( P(g=1 \, | \, \boldsymbol{\beta}, \mathbf{x}) = 1 &#8211; P(g=-1 \, | \, \boldsymbol{\beta}, \mathbf{x}) \).</p>
<h2>The Equivalence of Two Forms of Logistic Regression</h2>
<p>At first glance, the form (3) and the form (4) looks very different. However, the equivalence between these two forms can be easily established. Starting from the form (3), we can have:<br />
\[ \begin{eqnarray}<br />
P(y = 1 \, | \, \boldsymbol{\beta}, \mathbf{x}) &amp;=&amp; \frac{\exp(\boldsymbol{\beta}^{T} \mathbf{x})}{1 + \exp(\boldsymbol{\beta}^{T} \mathbf{x})} \nonumber \\<br />
&amp;=&amp; \frac{1}{\frac{1}{\exp(\boldsymbol{\beta}^{T} \mathbf{x})} + 1} \nonumber \\<br />
&amp;=&amp; \frac{1}{\exp(-\boldsymbol{\beta}^{T} \mathbf{x}) + 1} \nonumber \\<br />
&amp;=&amp; P(g= 1 \, | \, \boldsymbol{\beta}, \mathbf{x})<br />
\end{eqnarray} \]We can also establish the equivalence between \( P(y=0 \, | \, \boldsymbol{\beta}, \mathbf{x})\) and \(P(g=-1 \, | \, \boldsymbol{\beta}, \mathbf{x})\) easily by using property (2). Another way to establish the equivalence is from the classification rule. For the form (3), we have the following classification rule:<br />
\[ \begin{eqnarray}<br />
\frac{\frac{\exp(\boldsymbol{\beta}^{T} \mathbf{x})}{1 + \exp(\boldsymbol{\beta}^{T} \mathbf{x})}}{\frac{1}{1 + \exp(\boldsymbol{\beta}^{T} \mathbf{x})}} &amp; &gt; &amp; 1 \;\; \rightarrow \;\; y = 1 \nonumber \\<br />
\exp(\boldsymbol{\beta}^{T} \mathbf{x}) &amp; &gt; &amp; 1 \nonumber \\<br />
\boldsymbol{\beta}^{T} \mathbf{x} &amp; &gt; &amp; 0<br />
\end{eqnarray} \]An exactly same classification rule for the form (4) can also be obtained as:<br />
\[ \begin{eqnarray}<br />
\frac{\frac{1}{1 + \exp( - \boldsymbol{\beta}^{T} \mathbf{x})}}{\frac{1}{1 + \exp( \boldsymbol{\beta}^{T} \mathbf{x})}} &amp; &gt; &amp; 1 \;\; \rightarrow \;\; g = 1 \nonumber \\<br />
\frac{1 + \exp(\boldsymbol{\beta}^{T} \mathbf{x})}{1 + \exp( - \boldsymbol{\beta}^{T} \mathbf{x})} &amp; &gt; &amp; 1 \nonumber \\<br />
\exp(\boldsymbol{\beta}^{T} \mathbf{x}) &amp; &gt; &amp; 1 \nonumber \\<br />
\boldsymbol{\beta}^{T} \mathbf{x} &amp; &gt; &amp; 0<br />
\end{eqnarray} \] Therefore, we can see that two forms essentially learn the same classification boundary.</p>
<h2>Logistic Loss</h2>
<p>Since we establish the equivalence of two forms of Logistic Regression, it is convenient to use the second form as it can be explained by a general classification framework. Here, we assume \( y \) is the label of data and \( \mathbf{x} \) is a feature vector. The classification framework can be formalized as follows:<br />
\[ \begin{equation}<br />
\arg\min \sum_{i} L\Bigr(y_{i},f(\mathbf{x}_{i})\Bigl)<br />
\end{equation}\]where \(f\) is a hypothesis function and \(L\) is loss function. For Logistic Regression, we have the following instantiation:<br />
\[ \begin{eqnarray}<br />
f(\mathbf{x}) &amp;=&amp; \boldsymbol{\beta}^{T} \mathbf{x} \nonumber \\<br />
L\Bigr(y,f(\mathbf{x})\Bigl) &amp;=&amp; \log \Bigr( 1 + \exp(-y f(\mathbf{x})\Bigl)<br />
\end{eqnarray}\]where \(y \in \{ \pm 1 \}\).</p>
<h2>References</h2>
<p>[1] Trevor Hastie, Robert Tibshirani, and Jerome Friedman. The Elements of Statistical Learning. Springer Series in Statistics. Springer New York Inc., New York, NY, USA, 2001.<br />
[2] Tom M. Mitchell. Machine learning. McGraw Hill series in computer science. McGraw-Hill, 1997.<br />
[3] Jason D. M. Rennie. <a href="http://people.csail.mit.edu/jrennie/writing/lr.pdf" target="_blank">Logistic Regression</a>. http://people.csail.mit.edu/jrennie/writing, April 2003.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hongliangjie.com/2011/10/03/two-forms-of-logistic-regression/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Random Number Generation with C++ 0x in GCC</title>
		<link>http://www.hongliangjie.com/2011/09/16/random-number-generation-with-c-0x-in-gcc/</link>
		<comments>http://www.hongliangjie.com/2011/09/16/random-number-generation-with-c-0x-in-gcc/#comments</comments>
		<pubDate>Sat, 17 Sep 2011 00:38:41 +0000</pubDate>
		<dc:creator>Liangjie Hong</dc:creator>
				<category><![CDATA[Practical Programming]]></category>

		<guid isPermaLink="false">http://www.hongliangjie.com/?p=725</guid>
		<description><![CDATA[In this post, I woud like to explore how random number would be generated by using newly added features from C++ 0x in GCC. Rather than explaining the details of C++ 0x, I just post code here: 1 2 3 &#8230;<p class="read-more"><a href="http://www.hongliangjie.com/2011/09/16/random-number-generation-with-c-0x-in-gcc/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>In this post, I woud like to explore how random number would be generated by using newly added features from C++ 0x in GCC.</p>
<p>Rather than explaining the details of C++ 0x, I just post code here:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;random&gt;</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;string&gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">int</span> n<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">double</span> p, lambda, shape, mu, sigma<span style="color: #008080;">;</span>
  mt19937 eng<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">time</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  uniform_int_distribution<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span> uniform_int<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">3</span>,<span style="color: #0000dd;">7</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;[Uniform INT distribution]:&quot;</span> <span style="color: #000080;">&lt;&lt;</span> uniform_int<span style="color: #008000;">&#40;</span>eng<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
  <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
  uniform_real_distribution<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">double</span><span style="color: #000080;">&gt;</span> uniform_real<span style="color: #008000;">&#40;</span><span style="color:#800080;">0.0</span>,<span style="color:#800080;">1.0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;[Uniform REAL distribution]:&quot;</span> <span style="color: #000080;">&lt;&lt;</span> uniform_real<span style="color: #008000;">&#40;</span>eng<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
  <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
  n <span style="color: #000080;">=</span> <span style="color: #0000dd;">5</span><span style="color: #008080;">;</span>
  p <span style="color: #000080;">=</span> <span style="color:#800080;">0.3</span><span style="color: #008080;">;</span>
  binomial_distribution<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span> binomial<span style="color: #008000;">&#40;</span>n, p<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;[Binomial distribution]:&quot;</span> <span style="color: #000080;">&lt;&lt;</span> binomial<span style="color: #008000;">&#40;</span>eng<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
  <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
  lambda <span style="color: #000080;">=</span> <span style="color:#800080;">4.0</span><span style="color: #008080;">;</span>
  exponential_distribution<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">double</span><span style="color: #000080;">&gt;</span> exponential<span style="color: #008000;">&#40;</span>lambda<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;[Exponential distribution]:&quot;</span> <span style="color: #000080;">&lt;&lt;</span> exponential<span style="color: #008000;">&#40;</span>eng<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
  <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
  shape <span style="color: #000080;">=</span> <span style="color:#800080;">3.0</span><span style="color: #008080;">;</span>
  gamma_distribution<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">double</span><span style="color: #000080;">&gt;</span> gamma<span style="color: #008000;">&#40;</span>shape<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;[Gamma distribution]:&quot;</span> <span style="color: #000080;">&lt;&lt;</span> gamma<span style="color: #008000;">&#40;</span>eng<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
  <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
  p <span style="color: #000080;">=</span> <span style="color:#800080;">0.5</span><span style="color: #008080;">;</span>
  geometric_distribution<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span> geometric<span style="color: #008000;">&#40;</span>p<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;[Geometric distribution]:&quot;</span> <span style="color: #000080;">&lt;&lt;</span> geometric<span style="color: #008000;">&#40;</span>eng<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
  <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
  mu <span style="color: #000080;">=</span> <span style="color:#800080;">3.0</span><span style="color: #008080;">;</span> sigma <span style="color: #000080;">=</span> <span style="color:#800080;">4.0</span><span style="color: #008080;">;</span>
  normal_distribution<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">double</span><span style="color: #000080;">&gt;</span> normal<span style="color: #008000;">&#40;</span>mu, sigma<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;[Gaussian distribution]:&quot;</span> <span style="color: #000080;">&lt;&lt;</span> normal<span style="color: #008000;">&#40;</span>eng<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
  <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
  lambda <span style="color: #000080;">=</span> <span style="color:#800080;">7.0</span><span style="color: #008080;">;</span>
  poisson_distribution<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span> poisson<span style="color: #008000;">&#40;</span>lambda<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;[Poission distribution]:&quot;</span> <span style="color: #000080;">&lt;&lt;</span> poisson<span style="color: #008000;">&#40;</span>eng<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
  <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
  p <span style="color: #000080;">=</span> <span style="color:#800080;">0.6</span><span style="color: #008080;">;</span>
  bernoulli_distribution bernoulli<span style="color: #008000;">&#40;</span>p<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">10</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;[Bernoulli distribution]:&quot;</span> <span style="color: #000080;">&lt;&lt;</span> bernoulli<span style="color: #008000;">&#40;</span>eng<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
  <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Note, the code is very clean in the sense that you don&#8217;t need any extra libraries at all.</p>
<p>Please compile with:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">g++ -std=gnu++0x</pre></div></div>

<p>The GCC I used is GCC 4.6.1</p>
<p>Several references:<br />
1. http://www.johndcook.com/test_TR1_random.html (sort of out-dated)<br />
2. http://www.johndcook.com/cpp_TR1_random.html (sort of out-dated)<br />
3. http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.200x (current status of C++ 0x in GCC)<br />
4. http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt01ch03s02.html ( a list of header files of C++ in GCC)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hongliangjie.com/2011/09/16/random-number-generation-with-c-0x-in-gcc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

