This is an R question, but this can also be done in Java using the Riemann approximation
You need to define a Riemann interface
public interface Riemann extends
BiFunction<Function<Double,Double>,Integer,BinaryOperator<Double>>{}
Then you can use lambda calculus to implement the interface
int N=100000;
Riemann s = (f, n) -> (a, b) ->
IntStream.range(0, n).mapToDouble(i->f.apply(a + i*((b-a)/n))*((b-a)/n)).sum();
As an example we will calculate the probability of a Weibull random variable between 1/4 and 3/4 using shape parameter k=1.5
double k=1.5;
Optional<Double>weib=
Optional.of(s.apply(x->k*pow(x,k-1)*exp(-pow(x,k)), N).apply(0.25,0.75));
weib.ifPresent(System.out::println);
The result should be 0.36 or 36%. The advantage of using your own integral libraries instead of package libraries is that it helps to understand what is going on in the background.
density
todens<-density(df$x, from=0, to=24)
. But then when I'm caculatingintegrate(approxfun(dens), lower=0, upper=24)
I'm not getting the "full" probability (1) I was expected to get. Is there a way to limit my density function such that I'll get what I'm expecting to get? – Pinball