Hypothesis Testing Probability Density Estimates Is there a good way to test an probability density estimate against observed data? I know there exist various approaches to probability density estimation and testing individual parameters of density estimates against observed data, but do not know of any approach to test a general function against data. Would some approach where the function estimate and the data are treated as histograms work?
Specifically, could one just bin the estimate into a histogram and build confidence regions of Dirichlet parameters using the multinomial distribution expression given observed data?
 A: One general method of testing data against a particular probability CDF (with known parameters) is the Kolmogorov-Smirnov goodness-of-fit test.
Suppose we have $n = 2000$ observations from $\mathsf{Gamma}(5, .2)$ in the vector x as follows. [The shape parameter is $5$
and the rate parameter is $0.2.]$
set.seed(121)
x = rgamma(2000, 5, .2)
summary(x)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  2.551  16.630  23.469  25.020  31.425  89.405 

hdr="n=2000: Sample from BETA(5, .2) with Density"
hist(x, prob=T, br=35, ylim=c(0,.045), col="wheat", main=hdr)
 curve(dgamma(x, 5, .2), add=T, col="blue", lwd=2)


The mean of this distribution is $\mu = 5/.2 = 25,$ which
is nearly matched by the sample mean, and the population
median is $\eta = 23.35454,$ which is nearly matched by
the sample median 23.57. Similarly, the population and
sample upper quartiles are not far apart. So it seems
feasible that x was sampled from $\mathsf{Gamma}(5, .2).$
qgamma(.5, 5, .2)
[1] 23.35454
qgamma(.75, 5, .20)
[1] 31.37215

Perhaps most important, the K-S test does not reject the sample x as coming from the population $\mathsf{Gamma}(5, .2).$
ks.test(x, pgamma, 5, .2)

        One-sample Kolmogorov-Smirnov test

data:  x
D = 0.013078, p-value = 0.8837
alternative hypothesis: two-sided

The test statistic $D$ is the maximum vertical distance between
the CDF of the distribution [dashed blue] in the figure below) and the empirical CDF (ECDF) of the
the sample [orange].
hdr = "n==2000: GAMMA(5,.2) CDF (orange) and Sample ECDF"
plot(ecdf(x), col="orange", main = hdr)
 curve(pgamma(x, 5, .2), add=T, col="blue", lwd=2, lty="dashed")


However, the K-S test does reject the null hypothesis that x was sampled at random from $\mathsf{Gamma}(4, .16),$ which also has $\mu=25.$
ks.test(x, pgamma, 4, .16)$p.val
[1] 0.006478847

Note: If data may be normal, then the Shapiro-Wilk test will tend not to reject a sample from any normal distribution regardless of its mean or variance, and to reject non-normal samples.
shapiro.test(rnorm(50, 100, 15))$p.val
[1] 0.3374453

