5 kyu

Mean without outliers

671 of 775kingcobra

Description:

The mean and standard deviation of a sample of data can be thrown off if the sample contains one or many outlier(s) :

(image source)

For this reason, it is usually a good idea to check for and remove outliers before computing the mean or the standard deviation of a sample. To this aim, your function will receive a list of numbers representing a sample of data. Your function must remove any outliers and return the mean of the sample, rounded to two decimal places (round only at the end).

Since there is no objective definition of "outlier" in statistics, your function will also receive a cutoff, in standard deviation units. So for example if the cutoff is 3, then any value that is more than 3 standard deviations above or below the mean must be removed. Notice that, once outlying values are removed in a first "sweep", other less extreme values may then "become" outliers, that you'll have to remove as well!

Example :

sample = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100]
cutoff = 3
clean_mean(sample, cutoff)  5.5
# R uses sam instead of sample to avoid conflicts with the
# base function sample()
sam <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100)
cutoff <- 3
clean_mean(sam, cutoff)
[1] 5.5

Formula for the mean :

(where n is the sample size)

Formula for the standard deviation :

(where N is the sample size, xi is observation i and x̄ is the sample mean)

Note : since we are not computing the sample standard deviation for inferential purposes, the denominator is n, not n - 1.

Recursion
Statistics
Algorithms
Data Science

Stats:

CreatedJul 10, 2017
PublishedJul 10, 2017
Warriors Trained1571
Total Skips75
Total Code Submissions10555
Total Times Completed775
Python Completions671
R Completions117
Total Stars60
% of votes with a positive feedback rating92% of 106
Total "Very Satisfied" Votes90
Total "Somewhat Satisfied" Votes15
Total "Not Satisfied" Votes1
Total Rank Assessments7
Average Assessed Rank
5 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • kingcobra Avatar
  • Voile Avatar
  • mentalplex Avatar
Ad