Skip to content

Commit

Permalink
Improved quantile
Browse files Browse the repository at this point in the history
  • Loading branch information
arguiot committed Sep 9, 2022
1 parent d00bca4 commit 59ac707
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
4 changes: 4 additions & 0 deletions Euler.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
68C12AD12483284800BB4B3A /* Engineering.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68C12AD02483284800BB4B3A /* Engineering.swift */; };
68C12AD32483392C00BB4B3A /* Substring.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68C12AD22483392C00BB4B3A /* Substring.swift */; };
68C12AD524833E6500BB4B3A /* TablesTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68C12AD424833E6500BB4B3A /* TablesTest.swift */; };
68F6882C28CA9BB9009806F2 /* StatisticsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68F6882B28CA9BB9009806F2 /* StatisticsTests.swift */; };
OBJ_176 /* Expression.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_10 /* Expression.swift */; };
OBJ_177 /* Grouper.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_12 /* Grouper.swift */; };
OBJ_178 /* LaTeX.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_13 /* LaTeX.swift */; };
Expand Down Expand Up @@ -153,6 +154,7 @@
68C12AD02483284800BB4B3A /* Engineering.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Engineering.swift; sourceTree = "<group>"; };
68C12AD22483392C00BB4B3A /* Substring.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Substring.swift; sourceTree = "<group>"; };
68C12AD424833E6500BB4B3A /* TablesTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TablesTest.swift; sourceTree = "<group>"; };
68F6882B28CA9BB9009806F2 /* StatisticsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatisticsTests.swift; sourceTree = "<group>"; };
"Euler::Euler::Product" /* Euler.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = Euler.framework; sourceTree = BUILT_PRODUCTS_DIR; };
"Euler::EulerTests::Product" /* EulerTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; path = EulerTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
OBJ_10 /* Expression.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Expression.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -593,6 +595,7 @@
OBJ_106 /* NodeTests.swift */,
OBJ_107 /* Tools */,
68C12AD424833E6500BB4B3A /* TablesTest.swift */,
68F6882B28CA9BB9009806F2 /* StatisticsTests.swift */,
);
name = EulerTests;
path = Tests/EulerTests;
Expand Down Expand Up @@ -787,6 +790,7 @@
OBJ_270 /* ExtensionsTests.swift in Sources */,
OBJ_271 /* GeneratorsTests.swift in Sources */,
OBJ_272 /* GeometryTests.swift in Sources */,
68F6882C28CA9BB9009806F2 /* StatisticsTests.swift in Sources */,
OBJ_273 /* MatrixTest.swift in Sources */,
OBJ_274 /* NodeTests.swift in Sources */,
OBJ_275 /* Convenience.swift in Sources */,
Expand Down
8 changes: 4 additions & 4 deletions Sources/Euler/Statistics/Quantile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public extension Statistics {
let half = Int(floor(Double(sorted.count) / 2.0))

if self.list.count % 2 == 0 {
return sorted[half]
} else {
return (sorted[half - 1] + sorted[half]) / 2
} else {
return sorted[half]
}
}

Expand All @@ -49,8 +49,8 @@ public extension Statistics {
var array = self.list
let n = array.removeFirst()
let sorted = array.sorted()
let index = (sorted.count - 1) * n
guard let floored = floor(index).asInt() else { throw QuantileError.ArrayLength }
let index = Double(sorted.count - 1) * percentage
let floored = Int(index)
let diff = index - BigDouble(floored)

if index + 1 >= BigDouble(sorted.count) {
Expand Down
25 changes: 25 additions & 0 deletions Tests/EulerTests/StatisticsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// StatisticsTests.swift
// EulerTests
//
// Created by Arthur Guiot on 08/09/2022.
//

import XCTest
import Euler
class StatisticsTests: XCTestCase {
func testQuantile() {
let array: [BN] = [32.2, 32.0, 30.4, 31.0, 31.2, 31.2, 30.3, 29.6, 30.5, 30.7]

let stats = Statistics(list: array)

XCTAssertEqual(stats.median, BN(617, over: 20))
XCTAssertEqual(try stats.quantile(percentage: 0.5), 30.7)
XCTAssertEqual(try stats.quantile(percentage: 0.25), 30.4)
XCTAssertEqual(try stats.quantile(percentage: 0.75), 31.2)
}

static var allTests = [
("Quantile", testQuantile),
]
}

0 comments on commit 59ac707

Please sign in to comment.