﻿ Unreal Software - Thread: Function that evaluates outliers

# Forum

CS2D Scripts Function that evaluates outliers

# Function that evaluates outliers

5 replies
1

#### Function that evaluates outliers

User Offline

Yo, How to make a function that evaluates outliers? You may ask what outliers are, Outliers are numbers that are far from the average for example If we have [ 1,2,3,5,9,20 ] there 20 is outlier because it is far away from other numbers. Another example: If we have [ 7,11,30 ] 30 is outlier there.

Note this kind of outliers goes to statistics.

My idea: We need to compare all the numbers and the one that is more different from everything is superfluous.

That code has been written by my friend, It doesn't work at all but still. You may take some idea from it here it is.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
```function solution(...)
arg.n = nil

local tab = {}

local count = 0
local value = 0

for i, k in pairs(arg) do
for l, n in pairs(arg)do
if (i ~= l) then
count = count + 1
value = value + math.abs(k-n)
if (math.abs(k-n) > value/count) then
if (tab[k] ~= nil) then
tab[k] = tab[k] + 1
else
tab[k] = 1
end

if (tab[n] ~= nil) then
tab[n] = tab[n] + 1
else
tab[n] = 1
end
end
end
end
end

local max = 0
local excess

for i, k in pairs(tab) do
if (max < k) then
max = k
excess = i
end
end

return excess
end

print(solution(1,2,14,4,5,3))
--14
print(solution(100,99,88,78,1))
--1
```
It may still be not enough to understand what outliers really are, But you can find out more information about it on internet.

Would be appreciated
edited 2×, last 13.05.20 04:10:45 pm

#### Re: Function that evaluates outliers

User Offline

The solution for the one greatest outlier, I guess. Didnt do any research.
The function will return the index of the value!
Therefore the true outlier lays behind
myTable[GetOutlier(myTable)];
(yourTable=myTable for that regard)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
```--untested
function GetOutlier(table)

--Get Average
local average = 0;
for k,v in pairs(table) do
average = average + v;
end
average = average / #table;

-- Get "distance" relative to average for each number
local distanceBest = 0;
local indexBest = nil;
local distanceNow = 0;

for k,v in pairs(table) do
distanceNow = math.abs(v - average);
if (distanceNow > distanceBest) then -- a greater number has been found
indexBest = k;
distanceBest = distanceNow;
end
end

-- return
if (totype(indexBest) == "number")
return indexBest;
end
return "No Outliers found";
end
```
edited 1×, last 14.05.20 05:36:19 pm

#### Re: Function that evaluates outliers

User Offline

If you want to look up statistics, the proper terms is "deviation" or "standard deviation", "percentile".
There's an orthogonal approach to detecting outliers: "clustering". E.g you have unsorted elements and you want to put them in groups. But I don't think you need it here.

Though if I understand your description right, you only want Minimum/Maximum values to be determined as outliers?

#### Re: Function that evaluates outliers

User Offline

@ Bowlinghead: It wasn't working as expected, The result of
1
```print(GetOutlier({1,2,3,6}))
```
was
`4`
`6`
.

#### Re: Function that evaluates outliers

TrialAndError
User Offline

@ The Dark Shadow: It's returning the index instead of the number, so the 4th element in the table is 6