Forum

> > CS2D > Scripts > Function that evaluates outliers
Forums overviewCS2D overview Scripts overviewLog in to reply

English Function that evaluates outliers

5 replies
To the start Previous 1 Next To the start

old Function that evaluates outliers

The Dark Shadow
User Off Offline

Quote
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

old Re: Function that evaluates outliers

Bowlinghead
User Off Offline

Quote
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

old Re: Function that evaluates outliers

VADemon
User Off Offline

Quote
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?
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview