Density estimation from ECDF - numerical derivatives and scaled domains Suppose we want to get a density estimate of some data X. One way is to compute the empirical CDF,
N <- 1e5
x <- seq(min(X), max(X), length=N)
F <- ecdf(X)(x)

and then the density, by taking the derivative of F.
Question 1
Suppose the density
D <- diff(X, lag=1) / diff(x, lag=1)

Then the area under the density A <- sum(D) * diff(x)[1] will equal 1 (here assuming that x is an evenly spaced vector). However it may be desirable to smooth the estimate by taking a lag value greater than 1. e.g.
lg <- 50
D <- diff(X, lag=lg) / diff(x, lag=lg)

What do we need to do to D such that A <- sum(D) * diff(x)[1] is equal to unity? Namely, when taking lg > 1, how do we maintain that D is a density.
Question 2
Supposing x is the natural domain of our observable, we may instead want to consider a re-scaled domain. This can be done in two ways:

*

*Take F on the natural domain x and then graph xp vs. D where xp is for instance a constant re-scaling of x: xp <- x / max(X).

*Take F on the re-scaled domain and proceed as before with density estimation. e.g. F <- ecdf(X / max(X))(x) where x <- seq(0, 1, length=N).

I am interested in the second point, but have essentially the same problem as question 1. In point 2 we introduce a change of variables, so D should be multiplied by something to account for this. It seems to make sense on paper, but I'm not sure how to implement numerically - plus, a constant re-scaling seems too trivial to require computing inverse functions etc. Bonus points if you can combine Q1 and Q2.
 A: In comments, you say that your goal is to compare whether two cdfs are equal after scaling. For this goal, there is no need to compute densities, and the numerical procedures will be more stable without them.
Given a cdf $F$, we can compute the cdf for a rescaled variable by $$G(x) = F(m+sx)$$
where $m$ is some measure of central tendency and $s$ is some measure of dispersion. We could use

*

*$m$ as the population mean and $s$ as the population standard deviation, if $F$ was constructed from a population as an empirical cdf

*$m$ as the median ($F^{-1}(\frac12)$) and $s$ as half of the interquartile range ($F^{-1}(\frac34)-F^{-1}(\frac14)$), if $F$ was presented in some other way and robust statistics are appropriate

*$m$ as the mean and $s$ as the standard deviation calculated directly from $F$, as described at this question. If the distribution has minimum $a$ and maximum $b$, then we can write these as $m=b - \int_a^b F(x)dx$, $s=\sqrt{v}$, $v=b^2-\int_a^b 2xF(x)dx -m^2$.

Once we have two rescaled cdfs, $G_1$ and $G_2$, we can test their equality via a Kolmogorov-Smirnov test. This test uses a statistic like $\max |G_1(x)-G_2(x)|$, and again does not require the density.
