I have a piecewise linear function $f$ and I'd like to calculate the probability density or cumulative distribution function of $f(X)$, where $X$ has uniform distribution over the regions where $f$ is defined. For example,
$f(x)=\begin{cases} 6x+1 & 0\leq x < 1 \\ -3x+10 & 1\leq x < 2 \end{cases} $
Let me emphasize that this function is not a probability density. I want to obtain the density of $f(X)$. I don't care about $f$ outside of the limits given, which is why I haven't specified an otherwise condition.
I'm able to obtain the density of $f(X)$ empirically. I do this by generating a million numbers $x_1, x_2,\ldots, x_{10^6}$ uniformly from $0$ to $2$ and then creating the histogram of the $f(x_i)$. However, I'd like an analytical solution, for any piecewise function.
I'm going to implement this solution as an algorithm, so if the solution is best explained as a program, even better.
If the piecewise linear function $f$ is defined on an interval, and $X$ is chosen uniformly at random over that interval, then $f(X)$ will have the distribution of a finite mixture of uniform random variables. The density of $f(X)$ will look like a stack of blocks, which is not that easy to specify analytically, though you should be able to code up an algorithm to compute the density.
Suppose the subintervals where the pieces of $f$ are being defined are $[a_1,a_2]$ and $[a_2, a_3]$, etc, assumed disjoint. If the randomly chosen $X$ lands in the first of these subintervals, say subinterval $[a_1,a_2]$, it will be transformed linearly, so the result will have a uniform distribution with endpoints corresponding to a new interval whose endpoints will be $f_1(a_1)$ and $f_1(a_2)$, where $f_1$ is the equation of the line defined on subinterval $[a_1, a_2]$. Similarly if $X$ lands in the second subinterval, the result will be uniform over the interval with endpoints $f_2(a_2)$ and $f_2(a_3)$. You will need to swap the order of these endpoints if the linear function has a negative slope over the subinterval.
The end result, the density for $f(X)$, will be a mixture of the densities of these new uniform variables. Now a single uniform distribution has density $1/(\text{width of interval})$, which is easy to code because it's basically a rectangular block. However, if you have a mixture of these you need to scale the height of each block according to the relative width of the starting subinterval. So you have to scale the output uniform density arising from starting subinterval $[a_1,a_2]$ by the fraction of time that $X$ would land in $[a_1, a_2]$, and similarly scale the output density from the second subinterval by the fraction of time that $X$ would land in $[a_2, a_3]$. In the simple case where the starting subintervals all have the same length, the density of $f(X)$ will be the average of the individual output uniform densities.
In your example, the subintervals are $[a_1,a_2]=[0,1]$ and $[a_2,a_3]=[1,2]$. These get mapped to a uniform distribution over $[1,7]$ and a uniform distribution over $[4,7]$ respectively. The corresponding densities would be rectangular blocks with widths $6$ and $3$. Their heights would be $1/6$ and $1/3$ respectively. The mixture weights would be $1/2$ and $1/2$ respectively because the two subintervals have equal length, which changes the block heights to $1/12$ and $1/6$ respectively. The final density of $f(X)$ is the pointwise sum of these two block functions.