博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
让你拥有超能力:程序员应该掌握的统计学公式
阅读量:6274 次
发布时间:2019-06-22

本文共 9589 字,大约阅读时间需要 31 分钟。

让你拥有超能力:程序员应该掌握的统计学公式

作者:

出处:

Evan Miller收集了程序员应该掌握的统计学公式:

非常有用。
但他在文章前面的这段话更有启发意义:
能够运用统计学,就像拥有了秘密的超能力。
大多数人只会看看平均数,而你会看置信区间。
别人说“7比5大”,你会说,生成数据的过程是无法辨识的。
在众声喧哗的噪音里,你会听到关键的求救。

糟糕的是,并不是很多程序员具有这种超能力。这真是悲剧,因为运用统计学几乎总是能改善数据的展示和阐释。为此,我收集了自认为最有用的统计学公式。

Statistical Formulas For Programmers

By 

DRAFT: May 19, 2013

Being able to apply statistics is like having a secret superpower.

Where most people see averages, you see confidence intervals.

When someone says “7 is greater than 5,” you declare that they're really the same.

In a cacophony of noise, you hear a cry for help.

Unfortunately, not enough programmers have this superpower. That's a shame, because the application of statistics can almost always enhance the display and interpretation of data.

As my modest contribution to developer-kind, I've collected together the statistical formulas that I find to be most useful; this page presents them all in one place, a sort of statistical cheat-sheet for the practicing programmer.

Most of these formulas can be found in Wikipedia, but others are buried in journal articles or in professors' web pages. They are all classical (not Bayesian), and to motivate them I have added concise commentary. I've also added links and references, so that even if you're unfamiliar with the underlying concepts, you can go out and learn more. Wearing a red cape is optional.

Send suggestions and corrections to emmiller@gmail.com


Table of Contents


1. Formulas For Reporting Averages

One of the first programming lessons in any language is to compute an average. But rarely does anyone stop to ask: what does the average actually tell us about the underlying data?

1.1 Corrected Standard Deviation

The standard deviation is a single number that reflects how spread out the data actually is. It should be reported alongside the average (unless the user will be confused).

s=1N1i=1N(xix¯)2−−−−−−−−−−−−−−−−⎷

Where:

  • N is the number of observations
  • xi is the value of the ith observation
  • x¯ is the average value of xi

Reference: 

1.2 Standard Error of the Mean

From a statistical point of view, the "average" is really just an estimate of an underlying population mean. That estimate has uncertainty that is summarized by the standard error.

SE=sN−−√

Reference: 

1.3 Confidence Interval Around the Mean

A confidence interval reflects the set of statistical hypotheses that won't be rejected at a given significance level. So the confidence interval around the mean reflects all possible values of the mean that can't be rejected by the data. It is a multiple of the standard error added to and subtracted from the mean.

CI=x¯±tα/2SE

Where:

  • α is the significance level, typically 5% (one minus the confidence level)
  • tα/2 is the 1α/2 quantile of a t-distribution with N1 degrees of freedom

Reference: 

1.4 Two-Sample T-Test

A two-sample t-test can tell whether two groups of observations differ in their mean.

The test statistic is given by:

t=x1¯x2¯s21/n1+s22/n2−−−−−−−−−−−−√

The hypothesis of equal means is rejected if |t| exceeds the (1α/2) quantile of a t distribution with degrees of freedom equal to:

df=(s21/n1+s22/n2)2(s21/n1)2/(n11)+(s22/n2)2/(n21)

You can see a demonstration of these concepts in .

Reference: 


2. Formulas For Reporting Proportions

It's common to report the relative proportions of binary outcomes or categorical data, but in general these are meaningless without confidence intervals and tests of independence.

2.1 Confidence Interval of a Bernoulli Parameter

A Bernoulli parameter is the proportion underlying a binary-outcome event (for example, the percent of the time a coin comes up heads). The confidence interval is given by:

