- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 C 编程课上有一个作业要编写一个程序来获取 2 组实数的相关系数。我已经得到了方程式,它引用了维基百科,所以我仔细检查了那里的方程式。这是方程式的链接,从我的研究来看,它似乎是非常标准的:
我已经编写了程序,但是当我运行它时,我得到的结果数字大于 1,我知道这是不正确的。我多次查看了我的代码,但没有发现任何不合适的地方,所以我尝试在最后除以 n 而不是 n-1,这给了我预期的 -1 到 1 范围内的值,所以我测试了它与我在网上找到的数据值以及相关系数计算器 (http://easycalculation.com/statistics/correlation.php) 进行了比较,现在我输入的所有数字都得到了正确的结果。我不明白这是为什么,所以我想我可以在这里得到一些帮助。这是我的程序代码,如果还有其他明显的问题表明我在这里做错了,我很想听听一些建议,但主要是我想弄清楚为什么我会得到正确的结果是错误的等式。
然后它将读取两个数组(x 和 y)的值,然后计算两组数之间的相关系数。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void) {
int n; /* value to determine array length */
/* declare variables to hold results for each equation for x and y
initialize all to zero to prepare for summation */
float r = 0.0, xbar = 0.0, ybar = 0.0, sx = 0.0, sy = 0.0;
/*get number n input from user */
printf("Please enter a number n: ");
scanf("%d", &n);
if( n < 1) {
printf("n must be a positive number.\nPlease enter a new value: ");
scanf("%d", &n);
if( n < 1) {
printf("Invalid input, exiting...\n");
return 0;
}
}
/*initialize arrays x and y with length of n */
float x[n], y[n];
/*use for loop to read in values of x*/
int i;
for(i = 0; i < n; ++i) {
printf("Please enter a number for x: ");
scanf("%f", &x[i]);
}
/*use for loop to read in values of y*/
for(i = 0; i < n; ++i) {
printf("Please enter a number for y: ");
scanf("%f", &y[i]);
}
/*compute xbar */
for(i = 0; i < n; ++i) {
xbar += x[i];
}
xbar /= n;
/*compute ybar*/
for(i = 0; i < n; ++i) {
ybar += y[i];
}
ybar /= n;
/* compute standard deviation of x*/
for(i = 0; i < n; ++i) {
sx += (x[i] - xbar) * (x[i] - xbar);
}
sx = sqrt((sx / n));
/* compute standard deviation of y */
for(i = 0; i < n; ++i) {
sy += (y[i] - ybar) * (y[i] - ybar);
}
sy = sqrt((sy / n));
/*compute r, the correlation coefficient between the two arrays */
for( i = 0; i < n; ++i ) {
r += (((x[i] - xbar)/sx) * ((y[i] - ybar)/sy));
}
r /= (n); /* originally divided by n-1, but gave incorrect results
dividing by n instead produces the desired output */
/* print results */
printf("The correlation coefficient of the entered lists is: %6.4f\n", r);
return 0;
}
(看起来我的代码格式不工作,对此非常抱歉。尝试使用标签和按钮但无法弄清楚。看起来我让它工作了一些,比以前更好。)
最佳答案
您计算的标准偏差为:
sx = sqrt((sx / n));
sy
也是如此。
您使用的等式在分母中使用 n-1
来计算这个( reason :有 n-1
自由度,所以您应该除以通过 n-1
)。所以,你的 sx
和 sy
实际上是 sx'
和 sy'
,其中 sx' = sx *sqrt(n-1)/sqrt(n)
,和 sy' = sy*sqrt(n-1)/sqrt(n)
。所以,sx' * sy' = sx * sy * (n-1)/n
。由于 sx*sy
在分母中,因此您的计算因 n/(n-1)
而偏离。将其除以 n
即可得到求和之外所需的因数。
因此,如果您更改代码以计算样本标准差(除以 n-1
),您最终可以除以 n-1
,您的代码将得到你期望的结果。为了提高效率,由于除法无论如何都会抵消,您可以通过在 sx
和 的计算中不除以
,然后也省略最后的除法:n-1
来节省一些计算并提高准确性code>sy
sx = sqrt((sx / n));
sy = sqrt((sy / n));
成为
sx = sqrt(sx);
sy = sqrt(sy);
和:
r /= (n);
完全消失了。
编辑:既然你问了......
float
除非你必须这样做。 double
为您提供更高的精度。stdout
在大多数系统上都是行缓冲的,因此在调用 scanf()
之前您的提示可能不会出现。为确保显示提示,请在调用 printf()
后执行 fflush(stdout);
。scanf()
。对于读取数字,当有人输入不在数据类型范围内的数字时,scanf()
具有未定义的行为。此外,这对某些情况不利,例如当有人响应您的提示输入非整数时。对于您的情况,您可以将 n
作为命令行参数传递,然后使用 strtol(argv[1])
来解析数字。如果您无论如何都想从 stdin
读取,请使用 fgets() + sscanf()
组合,或 fgets() + strtol()
。xbar
和 ybar
。更好的是,您可以编写一个函数 double avg(double *data, int n)
,计算 n
值的平均值,然后执行:xbar=avg (x, n);
, ybar=avg(y, n);
。double std(double *data, int n)
,然后使用它来计算sx
和sy
.sqrt((sx/n));
最好写成 sqrt(sx/n);
. r/= (n);
也不需要括号。关于c - 奇数 PIL 逊相关系数结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2153466/
我的任务是编写一个java程序,首先询问用户将输入多少个数字,然后输出输入的奇数和偶数个数。它限制为整数 0-100。我的问题是:我的代码中缺少什么? import java.util.Scanner
我正在寻找有关 VBA 脚本的帮助。我一直在试图弄清楚如何使用 mod 功能。 这是我到目前为止所做的: Function AddOddNumbersWithMod(nr) Dim i, su
我只是想从 .NET 调用 kernel32 中的 GetPrivateProfileString 和 GetPrivateProfileSection,但遇到了一些我不明白的奇怪问题。 让我们从这个
我需要做的是在列表中交替应用 2 个函数。例如: (*2) (-3) [4,5,6,7,8] 会导致[8,2,12,4,16] , 因为 4*2 , 5-3 , 6*2 , 7-3 , 8*2 ...
我尝试在 JavaScript 中创建一个函数来判断一个数字是否为偶数,或者它是否是一个数字。我收到此错误: 这是 CodeCademy 中的类(class)。 最佳答案 您正在检查函数 isNaN
当我运行此命令时,不会打印任何内容,我尝试根据用户输入的内容打印一条消息,显示奇数或偶数。 import java.util.Scanner; public class Questions {
我必须编写一个程序来读取 3 个数字(使用输入框),并根据它们的值写入以下消息之一: 所有 3 个数字都是奇数或 所有 3 个数字都是偶数或 2 个数字是奇数,1 个数字是偶数或 1 个数字是奇数
我正在构建一个谷歌图像搜索的示例。我有一个图像网格(搜索结果)。当单击其中一张图像时,我正在使用 jquery 创建的部分标记中加载 html 文档。奇怪的是,如果你查看开发人员工具,html 已加载
我试图仅在偶数行上打印单词 * Even * ,而不包括第一行和第二行(最终不包括最后两行),但它打印 15 行,然后在整个过程中随机打印 * Even * 。使用 6 表示宽度,使用 15 表示高度
我有一个数学函数,它取决于由 给出的三个变量 {n、a 和 b} {a = n+1, b=n} 当 n 为偶数时 {b = n+1, a=n} 当 n 为奇数时 我的函数被调用了很多 次,n 各不相同
我有一个查询,其中有一个条件来检查房间号是否为奇数/偶数。问题在于房间号与建筑物信息一起存储在字符串中。 以下是数据库中数据的格式: ABC-0101A (Odd) ABC-0112B (Even
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我有一个包含类似内容的页面。 ################################# # __________________________ # # | |
这个问题在这里已经有了答案: Can I combine :nth-child() or :nth-of-type() with an arbitrary selector? (9 个回答) Ho
我正在尝试了解这个素数分解的特定解决方案(取自 http://rosettacode.org/wiki/Prime_decomposition#Python:_Using_floating_point
我知道这可能是一个愚蠢而简单的问题,但我对编程还很陌生。我有以下关于我在一个程序中看到的 if 运算符的问题。这是代码: d= -12.4; if(d) printf("%d \n", abs
我正在尝试制作一个脚本,根据天气情况输出用户名,他们被分配奇数或偶数值。我想我已经设法让奇数的工作,但偶数的不会输出。这是它的样子。 'commentid' 是确定将它们分配给奇数还是偶数的值。 ";
我的 NPE 的 Stacktrace 开始于: Caused by: java.lang.NullPointerException at pl.yourvision.crm.web.serv
我正在尝试查找给定数字(用户输入)是偶数还是奇数。 I'm simply applying AND operation on binary digits of a no. with 1, If the
有谁知道用于可变范围的桶的哈希函数(对于字符串,如果它重要的话),它总是奇数(或质数,如果需要的话)? 本质上,我正在寻找一个散列函数,它将在 n 个桶上提供均匀分布,其中 n 是奇数(或质数,因为
我是一名优秀的程序员,十分优秀!