I want to calculate Cumulative distribution function of Y, when Y is a Random Variable (Y=min{X,X'}) and X is a piecewise defined function Random variable. CDF of X is something like this code:
if (0<x<A) then FX = B(x)
else if (A<x<A^2) then FX = D(x)
I know that FY=1-((1-FX)^2). But it doesn't work when X is a piecewise defined function. How can I calculate FY?
Related
The goal is to compute the density function of a sum of n IID random variables via the density function of one of these random variables by:
Transforming the density function into the characteristic function via fft
Raise the characteristic function to the n
Transform the resulting characteristic function into the density function of interest via fft(inverse=TRUE)
The below is my naive attempt at this:
sum_of_n <- function(density, n, xstart, xend, power_of_2)
{
x <- seq(from=xstart, to=xend, by=(xend-xstart)/(2^power_of_2-1))
y <- density(x)
fft_y <- fft(y)
fft_sum_of_y <- (fft_y ^ n)
sum_of_y <- Re(fft(fft_sum_of_y, inverse=TRUE))
return(sum_of_y)
}
In the above, density is an arbitrary density function: for example
density <- function(x){return(dgamma(x = x, shape = 2, rate = 1))}
n indicates the number of IID random variables being summed. xstart and xend are the start and end of the approximate support of the random variable. power_of_2 is the power of 2 length for the numeric vectors used. As I understand things, lengths of powers of two increase the efficiency of the fft algorithm.
I understand at least partially why the above does not work as intended in general. Firstly, the values themselves will not be scaled correctly, as fft(inverse=TRUE) does not normalize by default. However, I find that the values are still not correct when I divide by the length of the vector i.e.
sum_of_y <- sum_of_y / length(sum_of_y)
which based on my admittedly limited understanding of fft is the normalizing calculation. Secondly, the resulting vector will be out of phase due to (someone correct me on this if I am wrong) the shifting of the zero frequency that occurs when fft is performed. I have tried to use, for example, pracma's fftshift and ifftshift, but they do not appear to address this problem correctly. For symmetric distributions e.g. normal, this is not difficult to address since the phase shift is typically exactly half, so that an operation like
sum_of_y <- c(sum_of_y[(length(y)/2+1):length(y)], sum_of_y[1:(length(y)/2)])
works as a correction. However, for asymmetric distributions like the gamma distribution above this fails.
In conclusion, are there adjustments to the code above that will result in an appropriately scaled and appropriately shifted final density function for the IID sum?
Does anyone know the algorithm to generate random variable from $F(x) = \Pi_{i=1}^n F_i(x)$ distribution where $i=1,2,..,n$ in R?
That's equivalent to saying Pr{X1 <= x & X2 <= x & ... & Xn <= x} for independent X's. Generate from each of the Fi's independently, and take the max. Independent generation gives you an ensemble meeting the individual distribution requirements, and taking the max meets the requirement of the joint probability statement.
In the special case where the Fi's are invertible and are the same for all i, this can be done with a single inverse transformation. Let's call Fi(x) === G(x) to avoid tripping over notation. Then F(x) = Gn(x), and by the inverse transform theorem Gn(X) is distributed uniformly between 0 and 1 for random variable X. Therefore, for U ~ uniform(0,1) and starting with your statement about F(x):
F(X) = Gn(X) = U
G(X) = U1/n
X = G-1(U1/n)
This gives a one-step method for generating the max of n independent and identically distributed random variates X, as opposed to having to generate n of them and take the max.
Beginner in R, I performs splinefun function on (x,y) values. I am searching to get the derivative of the function in x and the interpolated y values by the function. Also, I try to constrain the function to be >0.
Maybe someone already asked these questions ?
I performed the splinefun function, and have the impression that the function is not "smoothing" the values of observed (x,y) but passes exactly through these points. Is it how splinefun interpolates the values, or is there a way to constrain the function to "smooth" the (x,y) cloud?
With what I did, I always have y interpolated = y observed/measured.
Is there something to see with the "method" for the interpolation ("fmm", "monoH.FC",...), or with the "ties" argument?
I also tried to get the first derivative, thanks to the argument "f(x,deriv=1)", but I am not sure this is the right way to do that.
example of the code:
x <- c(1,8,14,21,28,35,42,65)
y <- c(65,30,70,150,40,0,15,0)
splinefun(x, y, method="fmm", ties=mean)
deriv <- f(x, deriv=1) #Get the first derivative
y <- f(x) # Get the interpolated y
I am searching how to :
1) Force the interpolated function to return a function with values for y only >0 (is there an argument in the splinefun function which handle that ?)
2) Get the values of the derivative of the interpolated function for the x (used for the interpolation)
3) Get the values of the y interpolated (different than the y that was used to perform the interpolation)
Thanks a lot for your help.
I want to compute the expected value of a multivariate function f(x) wrt to dirichlet distribution. My problem is "penta-nomial" (i.e 5 variables) so calculating the explicit form of the expected value seems unreasonable. Is there a way to numerically integrate it efficiently?
f(x) = \sum_{0,4}(x_i*log(n/x_i))
x = <x_0, x_1, x_2, x_3, x_4> and n is a constant
I want to turn a continuous random variable X with cdf F(x) into a continuous random variable Y with cdf F(y) and am wondering how to implement it in R.
For example, perform a probability transformation on data following normal distribution (X) to make it conform to a desirable Weibull distribution (Y).
(x=0 has CDF F(x=0)=0.5, CDF F(y)=0.5 corresponds to y=5, then x=0 corresponds to y=5 etc.)
There are many built in distribution functions, those starting with a 'p' will transform to a uniform and those starting with a 'q' will transform from a uniform. So the transform in your example can be done by:
y <- qweibull( pnorm( x ), 2, 6.0056 )
Then just change the functions and/or parameters for other cases.
The distr package may also be of interest for additional capabilities.
In general, you can transform an observation x on X to an observation y on Y by
getting the probability of X≤x, i.e. FX(x).
then determining what observation y has the same probability,
I.e. you want the probability Y≤y = FY(y) to be the same as FX(x).
This gives FY(y) = FX(x).
Therefore y = FY-1(FX(x))
where FY-1 is better known as the quantile function, QY. The overall transformation from X to Y is summarized as: Y = QY(FX(X)).
In your particular example, from the R help, the distribution functions for the normal distribution is pnorm and the quantile function for the Weibull distribution is qweibull, so you want to first of all call pnorm, then qweibull on the result.