CI=⎛⎝p+z2α/22N±zα/2[p(1p)+z2α/2/4N]/N−−−−−−−−−−−−−−−−−−−√⎞⎠/(1+z2α/2)

Where:

  • p is the observed proportion of interest
  • zα/2 is the (1α/2) quantile of a normal distribution

This formula can also be used as a .

Reference: 

2.2 Multinomial Confidence Intervals

If you have more than two categories, a multinomial confidence interval supplies upper and lower confidence limits on all of the category proportions at once. The formula is nearly identical to the preceding one.

CI=⎛⎝pj+z2α/22N±zα/2[pj(1pj)+z2α/2/4N]/N−−−−−−−−−−−−−−−−−−−−−√⎞⎠/(1+z2α/2)

Where:

  • pj is the observed proportion of the jth category

Reference: 

2.3 Chi-Squared Test

Pearson's chi-squared test can detect whether the distribution of row counts seem to differ across columns (or vice versa). It is useful when comparing two or more sets of category proportions.

The test statistic, called X2, is computed as:

X2=i=1nj=1m(Oi,jEi,j)2Ei,j

Where:

  • n is the number of rows
  • m is the number of columns
  • Oi,j is the observed count in row i and column j
  • Ei,j is the expected count in row i and column j

The expected count is given by:

Ei,j=nk=1Ok,jml=1Oi,lN

A statistical dependence exists if X2 is greater than the (1α) quantile of a χ2 distribution with (m1)×(n1) degrees of freedom.

You can see a 2x2 demonstration of these concepts in .

Reference: 


3. Formulas For Reporting Count Data

If the incoming events are independent, their counts are well-described by a Poisson distribution. A Poisson distribution takes a parameter λ, which is the distribution's mean — that is, the average arrival rate of events per unit time.

3.1. Standard Deviation of a Poisson Distribution

The standard deviation of Poisson data usually doesn't need to be explicitly calculated. Instead it can be inferred from the Poisson parameter:

σ=λ

This fact can be used to read an , for example.

Reference: 

3.2. Confidence Interval Around the Poisson Parameter

The confidence interval around the Poisson parameter represents the set of arrival rates that can't be rejected by the data. It can be inferred from a single data point of c events observed over t time periods with the following formula:

CI=(γ1(α/2,c)t,γ1(1α/2,c+1)t)

Where:

  • γ1(p,c) is the inverse of the lower incomplete gamma function

Reference: 

3.3. Conditional Test of Two Poisson Parameters

Please never do this:

From a statistical point of view, 5 events is indistinguishable from 7 events. Before reporting in bright red text that one count is greater than another, it's best to perform a test of the two Poisson means.

The p-value is given by:

p=2×c!tc×min⎧⎩⎨i=0c1ti1tci2i!(ci)!,i=c1cti1tci2i!(ci)!⎫⎭⎬

Where:

  • Observation 1 consists of c1 events over t1 time periods
  • Observation 2 consists of c2 events over t2 time periods
  • c=c1+c2 and t=t1+t2

You can see a demonstration of these concepts in .

Reference: 


4. Formulas For Comparing Distributions

If you want to test whether groups of observations come from the same (unknown) distribution, or if a single group of observations comes from a known distribution, you'll need a Kolmogorov-Smirnov test. A K-S test will test the entire distribution for equality, not just the distribution mean.

4.1. Comparing An Empirical Distribution to a Known Distribution

The simplest version is a one-sample K-S test, which compares a sample of n points having an observed cumulative distribution function F to a known distribution function having a c.d.f. of G. The test statistic is:

Dn=supx|F(x)G(x)|

In plain English, Dn is the absolute value of the largest difference in the two c.d.f.s for any value of x.

The critical value of Dn is given by Kα/n, where Kα is the value of x that solves:

1α=2π−−−√xk=1exp((2k1)2π2/(8x2))

