Building a function of a random variable dynamically in python
<p>I have some random variables using <code>scipy.stats</code> as follows:</p>

<pre><code>import scipy.stats as st
x1 = st.uniform()
x2 = st.uniform()
</code></pre>

<p>Now I would like make another random variable based on previous random variables and make some calculations like <code>var</code> for the new random variable. Assume that I want the new random variable to be something like <code>max(2, x1) + x2</code>. How can I define this dynamically?</p>

<p>Not directly, I think. However, this approach might be of use to you.</p>

<p>Assume to begin with that you know either the pdf or the cdf of the function of the random variables of interest. Then you can use rv_continuous in scipy.stats to calculate the variance and other moments of that function using the recipe offered at <a class='doc-link' href="http://stackoverflow.com/documentation/scipy/6873/rv-continuous-for-distribution-with-parameters#t=201610161945055670237">SO doc</a>. (Incidentally, someone doesn't like it for some reason. If you think of improvements, please comment.)</p>

<p>Obviously the 'fun' begins here. Usually you would attempt to define the cdf. For any given value of the random variable this is the probability that an expression such as the one you gave is not more than the given value. Thus determining the cdf reduces to solving a (an infinite) collection of inequalities in two variables. Of course there is often a strong pattern that greatly reduces the complexity and difficulty of performing this task.</p>

python random scipy