如何在 R 中绘制阶乘函数 [英] How to plot a factorial function in R

查看:147
本文介绍了如何在 R 中绘制阶乘函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制:

使用以下 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!!我怎样才能从你建议的情节中得到这些描述?

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!! How can I get these descriptives from your suggested plot?

我对你的课程材料试图做什么感到困惑.你给出的概率是条件概率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 中绘制阶乘函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