Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added medianlow and medianHigh functions #4

Merged
merged 1 commit into from
Dec 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ This library is a collection of functions that perform statistical calculations
* [covarianceSample](#sample-covariance)
* [max](#max)
* [median](#median)
* [medianLow](#medianLow)
* [medianHigh](#medianHigh)
* [min](#min)
* [pearson](#pearson-correlation-coefficient)
* [standardDeviationPopulation](#population-standard-deviation)
Expand Down Expand Up @@ -126,6 +128,34 @@ Sigma.median([1, 12, 19.5, 3, -5])
// Result: 3
```

### Median Low

Returns the [median value](http://en.wikipedia.org/wiki/Median) from the array.

**Note**:

* Returns nil when the array is empty.
* Returns the lower of the two middle values if there is an even number of items in the array.

```Swift
Sigma.median([1, 12, 19.5, 10, 3, -5])
// Result: 3
```

### Median High

Returns the [median value](http://en.wikipedia.org/wiki/Median) from the array.

**Note**:

* Returns nil when the array is empty.
* Returns the higher of the two middle values if there is an even number of items in the array.

```Swift
Sigma.median([1, 12, 19.5, 10, 3, -5])
// Result: 10
```

### Sample variance

Computes [variance](http://en.wikipedia.org/wiki/Variance) based on a sample.
Expand Down Expand Up @@ -320,6 +350,7 @@ If you need help or want to extend the library feel free to create an issue or s
## Contributors

* [Thomas Fankhauser](https://github.com/southdesign)
* [John Clema](https://github.com/JohnClema)

## License

Expand Down
51 changes: 50 additions & 1 deletion SigmaSwiftStatistics/Sigma.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public struct Sigma {
let sorted = values.sort { $0 < $1 }

if count % 2 == 0 {
// Event number of items - return the mean of two middle values
// Even number of items - return the mean of two middle values
let leftIndex = Int(count / 2 - 1)
let leftValue = sorted[leftIndex]
let rightValue = sorted[leftIndex + 1]
Expand All @@ -121,6 +121,55 @@ public struct Sigma {
}
}

/**

Returns the central value from the array after it is sorted.

http://en.wikipedia.org/wiki/Median

- parameter values: Array of decimal numbers.
- returns: The median value from the array. Returns nil for an empty array. Returns the smaller of the two middle values if there is an even number of items in the array.

Example

Sigma.median([1, 12, 19.5, 3, -5]) // 3

*/
public static func medianLow(values: [Double]) -> Double? {
let count = Double(values.count)
if count == 0 { return nil }
let sorted = values.sort { $0 < $1 }

if count % 2 == 0 {
// Even number of items - return the lower of the two middle values
return sorted[Int(count / 2) - 1]
} else {
// Odd number of items - take the middle item.
return sorted[Int(count / 2)]
}
}

/**

Returns the central value from the array after it is sorted.

http://en.wikipedia.org/wiki/Median

- parameter values: Array of decimal numbers.
- returns: The median value from the array. Returns nil for an empty array. Returns the greater of the two middle values if there is an even number of items in the array.

Example

Sigma.median([1, 12, 19.5, 3, -5, ]) // 3

*/
public static func medianHigh(values: [Double]) -> Double? {
let count = Double(values.count)
if count == 0 { return nil }
let sorted = values.sort { $0 < $1 }
return sorted[Int(count / 2)]
}

/**

Computes variance based on a sample.
Expand Down
42 changes: 42 additions & 0 deletions SigmaSwiftStatisticsTests/SigmaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,48 @@ class SigmaTests: XCTestCase {
XCTAssert(result == nil)
}

// MARK: - Median Low
func testMedianLow_oddNumberOfItems() {
let result = Sigma.medianLow([1, 12, 19.5, 3, -5])!
XCTAssertEqual(3, result)
}

func testMedianLow_eventNumberOfItems() {
let result = Sigma.medianLow([1, 12, 19.5, 3, -5, 8])!
XCTAssertEqual(3, result)
}

func testMedianLow_oneItem() {
let result = Sigma.medianLow([2])!
XCTAssertEqual(2, result)
}

func testMedianLow_whenEmpty() {
let result = Sigma.medianLow([])
XCTAssert(result == nil)
}

//MARK: - Median High
func testMedianHigh_oddNumberOfItems() {
let result = Sigma.medianHigh([1, 12, 19.5, 3, -5])!
XCTAssertEqual(3, result)
}

func testMedianHigh_eventNumberOfItems() {
let result = Sigma.medianHigh([1, 12, 19.5, 3, -5, 8])!
XCTAssertEqual(8, result)
}

func testMedianHigh_oneItem() {
let result = Sigma.medianHigh([2])!
XCTAssertEqual(2, result)
}

func testMedianHigh_whenEmpty() {
let result = Sigma.medianHigh([])
XCTAssert(result == nil)
}

// MARK: - Sample variance

func testVarianceSample() {
Expand Down