在c++中进行kolmogorov-smrinov测试



我用c++为Kolmogorov-Smrinov测试编写了一个程序,但它不起作用。它没有返回d+(dp)、d-(dn)的正确值。我在程序中找不到任何错误。请大家快一点帮忙。我认为在函数dplus和adminus的函数中,数组作为arg传递时存在一些错误。注:本程序仅适用于阿尔法值为0.05且表中数值为0.565 的分布

#include <stdio.h>
#include <math.h>

float dplus(float num[], int n);
float dminus(float num[], int n);
float larges(float data[], int n);
int main()
{
printf("Kolmogorov Testn");
int n;
float dvalue1;
printf("Enter number of elements to compute for tets: t");
scanf("%d", &n);
float num[n];
float dp, dn;
for(int i=0; i<n; i++)
{
scanf("%f", &num[i]);
} 
//sorting in ascending order
for(int i=0; i<n; i++)
{
for(int j=i+1; j<n; j++)
{
if(num[i]>num[j])
{
float temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}

printf("nNumbers in ascending order is: t");
for(int i=0; i<n; i++)
{
printf("%0.2ft",num[i]);
}

dp = dplus(num, n);
dn = dminus(num, n);
printf("ndp = %f",dp);
printf("ndp = %f",dn);

if(dp>dn)
{
dvalue1 = dp;
}
else
{
dvalue1 = dn;
}

float dvalue = 0.565;  //for alpha = 0.05
printf("nCalculated D = %0.2f",dvalue1);
if(dvalue1 < dvalue)
{
printf("n Since D is less tha Dalpha so the data is uniformily distributed.");
}
else
{
printf("nSince  D is greater than Dalpha so the data is not       uniformily distributed.");
}
return 0;
}

float dplus(float num[], int n)
{
float data[n];
int count=1;
for(int i=0; i<n; i++)
{
while(count<=n)
{
data[i] = ((count/n)-num[i]); 
count++;
}
}
float lar = larges(data, n);
return lar;
}
float dminus(float num[], int n)
{
float data[n];
int count=1;
for(int i=0; i<n; i++)
{
while(count<=n)
{
data[i] = (num[i]-((count-1)/n));   
count++;
} 
}
float lar = larges(data, n);
return lar;
}
float larges(float data[], int n)
{
for(int i=1; i<n; i++)  
{
if(data[0]<data[i])
data[0] = data[i];
}
float lar = data[0];
return lar;
}

这段C代码应该给出D+和D的正确值,但我不确定临界值。

#include <stdio.h>
double dplus(double num[], double d1[], int length, double n) {
for (int i = 0; i < length; i++) {
d1[i] = ((i + 1) / n) - num[i];
}
double d1max = d1[0];
for (int i = 0; i < length; i++) {
if (d1max <= d1[i]) {
d1max = d1[i];
}
}
printf("D+ = %fn", d1max);
return d1max;
}
double dminus(double num[], double d2[], int length, double n) {
for (int i = 0; i < length; i++) {
d2[i] = (num[i] - (i) / n);
}
double d2max = d2[0];
for (int i = 0; i < length; i++) {
if (d2max <= d2[i]) {
d2max = d2[i];
}
}
printf("D- = %fn", d2max);
return d2max;
}
void kst(double num[], int length) {
for (int i = 0; i < length; i++) {
for (int j = i + 1; j < length; j++) {
if (num[i] > num[j]) {
double temp;
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
double d1[length];
double d2[length];
double n = (double) length;
double d1max = dplus(num, d1, length, n);
double d2max = dminus(num, d2, length, n);
double dplus = d1max;
double dminus = d2max;
double d;
if (dplus > dminus) {
d = dplus;
printf("D = %fn", d);
} else {
d = dminus;
printf("D = %fn", d);
}

}
int main() {
printf("Kolmogorov Testn");
int n;
double dvalue1;
printf("Enter number of elements to compute for tets: t");
scanf("%d", &n);
double num[n];
double dp, dn;
int i;
for (i = 0; i < n; i++) {
scanf("%lf", &num[i]);
}
kst(num, i);
}

dplus()dminus()例程中的count变量及其伴随的while()循环似乎没有意义。此外,您应该注意表达式是进行整数除法还是浮点除法。下面是一个简化的代码返工——看看它是否更接近你的意图:

#include <stdio.h>
#include <math.h>
float dplus(float num[], int n);
float dminus(float num[], int n);
float largest(float data[], int n);
int main()
{
printf("Kolmogorov Testn");
int n;
printf("Enter number of elements to compute for test: ");
scanf("%d", &n);
float num[n];
for (int i = 0; i < n; i++)
{
scanf("%f", &num[i]);
} 
// sorting in ascending order
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (num[i] > num[j])
{
float temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
printf("Numbers in ascending order are: t");
for (int i = 0; i < n; i++)
{
printf("%0.2ft", num[i]);
}
printf("n");
float dp = dplus(num, n);
float dn = dminus(num, n);
printf("dp = %fn", dp);
printf("dn = %fn", dn);
float dvalue = dn;
if (dp > dn)
{
dvalue = dp;
}
printf("Calculated D = %0.2fn", dvalue);
float dalpha = 0.565;  // for alpha = 0.05
if (dalpha > dvalue)
{
printf("Since D is less than Dalpha, the data is uniformly distributed.n");
}
else
{
printf("Since D is greater than Dalpha, the data is not uniformly distributed.n");
}
return 0;
}
float dplus(float num[], int n)
{
float data[n];
for (int i = 0; i < n; i++)
{
data[i] = (i + 1) / ((float) n) - num[i];
}
return largest(data, n);
}
float dminus(float num[], int n)
{
float data[n];
for (int i = 0; i < n; i++)
{
data[i] = num[i] - i / ((float) n);    
}
return largest(data, n);
}
float largest(float data[], int n)
{
float large = data[0];
for (int i = 1; i < n; i++)  
{
if (large < data[i])
large = data[i];
}
return large;
}

我看不出你的数组传递有任何错误。

相关内容

  • 没有找到相关文章

最新更新