來(lái)源 標(biāo)準(zhǔn)差 Standard Deviation
期望值 Expected Values: 一個(gè)離散隨機(jī)變量X的期望值,基本上就是平均值(μ). 我們也可以稱這是X的數(shù)學(xué)期望(或簡(jiǎn)稱期望).
感覺(jué)他說(shuō)得不對(duì),期望是各個(gè)可能出現(xiàn)的結(jié)果乘以對(duì)于的概率的值的和.
基本上hackerrank這里寫得不能再模糊了,我完全不明白
"The expected value of a discrete random variable, , is more or less another way of referring to the mean (). We can also refer to this as the mathematical expectation (or just the expectation) of . "
查了維基, 大概有點(diǎn)概念
"是試驗(yàn)中每次可能的結(jié)果乘以其結(jié)果概率的總和"
例如浪慌,擲一枚公平的六面骰子,其每次“點(diǎn)數(shù)”的期望值是3.5萝快,計(jì)算如下:
方差 Variance δ2: 方差是從離散隨機(jī)變量X的期望μ中得到的X的值波動(dòng)程度的一種平均度量. 你可以認(rèn)為它是隨機(jī)變量值和平均值的差的期望(求平均值).給定一個(gè)n個(gè)元素的數(shù)據(jù)集X:
其中xi是S的第i個(gè)元素而μ是S所有元素的平均值.
標(biāo)準(zhǔn)差 δ
標(biāo)準(zhǔn)差量化一個(gè)數(shù)據(jù)集中變化波動(dòng)的總和, 給定一個(gè)n元素的數(shù)據(jù)集X:
其中xi是S的第i個(gè)元素而μ是S所有元素的平均值.
目標(biāo)
給定一個(gè)含N個(gè)整數(shù)的數(shù)組X, 計(jì)算和打印出其標(biāo)準(zhǔn)差, 你的答案應(yīng)該精確到小數(shù)點(diǎn)后一位(例如,12.3). 標(biāo)準(zhǔn)差的允許誤差(error)在±0.1之間.
輸入格式
第一行是一個(gè)整數(shù)N,表示輸入數(shù)組的元素?cái)?shù)量.
第二行包含N個(gè)空格分隔的整數(shù),表示數(shù)組中的各個(gè)元素
約束條件
- 5 <= N <= 100
- 0 < xi <= 105, xi是數(shù)組X中第i個(gè)元素.
輸出格式
輸出標(biāo)準(zhǔn)差到新的一行中, 精確到小數(shù)點(diǎn)后一位(例如:12.3)
示例輸入
5
10 40 30 50 20
示例輸出
14.1
示例解析
第一, 我們找到平均值
- (x0 - μ)2 = (10 - 30)2 = 400
- (x1 - μ)2 = (40 - 30)2 = 100
- (x2 - μ)2 = (30 - 30)2 = 0
- (x3 - μ)2 = (50 - 30)2 = 400
- (x4 - μ)2 = (20 - 30)2 = 100
現(xiàn)在我們計(jì)算方差∑N-1i=0(xi - μ)2 = 400 + 100 + 0 + 400 + 100 = 1000, so:
最后舍入到小數(shù)點(diǎn)后一位, 答案是14.1
python
def standard_deviation():
n = int(input())
X = [int(x) for x in input().split(" ")]
mean = sum(X) / n
print("{0:.1f}".format(sqrt(sum([(x - mean) ** 2 for x in X]) / n)))
if __name__ == '__main__':
standard_deviation()
java
import java.io.*;
import java.util.*;
import java.util.stream.*;
public class Solution {
public static void main(String[] args) {
try (Scanner scan = new Scanner(System.in)) {
int n = scan.nextInt();
scan.nextLine();
int[] X = Stream.of(scan.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
final double mean = IntStream.of(X).average().getAsDouble();
System.out.printf("%.1f\n", Math.sqrt(IntStream.of(X).mapToDouble(i -> (i - mean) * (i - mean)).sum() / (double) X.length));
}
}
}
scala
object Solution {
def main(args: Array[String]) {
import java.util.Scanner
val scan: Scanner = new Scanner(System.in)
try {
val n: Int = scan.nextInt
scan.nextLine()
val X: Array[Int] = scan.nextLine().split(" ").map(_.toInt).toArray
val mean: Double = X.sum / X.length.toDouble
printf("%.1f\n", Math.sqrt(X.map(x => (x - mean) * (x - mean)).sum / X.length))
} finally {
scan.close()
}
}
}