The critical must be solved iteratively, e.g. by Newton's method. If only the p-value is needed, it can be computed directly by solving the above for α.

Reference: 

4.2. Comparing Two Empirical Distributions

The two-sample version is similar, except the test statistic is given by:

Dn1,n2=supx|F1(x)F2(x)|

Where F1 and F2 are the empirical c.d.f.s of the two samples, having n1 and n2 observations, respectively. The critical value of the test statistic is Kα/n1n2/(n1+n2)−−−−−−−−−−−−√ with the same value of Kα above.

Reference: 

4.3. Comparing Three or More Empirical Distributions

k-sample extension of Kolmogorov-Smirnov was described by J. Kiefer in a . The test statistic is:

T=supxj=1knj|Fj(x)F¯(x)|

Where F¯ is the c.d.f. of the combined samples. The critical value of T is a2 where a solves:

1α=4Γ(h2)2h/2ahn=1(γ(h2)/2,n)h2exp[(γ(h2)/2,n)2/2a2][Jh/2(γ(h2)/2,n)]2

Where:

  • h=k1
  • Jh/2 is a Bessel function of the first kind with order h/2
  • γ(h2)/2,n is the nth zero of J(h2)/2

To compute the critical value, this equation must also be solved iteratively. When k=2, the equation reduces to a two-sample Kolmogorov-Smirnov test. The case of k=4 can also be reduced to a simpler form, but for other values of k, the equation cannot be reduced.

Reference: 


5. Formulas For Drawing a Trend Line

Trend lines (or best-fit lines) can be used to establish a relationship between two variables and predict future values.

5.1. Slope of a Best-Fit Line

The slope of a best-fit (least squares) line is:

m=Ni=1(xix¯)(yiy¯)Ni=1(xix¯)2

Where:

  • {
    x1,,xN}
     is the independent variable with sample mean x¯
  • {
    y1,,yN}
     is the dependent variable with sample mean y¯

5.2. Standard Error of the Slope

The standard error around the estimated slope is:

SE=Ni=1(yiy¯m(xix¯))2/(N2)−−−−−−−−−−−−−−−−−−−−−−−−−−−−−√Ni=1(xix¯)2−−−−−−−−−−−√

5.3. Confidence Interval Around the Slope

The confidence interval is constructed as:

CI=m±tα/2SE

Where:

  • α is the significance level, typically 5% (one minus the confidence level)
  • tα/2 is the 1α/2 quantile of a t-distribution with N2 degrees of freedom

Reference: 


If you own a Mac, check out my desktop statistics software:

Statistical analyzer

 –  – 

转载于:https://www.cnblogs.com/leonxyzh/archive/2013/05/21/7289114.html

你可能感兴趣的文章
使用深度学习检测DGA(域名生成算法)——LSTM的输入数据本质上还是词袋模型...
查看>>
【转】利用mybatis-generator自动生成代码
查看>>
架构师应该了解的知识1
查看>>
在Flex (Flash)中嵌入HTML 代码或页面—Flex IFrame
查看>>
防止Direct Input获取多次输入
查看>>
Interspeech 2017 | Self-adaptive Speech Recognition Technology
查看>>
Linux中MySQL数据库max_allowed_packet的调整
查看>>
MySQL 学习笔记 二
查看>>
Host prepare for your automation work
查看>>
Thinkphp中field和getField
查看>>
AngularJS之初级Route【一】(六)
查看>>
QTP的那些事--采用DOM,描述性编程获取指定的对象
查看>>
linux异步通信之epoll【转】
查看>>
前端自学路线之js篇
查看>>
C++:运算符重载函数之友元运算符重载
查看>>
ANT task之Junit、JunitReport
查看>>
selenium的那些事--运行报错
查看>>
谋求职业发展,是“走”还是“留”
查看>>
SpreadJS 在 Angular2 中支持绑定哪些属性?
查看>>
Lucene 定义
查看>>