Note - I put piecewise in quotes because I don't think it's the right term to use (I can't figure out what to call it).
I am building a program to model the load that a user places on a server. The load a user produces follows a normal distribution. Depending on which application the user is using, however, the mean and variance of that normal distribution will be different. What I am trying to do is calculate the overall mean and variance for a user's activity given the proportions of time they use each application.
For example, Application A follows $\mathcal{N}(100, 50)$ and Application B follows $\mathcal{N}(500, 20)$. If a user uses A 50% of the time and B the other 50%, what is the mean and variance of the data that the user would produce during a day?
I'm able to simulate this by selecting a number from a uniform distribution between 0 and 1 and then generating a value from the appropriate distribution. Something like this:
$f(x) = \begin{cases} \mathcal{N}(100, 50), &0 \le x \lt 0.5\\ \mathcal{N}(500, 20), &0.5 \le x \lt 1\\ \end{cases} $
When I simulate a large number of these values and measure the results, it looks like the mean is just
$\sum\limits_{i=1}^n\mu_ip$
where $p$ is the percentage of the day a user is using each application.
I can't figure out what pattern the variance follows or what the formula might be to determine it without measuring a bunch of simulated values (When I simulate the above example, the variance looks to be something close to 41500).
I'd appreciate confirmation that how I'm calculating the combined mean is correct and some help in figuring out how to determine the variance of the overall distribution.
Let the two normal random variables be $X$ and $Y$, where $X$ is chosen with probability $p$, and $Y$ is chosen with probability $q=1-p$.
If $W$ is the resulting random variable, then $\Pr(W\le w)=p\Pr(X\le w)+q\Pr(Y\le w)$.
Differentiate. We get $f_W(w)=pf_X(w)+qf_Y(w)$.
The mean of $W$ is $\int_{-\infty}^\infty wf_W(w)$. Calculate. We get $$\int_{-\infty}^\infty w(pf_X(w)+qf_Y(w))\,dw.$$ This is $pE(X)+qE(Y)$, confirming your observation.
For the variance, we want $E(W^2)-(E(W))^2$. For $E(W^2)$, we calculate $\int_{-\infty}^{\infty} w^2(pf_X(w)+qf_Y(w))\,dw$. This is $pE(X^2)+qE(Y^2)$.
But $pE(X^2)= p(\text{Var}(X)+(E(X))^2)$ and $qE(Y^2)= q(\text{Var}(Y)+(E(Y))^2)$
Putting things together we get $$\text{Var}(W)=p\text{Var}(X)+q\text{Var}(Y)+ p(E(X))^2+q(E(Y))^2- (pE(X)+qE(Y))^2.$$
Remark: For a longer discussion, please look for Mixture Distributions.