-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
96 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package math | ||
|
||
import ( | ||
"math" | ||
"sort" | ||
) | ||
|
||
// Calculate the median of a slice of floats | ||
func median(data []float64) float64 { | ||
sort.Float64s(data) | ||
n := len(data) | ||
if n%2 == 0 { | ||
return (data[n/2-1] + data[n/2]) / 2 | ||
} | ||
return data[n/2] | ||
} | ||
|
||
// Calculate the mean of a slice of floats | ||
func mean(data []float64) float64 { | ||
sum := 0.0 | ||
for _, value := range data { | ||
sum += value | ||
} | ||
return sum / float64(len(data)) | ||
} | ||
|
||
// Remove outliers using the MAD method and calculate the mean of the remaining data | ||
func removeOutliersAndMean(data []float64) float64 { | ||
if len(data) == 0 { | ||
return 0 | ||
} | ||
|
||
med := median(data) | ||
|
||
// Calculate absolute deviations from the median | ||
absDevs := make([]float64, len(data)) | ||
for i, value := range data { | ||
absDevs[i] = math.Abs(value - med) | ||
} | ||
|
||
// Calculate the median of the absolute deviations | ||
mad := median(absDevs) | ||
|
||
// Define a threshold using the MAD; here we use 3 times the MAD | ||
threshold := 3.0 * mad | ||
|
||
// Filter out outliers | ||
filteredData := make([]float64, 0) | ||
for _, value := range data { | ||
if math.Abs(value-med) <= threshold { | ||
filteredData = append(filteredData, value) | ||
} | ||
} | ||
|
||
// Calculate the mean of the remaining data | ||
return mean(filteredData) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package math | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestMean(t *testing.T) { | ||
data := []float64{1, 2, 2, 2, 3, 10, 2, 2, 1, 2, 3, 2, 100} | ||
fmt.Printf("Original data: %v\n", data) | ||
result := removeOutliersAndMean(data) | ||
fmt.Printf("Mean after removing outliers: %f\n", result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters