Learn to build secure applications from the mindset of the hacker and avoid being exploited.

I have two tables of temperature destribution;

And I want to know if these two distributions are significally different;

Therefore I want to apply the Kolmogorov-Smirnov Test for two distributions,

can anyone adapt the test for my needs?

thanks,

see my attached code

Regi-es.pas

And I want to know if these two distributions are significally different;

Therefore I want to apply the Kolmogorov-Smirnov Test for two distributions,

can anyone adapt the test for my needs?

thanks,

see my attached code

Regi-es.pas

Do more with

EXPERT OFFICE^{®} is a registered trademark of EXPERTS EXCHANGE^{®}

Try this for more info on the test if you need it: http://www.princeton.edu/~achaney/tmve/wiki100k/docs/Kolmogorov-Smirnov_test.html

There is a Wikipedia article too, but I mind the math articles there to be a little overly complicated and hard to understand: http://en.wikipedia.org/wiki/Kolmogorov%E2%80%93Smirnov_test#Two-sample_Kolmogorov.E2.80.93Smirnov_test

```
// line between two points
// y = (x - x1) * (y2-y1)/(x2-x1) + y1
procedure CalcKSTest(data, dataOrig: T1DimArray; var fKSTemp: Double; var fDistance, fFreq, fFreqOrig: Double);
function GetCurrentCumulativeValue(d: T1DimArray; fT: Double): Double;
var
k, nMax_Orig: Integer;
fNextT, fNextFreq, fPrevT, fPrevFreq: Double;
begin
Result := 0;
fPrevT := 0;
fPrevFreq := 0;
//calc max cumulative value - original
nMax_Orig := 0;
for k:= Low(d) to High(d) do
begin
nMax_Orig := nMax_Orig + d[k].nCount;
end;
//sum freq. of temp. until fT temperature
for k:= Low(d) to High(d) do
begin
if d[k].fTemp <= fT then //until max fT
begin
fPrevT := d[k].fTemp;
Result := Result + d[k].nCount/nMax_Orig;
fPrevFreq := Result;
if fPrevT=fT then Break;
end
else
begin
//calc rest of interpolated line between temp les than fT and first greater then fT
if d[k].fTemp > fT then
begin
fNextT := fPrevT;
fNextFreq := fPrevFreq;
if k<High(d) then
begin
fNextT := d[k].fTemp;
fNextFreq := Result + d[k].nCount/nMax_Orig;
end;
//go interpolate to fT - line between two points
if fPrevT<>fNextT then
begin
Result := ((fT - fPrevT)*(fNextFreq - fPrevFreq)/(fNextT - fPrevT)) + fPrevFreq;
end;
end;
Break;
end;
end;
end;
var
i, nMax: Integer;
f, f_orig, fTemp: Double;
begin
fKSTemp :=0;
fDistance := 0;
fFreq := 0;
fFreqOrig := 0;
f := 0;
//calc max cumulative value
nMax := 0;
for i:= Low(data) to High(data) do
begin
nMax := nMax + data[i].nCount;
end;
//go for all temp. values from newer source
for i:= Low(data) to High(data) do
begin
f := f + data[i].nCount/nMax;
fTemp := data[i].fTemp;
//what is distance from orig?
//get cumulative freq. for fTemp in orig
f_orig := GetCurrentCumulativeValue(dataOrig, fTemp);
//get max distance
if Abs(f-f_orig)>Abs(fDistance) then
begin
//keep values
fDistance := f-f_orig;
fFreq := f;
fFreqOrig := f_orig;
fKSTemp := fTemp;
end;
end;
end;
```

... to represent visually add this to the bottom...

```
...
data, dataOrig: T1DimArray;
fKSTemp, fDist, fKSFreq, fKSFreqOrig: Double;
...
Series6.Clear;
Series6.LinePen.Color := clBlue;
CalcKSTest(data, dataOrig, fKSTemp, fDist, fKSFreq, fKSFreqOrig);
Series6.addxy(fKSTemp, fKSFreqOrig);
Series6.addxy(fKSTemp, fKSFreq);
...
```

... this will draw line between two data series where KS distance is a maximum.

You must decide what is maximum value for difference between two distributions where you assume that distributions are "similar". This value is a trigger when you must assume that "something is wrong".

If you know what is wrong here, write a descriptive pseudo-code then we can help you more.

## Premium Content

You need an Expert Office subscription to comment.Start Free Trial