2015
04-13

# Statistical Problems

Last year Dige was having a statistic course and had to solve many problems and calculate many results. However, he thought it was worthless to practice that much since that after solving several problems, the rest was just repeating and repeating, which is too simple, sometimes naive. Therefore, he wanted someone to help him write a program, which would output all the results he needs immediately instead of calculating them himself.

The statistic problems might include z-scores, one-sample t-tests, correlation and regression.

For a population of real numbers, the standard deviation (SD) of the numbers is defined as

, where n is the number of the numbers, and Mx is the mean value of the numbers. The z-score for each number is defined as
.
You know, sometimes (almost always) we cannot get data for a whole population, and what we’ve got is just a sample from it. For a sample from a population, the SD is
, while the standard error (SE) is
. In a one-sample t-test, the t-value is defined as
.

There are many ways to calculate a correlation coefficient between two variables X and Y, and one of the most popular one is Pearson’s r, which is

. Note that the SDs here are population SDs.
It is a little complicated for regression, which is meant to find the “best” a and b for the estimation
. Here
, and
. If you use these equations to solve a regression problem, it doesn’t matter which SD you use here, sample or population. However, you should use the same type of SD for and .

Inputs include multiple cases (no more than 100) and would end with EOF.
Each case starts with a line including two integers p and n. p indicates the kind of problem to solve, 1 means z-score, 2 means one-sample t-test, 3 means Pearson correlation, 4 means regression. n is the number of data points. n is no smaller than 2 and no larger than 10000.
If p is 1, then the following input of the case would be n numbers.
If p is 2, then the following input of the case would be (n + 1) numbers, the first one is the mean of the population and the rest are the data of the sample.
If p is 3 or 4, then the following input of the case would be 2 lines, each with n numbers. The first line are Xs and the second are Ys.
Note that inputs can be separated not only by spaces, but also tabs and new lines, or even combinations of them.

Inputs include multiple cases (no more than 100) and would end with EOF.
Each case starts with a line including two integers p and n. p indicates the kind of problem to solve, 1 means z-score, 2 means one-sample t-test, 3 means Pearson correlation, 4 means regression. n is the number of data points. n is no smaller than 2 and no larger than 10000.
If p is 1, then the following input of the case would be n numbers.
If p is 2, then the following input of the case would be (n + 1) numbers, the first one is the mean of the population and the rest are the data of the sample.
If p is 3 or 4, then the following input of the case would be 2 lines, each with n numbers. The first line are Xs and the second are Ys.
Note that inputs can be separated not only by spaces, but also tabs and new lines, or even combinations of them.

1 3
1 2 3

1 2
1 2

2 3
2 1 2 3

3 4
1 2 3 4
5 6 7 8

4 3
1 2 3
3 5 7

-1.22 0.00 1.22
-1.00 1.00
0.00
1.00
1.00 2.00
HintOutput -0.00 as it is. 

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int main()
{
int p,n,i,k=0;
double Mx,My,SDx,SDy,SE,rxy,a[10002],b[10002];
while(scanf(“%d%d”,&p,&n)!=EOF)
{
double sum=0,sx=0,sy=0,Sx=0,Sy=0,s=0,t,SD=0,ka,kb;
if(p==1)
{
for(i = 0 ; i < n ; i ++ )
{
scanf(“%lf”,&a[i]);
sum+=a[i];
}
Mx = sum/n;
for( i = 0 ; i < n ; i ++ )
s += ((a[i]-Mx)*(a[i]-Mx));
SDx = sqrt(s/n);
printf(“%.2lf”,(a[0]-Mx)/SDx);
for(i=1;i<n;i++)
printf(” %.2lf”,(a[i]-Mx)/SDx);
printf(“\n”);
}
else if(p==2)
{
for( i = 0 ; i <= n ; i ++ )
{
scanf(“%lf”,&a[i]);
if(i!=0)
sum+=a[i];
}
Mx=sum/n;
for( i = 1 ; i <= n ; i ++ )
s+=((a[i]-Mx)*(a[i]-Mx));
SDx=sqrt(s/(n-1));
SE=SDx/sqrt(n);
t=(Mx-a[0])/SE;
printf(“%.2lf\n”,t);
}
else
{
for( i = 0 ; i < n ; i ++ )
{
scanf(“%lf”,&a[i]);
sx += a[i];
}
for( i = 0 ; i < n ; i ++ )
{
scanf(“%lf”,&b[i]);
sy += b[i];
}
Mx = sx/n;
for( i = 0 ; i < n ; i ++ )
Sx += ((a[i]-Mx)*(a[i]-Mx));
SDx = sqrt(Sx/n);
My = sy/n;
for( i = 0 ; i < n ; i ++ )
Sy += ((b[i]-My)*(b[i]-My));
SDy = sqrt(Sy/n);
for( i = 0 ; i < n ; i ++)
{
SD += ((a[i]-Mx)*(b[i]-My));
}
rxy = SD/(n*SDx*SDy);
kb = rxy*SDy/SDx;
ka = My-kb*Mx;
if(p==3)
{
printf(“%.2lf\n”,rxy);
}
else
{
printf(“%.2lf %.2lf\n”,ka,kb);
}
}
}
return 0;
}

1. Excellent Web-site! I required to ask if I might webpages and use a component of the net web website and use a number of factors for just about any faculty process. Please notify me through email regardless of whether that would be excellent. Many thanks