Z-分数计算器 - 计算数据点的标准分数
计算任何数据点的z分数(标准分数),确定其相对于均值的位置,使用标准差。非常适合统计分析和数据标准化。
文档
Z-Score 计算器
介绍
z-score(或标准分数)是一种统计测量,描述一个值与一组值的均值之间的关系。它表示一个元素与均值相差多少个标准差。z-score 是统计学中的一个重要工具,允许对不同数据集进行标准化并识别异常值。
公式
z-score 的计算公式如下:
其中:
- = z-score
- = 单个数据点
- = 数据集的均值
- = 数据集的标准差
该公式计算一个数据点与均值相差多少个标准差。
计算
要计算数据点的 z-score:
-
计算均值 ():
将所有数据点相加并除以数据点的数量。
-
计算标准差 ():
-
方差 ():
-
标准差:
-
-
计算 Z-Score:
将值代入 z-score 公式。
边缘案例
-
零标准差 ():
当所有数据点相同,标准差为零,导致 z-score 未定义,因为不能除以零。在这种情况下,z-score 的概念不适用。
-
数据点等于均值 ():
如果数据点等于均值,则 z-score 为零,表示它正好是平均值。
-
非数值输入:
确保所有输入均为数值。非数值输入将导致计算错误。
累积分布概率
与 z-score 相关的 累积分布概率 表示来自标准正态分布的随机变量小于或等于给定值的概率。它是正态分布曲线在指定 z-score 左侧的面积。
数学上,累积分布概率 是通过标准正态分布的累积分布函数(CDF)计算的:
其中:
- = 在 处的标准正态分布的 CDF
累积分布概率在统计学中对于确定某个值在特定范围内发生的可能性至关重要。它广泛应用于质量控制、金融和社会科学等领域。
SVG 图表
下面是一个 SVG 图表,说明标准正态分布曲线和 z-score:
图:带有 Z-Score 阴影的标准正态分布曲线
该图显示了正态分布曲线,均值 位于中心。阴影区域表示到数据点 的累积分布概率,对应于 z-score。
用例
应用
-
不同尺度之间的标准化:
z-scores 通过标准化数据集,允许在不同尺度之间进行比较。
-
异常值检测:
识别与均值相差显著的数据点(例如,z-scores 小于 -3 或大于 3)。
-
统计检验:
用于假设检验,包括 z 检验,以确定样本均值是否显著不同于已知的总体均值。
-
质量控制:
在制造中,z-scores 帮助监控过程,以确保输出保持在可接受的范围内。
-
金融与投资:
通过比较回报相对于平均市场表现来评估股票表现。
替代方案
-
T-Score:
类似于 z-score,但在样本量小且总体标准差未知时使用。
-
百分位排名:
表示在其频率分布中等于或低于它的分数的百分比。
-
标准差单位:
使用原始标准差值而不进行 z-score 标准化。
历史
z-score 的概念源于 19 世纪早期卡尔·弗里德里希·高斯对正态分布的研究。标准正态分布是 z-scores 的基础,进一步由亚伯拉罕·德·莫弗和皮埃尔-西蒙·拉普拉斯等统计学家发展。随着 20 世纪统计方法的进步,z-scores 的使用变得普遍,特别是在心理测试和质量控制领域。
示例
Excel
1## 在 Excel 中计算 z-score
2## 假设数据点在单元格 A2,均值在单元格 B2,标准差在单元格 C2
3=(A2 - B2) / C2
4
R
1## 在 R 中计算 z-score
2calculate_z_score <- function(x, mean, sd) {
3 if (sd == 0) {
4 stop("标准差不能为零。")
5 }
6 z <- (x - mean) / sd
7 return(z)
8}
9
10## 示例用法:
11x <- 85
12mu <- 75
13sigma <- 5
14z_score <- calculate_z_score(x, mu, sigma)
15print(paste("Z-score:", z_score))
16
MATLAB
1% 在 MATLAB 中计算 z-score
2function z = calculate_z_score(x, mu, sigma)
3 if sigma == 0
4 error('标准差不能为零。');
5 end
6 z = (x - mu) / sigma;
7end
8
9% 示例用法:
10x = 90;
11mu = 80;
12sigma = 8;
13z = calculate_z_score(x, mu, sigma);
14fprintf('Z-score: %.2f\n', z);
15
JavaScript
1// 在 JavaScript 中计算 z-score
2function calculateZScore(x, mu, sigma) {
3 if (sigma === 0) {
4 throw new Error('标准差不能为零。');
5 }
6 return (x - mu) / sigma;
7}
8
9// 示例用法:
10const x = 100;
11const mu = 85;
12const sigma = 7;
13try {
14 const z = calculateZScore(x, mu, sigma);
15 console.log(`Z-score: ${z.toFixed(2)}`);
16} catch (error) {
17 console.error(error.message);
18}
19
Python
1## 在 Python 中计算 z-score
2def calculate_z_score(x, mu, sigma):
3 if sigma == 0:
4 raise ValueError("标准差不能为零。")
5 return (x - mu) / sigma
6
7## 示例用法:
8x = 95
9mu = 88
10sigma = 4
11try:
12 z = calculate_z_score(x, mu, sigma)
13 print("Z-score:", round(z, 2))
14except ValueError as e:
15 print(e)
16
Java
1// 在 Java 中计算 z-score
2public class ZScoreCalculator {
3 public static double calculateZScore(double x, double mu, double sigma) {
4 if (sigma == 0) {
5 throw new IllegalArgumentException("标准差不能为零。");
6 }
7 return (x - mu) / sigma;
8 }
9
10 public static void main(String[] args) {
11 double x = 110;
12 double mu = 100;
13 double sigma = 5;
14
15 try {
16 double z = calculateZScore(x, mu, sigma);
17 System.out.printf("Z-score: %.2f%n", z);
18 } catch (IllegalArgumentException e) {
19 System.err.println(e.getMessage());
20 }
21 }
22}
23
C/C++
1// 在 C++ 中计算 z-score
2#include <iostream>
3#include <stdexcept>
4
5double calculate_z_score(double x, double mu, double sigma) {
6 if (sigma == 0) {
7 throw std::invalid_argument("标准差不能为零。");
8 }
9 return (x - mu) / sigma;
10}
11
12int main() {
13 double x = 130;
14 double mu = 120;
15 double sigma = 10;
16
17 try {
18 double z = calculate_z_score(x, mu, sigma);
19 std::cout << "Z-score: " << z << std::endl;
20 } catch (const std::exception &e) {
21 std::cerr << e.what() << std::endl;
22 }
23
24 return 0;
25}
26
Ruby
1## 在 Ruby 中计算 z-score
2def calculate_z_score(x, mu, sigma)
3 raise ArgumentError, "标准差不能为零。" if sigma == 0
4 (x - mu) / sigma
5end
6
7## 示例用法:
8x = 105
9mu = 100
10sigma = 5
11begin
12 z = calculate_z_score(x, mu, sigma)
13 puts "Z-score: #{z.round(2)}"
14rescue ArgumentError => e
15 puts e.message
16end
17
PHP
1<?php
2// 在 PHP 中计算 z-score
3function calculate_z_score($x, $mu, $sigma) {
4 if ($sigma == 0) {
5 throw new Exception("标准差不能为零。");
6 }
7 return ($x - $mu) / $sigma;
8}
9
10// 示例用法:
11$x = 115;
12$mu = 110;
13$sigma = 5;
14
15try {
16 $z = calculate_z_score($x, $mu, $sigma);
17 echo "Z-score: " . round($z, 2);
18} catch (Exception $e) {
19 echo $e->getMessage();
20}
21?>
22
Rust
1// 在 Rust 中计算 z-score
2fn calculate_z_score(x: f64, mu: f64, sigma: f64) -> Result<f64, String> {
3 if sigma == 0.0 {
4 return Err("标准差不能为零。".to_string());
5 }
6 Ok((x - mu) / sigma)
7}
8
9fn main() {
10 let x = 125.0;
11 let mu = 115.0;
12 let sigma = 5.0;
13
14 match calculate_z_score(x, mu, sigma) {
15 Ok(z) => println!("Z-score: {:.2}", z),
16 Err(e) => println!("{}", e),
17 }
18}
19
C#
1// 在 C# 中计算 z-score
2using System;
3
4public class ZScoreCalculator
5{
6 public static double CalculateZScore(double x, double mu, double sigma)
7 {
8 if (sigma == 0)
9 throw new ArgumentException("标准差不能为零。");
10 return (x - mu) / sigma;
11 }
12
13 public static void Main()
14 {
15 double x = 135;
16 double mu = 125;
17 double sigma = 5;
18
19 try
20 {
21 double z = CalculateZScore(x, mu, sigma);
22 Console.WriteLine($"Z-score: {z:F2}");
23 }
24 catch (ArgumentException e)
25 {
26 Console.WriteLine(e.Message);
27 }
28 }
29}
30
Go
1// 在 Go 中计算 z-score
2package main
3
4import (
5 "errors"
6 "fmt"
7)
8
9func calculateZScore(x, mu, sigma float64) (float64, error) {
10 if sigma == 0 {
11 return 0, errors.New("标准差不能为零")
12 }
13 return (x - mu) / sigma, nil
14}
15
16func main() {
17 x := 140.0
18 mu := 130.0
19 sigma := 5.0
20
21 z, err := calculateZScore(x, mu, sigma)
22 if err != nil {
23 fmt.Println(err)
24 } else {
25 fmt.Printf("Z-score: %.2f\n", z)
26 }
27}
28
Swift
1// 在 Swift 中计算 z-score
2func calculateZScore(x: Double, mu: Double, sigma: Double) throws -> Double {
3 if sigma == 0 {
4 throw NSError(domain: "标准差不能为零。", code: 1, userInfo: nil)
5 }
6 return (x - mu) / sigma
7}
8
9// 示例用法:
10let x = 120.0
11let mu = 110.0
12let sigma = 5.0
13
14do {
15 let z = try calculateZScore(x: x, mu: mu, sigma: sigma)
16 print("Z-score: \(String(format: "%.2f", z))")
17} catch let error as NSError {
18 print(error.domain)
19}
20
参考文献
-
标准分数 - 维基百科
-
理解 Z-Scores - Statistics Solutions
-
正态分布与 Z-Scores - 可汗学院
其他资源
反馈
点击反馈提示开始给这个工具反馈