Last active
December 17, 2015 21:29
-
-
Save linqing/5675490 to your computer and use it in GitHub Desktop.
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
package math | |
object Statistics1 { | |
// 中位数(又称中值,英语:Median),统计学中的专有名词,代表一个样本、种群或概率分布中的一个数值, | |
// 其可将数值集合划分为相等的上下两部分。对于有限的数集,可以通过把所有观察值高低排序后找出正中间的一个作为中位数。 | |
// 如果观察值有偶数个,则中位数不唯一,通常取最中间的两个数值的平均数作为中位数。 | |
def median(xs: Vector[Double]): Double = xs(xs.size / 2) | |
//Quartile(四分位数): | |
// 第0个Quartile实际为通常所说的最小值(MINimum) | |
// 第1个Quartile(En:1st Quartile) | |
// 第2个Quartile实际为通常所说的中分位数(中数,二分位分,中位数:Median) | |
// 第3个Quartile(En:3rd Quartile) | |
// 第4个Quartile实际为通常所说的最大值(MAXimum) | |
def quartiles(xs: Vector[Double]): (Double, Double, Double) = | |
(xs(xs.size / 4), median(xs), xs(xs.size / 4 * 3)) | |
// 内距IQR即Inter-Quartile Range, 这是统计技术上的名词。 | |
// 内距又称为四分位差,是两个四分位数之差,即内距IQR=高四分位数—低四分位数。 | |
def iqr(xs: Vector[Double]): Double = quartiles(xs) match { | |
case (lowerQuartiles, _, upperQuartiles) => upperQuartiles - lowerQuartiles | |
} | |
// 算数平均数 | |
def mean(xs: Vector[Double]): Double = { | |
xs.reduce(_ + _) / xs.size | |
} | |
} | |
object Statistics2 { | |
trait NumberLike[A] { | |
def get: A | |
def plus(y: NumberLike[A]): NumberLike[A] | |
def minus(y: NumberLike[A]): NumberLike[A] | |
def divide(y: Int): NumberLike[A] | |
} | |
type Quartile[A] = (NumberLike[A], NumberLike[A], NumberLike[A]) | |
// 中位数(又称中值,英语:Median),统计学中的专有名词,代表一个样本、种群或概率分布中的一个数值, | |
// 其可将数值集合划分为相等的上下两部分。对于有限的数集,可以通过把所有观察值高低排序后找出正中间的一个作为中位数。 | |
// 如果观察值有偶数个,则中位数不唯一,通常取最中间的两个数值的平均数作为中位数。 | |
def median[A](xs: Vector[NumberLike[A]]): NumberLike[A] = xs(xs.size / 2) | |
//Quartile(四分位数): | |
// 第0个Quartile实际为通常所说的最小值(MINimum) | |
// 第1个Quartile(En:1st Quartile) | |
// 第2个Quartile实际为通常所说的中分位数(中数,二分位分,中位数:Median) | |
// 第3个Quartile(En:3rd Quartile) | |
// 第4个Quartile实际为通常所说的最大值(MAXimum) | |
def quartiles[A](xs: Vector[NumberLike[A]]): Quartile[A] = | |
(xs(xs.size / 4), median(xs), xs(xs.size / 4 * 3)) | |
// 内距IQR即Inter-Quartile Range, 这是统计技术上的名词。 | |
// 内距又称为四分位差,是两个四分位数之差,即内距IQR=高四分位数—低四分位数。 | |
def iqr[A](xs: Vector[NumberLike[A]]): NumberLike[A] = quartiles(xs) match { | |
case (lowerQuartiles, _, upperQuartiles) => upperQuartiles minus lowerQuartiles | |
} | |
// 算数平均数 | |
def mean[A](xs: Vector[NumberLike[A]]): NumberLike[A] = { | |
xs.reduce(_ plus _) divide xs.size | |
} | |
} | |
object Math { | |
trait NumberLike[T] { | |
def plus(x: T, y: T): T | |
def minus(x: T, y: T): T | |
def divide(x: T, y: Int): T | |
} | |
object NumberLike { | |
implicit object NumberLikeDouble extends NumberLike[Double] { | |
def plus(x: Double, y: Double) = x + y | |
def minus(x: Double, y: Double) = x - y | |
def divide(x: Double, y: Int) = x / y | |
} | |
implicit object NumberLikeInt extends NumberLike[Int] { | |
def plus(x: Int, y: Int) = x + y | |
def minus(x: Int, y: Int) = x - y | |
def divide(x: Int, y: Int) = x / y | |
} | |
} | |
} | |
object Statistics3 { | |
import Math.NumberLike | |
type Quartile[A] = (A, A, A) | |
// 中位数(又称中值,英语:Median),统计学中的专有名词,代表一个样本、种群或概率分布中的一个数值, | |
// 其可将数值集合划分为相等的上下两部分。对于有限的数集,可以通过把所有观察值高低排序后找出正中间的一个作为中位数。 | |
// 如果观察值有偶数个,则中位数不唯一,通常取最中间的两个数值的平均数作为中位数。 | |
def median[T](xs: Vector[T]): T = xs(xs.size / 2) | |
//Quartile(四分位数): | |
// 第0个Quartile实际为通常所说的最小值(MINimum) | |
// 第1个Quartile(En:1st Quartile) | |
// 第2个Quartile实际为通常所说的中分位数(中数,二分位分,中位数:Median) | |
// 第3个Quartile(En:3rd Quartile) | |
// 第4个Quartile实际为通常所说的最大值(MAXimum) | |
def quartiles[T](xs: Vector[T]): Quartile[T] = | |
(xs(xs.size / 4), median(xs), xs(xs.size / 4 * 3)) | |
// 内距IQR即Inter-Quartile Range, 这是统计技术上的名词。 | |
// 内距又称为四分位差,是两个四分位数之差,即内距IQR=高四分位数—低四分位数。 | |
def iqr[T](xs: Vector[T])(implicit ev: NumberLike[T]): T = quartiles(xs) match { | |
case (lowerQuartiles, _, upperQuartiles) => ev.minus(upperQuartiles, lowerQuartiles) | |
} | |
// 算数平均数 | |
// def mean[T](xs: Vector[T])(implicit ev: NumberLike[T]): T = { | |
// ev.divide(xs.reduce(ev.plus), xs.size) | |
// } | |
def mean[T](xs: Vector[T])(implicit ev: NumberLike[T]): T = | |
ev.divide(xs.reduce(ev.plus(_, _)), xs.size) | |
} | |
// 使用contenxt bounds | |
object Statistics4 { | |
import Math.NumberLike | |
type Quartile[A] = (A, A, A) | |
// 中位数(又称中值,英语:Median),统计学中的专有名词,代表一个样本、种群或概率分布中的一个数值, | |
// 其可将数值集合划分为相等的上下两部分。对于有限的数集,可以通过把所有观察值高低排序后找出正中间的一个作为中位数。 | |
// 如果观察值有偶数个,则中位数不唯一,通常取最中间的两个数值的平均数作为中位数。 | |
def median[T](xs: Vector[T]): T = xs(xs.size / 2) | |
//Quartile(四分位数): | |
// 第0个Quartile实际为通常所说的最小值(MINimum) | |
// 第1个Quartile(En:1st Quartile) | |
// 第2个Quartile实际为通常所说的中分位数(中数,二分位分,中位数:Median) | |
// 第3个Quartile(En:3rd Quartile) | |
// 第4个Quartile实际为通常所说的最大值(MAXimum) | |
def quartiles[T](xs: Vector[T]): Quartile[T] = | |
(xs(xs.size / 4), median(xs), xs(xs.size / 4 * 3)) | |
// 内距IQR即Inter-Quartile Range, 这是统计技术上的名词。 | |
// 内距又称为四分位差,是两个四分位数之差,即内距IQR=高四分位数—低四分位数。 | |
def iqr[T: NumberLike](xs: Vector[T]): T = quartiles(xs) match { | |
case (lowerQuartiles, _, upperQuartiles) => | |
implicitly[NumberLike[T]].minus(upperQuartiles, lowerQuartiles) | |
} | |
// 算数平均数 | |
// def mean[T](xs: Vector[T])(implicit ev: NumberLike[T]): T = { | |
// ev.divide(xs.reduce(ev.plus), xs.size) | |
// } | |
def mean[T: NumberLike](xs: Vector[T]): T = | |
implicitly[NumberLike[T]].divide(xs.reduce(implicitly[NumberLike[T]].plus(_, _)), xs.size) | |
} | |
object BigIntImplicits { | |
import Math.NumberLike | |
implicit object NumberLikeBigNInteger extends NumberLike[BigInt] { | |
def plus(x: BigInt, y: BigInt) = x + y | |
def minus(x: BigInt, y: BigInt) = x - y | |
def divide(x: BigInt, y: Int) = x / y | |
} | |
} | |
object LastTest extends App { | |
import Statistics4._ | |
import BigIntImplicits._ | |
val numbers: Vector[BigInt] = ( 1 to 10).toList.map(i => BigInt(i) ).toVector | |
println(mean(numbers)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment