问题描述
我正在尝试绘制:
使用以下 R 代码没有成功:
Using the following R code with no success:
N= seq(from=150, to=2000)
P=((factorial(60) / factorial(50))*(factorial(N-60) /factorial(N-150))) /(factorial(N) /factorial(N-100))
plot(N,P)
推荐答案
几乎总是,涉及阶乘的概率表达式是N 选 K"计算的一些结果:
Almost always, probability expression involving factorial is some result of "N choose K" computation:
但是通过阶乘来计算它的效率非常低,最重要的是,它在数值上不稳定.使用 factorial()
查看您的代码:您得到了 NaN
.
But it is very inefficient to compute this via factorial, and most importantly, it is not numerically stable. Have a look at your code using factorial()
: you got NaN
.
在 R 中,choose(N, K)
函数快速稳定地计算N 选择 K".
In R, the choose(N, K)
function computes "N choose K" fast and stably.
现在,仔细检查给定的公式表明它等价于:
Now, a careful inspection of your given formulation shows that it is equivalent to:
choose(N-100, 50) / choose(N, 60)
所以,你可以这样做:
P <- choose(N-100, 50) / choose(N, 60)
plot(N, P, type = "l")
跟进
这是一个非常有效的功能.但是这个图的均值、众数和中位数与我在我的课程材料中针对同一个图的那些不匹配?平均值应该是 727,众数 = 600,中位数 = 679!!我怎样才能从你建议的情节中得到这些描述?
我对你的课程材料试图做什么感到困惑.你给出的概率是条件概率P(D|N)
,即随机变量D
的概率.当我们针对 N
勾画 P
时.因此,上面的图不是概率质量函数!那么,我们如何使用它来计算随机变量N
的平均值、众数和中位数等统计数据????
I am confused by what your course material is trying to do. The probability you give is conditional probability P(D | N)
, i.e., a probability for random variable D
. While we sketch P
against N
. Hence, the plot above is not a probability mass function! Then, how can we use it to compute statistics like mean, mode and median, for random variable N
???
无论如何,既然你问并坚持要得到答案,让我们假装这是随机变量N
的概率质量函数.但由于它不是真实的,sum(P)
不是甚至接近于 1.我们实际上有 sum(P) = 3.843678e-12
.因此,要将其用作合适的概率质量函数,我们需要先对其进行归一化.
Well anyway, since you ask and insist on getting an answer, let's pretend this is a probability mass function for random variable N
. But since it is not a true one, sum(P)
is not or even close to 1. We actually have sum(P) = 3.843678e-12
. So, to use it as a proper probability mass function, we need to normalize it first.
P <- P / sum(P)
现在P
相加为1.
为了计算均值,我们做
sum(N * P)
# [1] 726.978
为了计算模式,我们做
N[which.max(P)]
# 599
为了计算中位数,我们做
To compute median, we do
N[which(cumsum(P) > 0.5)[1]]
# 679
这篇关于如何在 R 中绘制阶乘函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!