简体   繁体   English

如何在R中绘制阶乘函数

[英]How to plot a factorial function in R

I'm trying to plot: 我正在尝试绘制:

在此处输入图片说明

Using the following R code with no success: 使用以下R代码没有成功:

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)

Almost always, probability expression involving factorial is some result of "N choose K" computation: 几乎总是,涉及阶乘的概率表达式是“ N select K”计算的一些结果:

在此处输入图片说明

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 . 使用factorial()看一下代码: NaN

In R, the choose(N, K) function computes "N choose K" fast and stably. 在R中, choose(N, K)函数可快速,稳定地计算“ N select K”。

Now, a careful inspection of your given formulation shows that it is equivalent to: 现在,仔细检查您给定的配方,它等同于:

choose(N-100, 50) / choose(N, 60)

So, you can do: 因此,您可以执行以下操作:

P <- choose(N-100, 50) / choose(N, 60)
plot(N, P, type = "l")

在此处输入图片说明


Follow-up 跟进

Hi, this is a very efficient function. 嗨,这是一个非常有效的功能。 But mean, mode, and median of this plot doesn't match the ones I have in my course materials for the same plot? 但是,该图的均值,众数和中位数与我在课程材料中针对同一图的那些不匹配吗? The mean should be 727, Mode= 600, median= 679!! 平均值应该是727,Mode = 600,中位数= 679! How can I get these descriptives from your suggested plot? 我如何从您建议的情节中获得这些描述?

I am confused by what your course material is trying to do. 我对您的课程材料想做什么感到困惑。 The probability you give is conditional probability P(D | N) , ie, a probability for random variable D . 您给出的概率是条件概率P(D | N) ,即随机变量D的概率。 While we sketch P against N . 当我们针对N绘制P时。 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计算诸如均值,众数和中位数之类的统计信息?

Well anyway, since you ask and insist on getting an answer, let's pretend this is a probability mass function for random variable N . 嗯,无论如何,既然您要求并坚持要得到答案,我们就假设这是随机变量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 . 但是由于它不是一个真实的值,因此sum(P)甚至不是1。我们实际上有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)

Now P sum up to 1. 现在P总和为1。

To compute mean, we do 为了计算均值,我们这样做

sum(N * P)
# [1] 726.978

To compute mode, we do 为了计算模式,我们要做

N[which.max(P)]
# 599

To compute median, we do 为了计算中位数,我们这样做

N[which(cumsum(P) > 0.5)[1]]
# 679

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM