# Problem with NumericalDerivative, returning zeros

(Antoine Cailliau) #1

Hi,

I’m trying to use numerical differentiation. Although it works with `Math.Sin`, it appears it is not functioning with my empirical PDF function. Here is the code:

``````var whiteNoise = Generate.Normal(1000, mean: 10.0, standardDeviation: 2.0);
// Console.WriteLine(string.Join(",", whiteNoise));

var ecdf = Statistics.EmpiricalCDFFunc(whiteNoise);
var data = Generate.LinearRangeMap(step: 1, start: 3.0, stop: 17.0, map: ecdf);
// Console.WriteLine(string.Join(",", data));

var derivative = new NumericalDerivative();
var epdf = derivative.CreateDerivativeFunctionHandle(ecdf, 1);
data = Generate.LinearRangeMap(step: 1, start: 3.0, stop: 17.0, map: epdf);
Console.WriteLine(string.Join(",", data));
``````

It outputs a list of zeros. I’ve checked with R, and it should not return zeros. The expected values are something like this: `NA 0.0030 0.0155 0.0360 0.0585 0.1140 0.1780 0.1920 0.1705 0.1210 0.0660 0.0310 0.0105 0.0030 NA`

However, I’m not 100% sure that I’m using the library correctly.

Thanks in advance for the help

Antoine

(Christoph Rüegg) #2

I don’t think this approach can work: the empirical CDF is a step function, which is not numerically differentiable in a meaningful way (i.e. the derivative is zero, except at the 1000 points where it is undefined). Perhaps a histogram comes closer to what you’re looking for?

``````var histogram = new Histogram(whiteNoise, 13);
var density = new double[histogram.BucketCount];
for (int i = 0; i < density.Length; i++)
{
density[i] = histogram[i].Count/(double)histogram.DataCount;
}``````

(Antoine Cailliau) #3

Yes. Thank you very much, that’s indeed what I was looking for.