Software: Statistics and Header Files
Statistical Measurements
Important functions include maximum, minimum, average, and median.
Example 1.1
/*–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
/* This function returns the average or mean value of an */
/* array x with n elements. */
double mean(double x[],int npts)
{ /* Declare and initialize variables. */
int k;
double sum=0;
/* Determine mean value. */
for (k=0; k<=npts-1; k++)
sum += x[k];
/* Return mean value. */
return sum/n;
}
Sum was initialized to zero in declaration statement
Example 1.2
/*–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
/* This function returns the median value in the sorted */
/* array x with npts elements. */
double median(double x[],int npts)
{
/* Declare variables. */
int k;
double median_x;
/* Determine median value. */
k = floor(npts/2);
if (n%2 != 0)
median_x = x[k];
else
median_x = (x[k-1] + x[k])/2;
/* Return median value. */
return median_x;
}
Example 1.1
/*–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
/* This function returns the average or mean value of an */
/* array x with n elements. */
double mean(double x[],int npts)
{ /* Declare and initialize variables. */
int k;
double sum=0;
/* Determine mean value. */
for (k=0; k<=npts-1; k++)
sum += x[k];
/* Return mean value. */
return sum/n;
}
Sum was initialized to zero in declaration statement
Example 1.2
/*–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
/* This function returns the median value in the sorted */
/* array x with npts elements. */
double median(double x[],int npts)
{
/* Declare variables. */
int k;
double median_x;
/* Determine median value. */
k = floor(npts/2);
if (n%2 != 0)
median_x = x[k];
else
median_x = (x[k-1] + x[k])/2;
/* Return median value. */
return median_x;
}
Variance and Standard Deviation
Variance is one of the most important statistical measurements for a set of data.
In both datasets, mean would be around 3, but data2 has more variance.
| Figure 1.1 Random number sets |
| Figure 1.2 Variance equation |
value squared so value is always positive
Standard deviation is square root of variance
Example 1.3
/*–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
/* This function returns the variance of an array x */
/* with npts elements. */
double variance(double x[],int npts)
{
/* Declare variables and function prototypes. */
int k;
double sum=0, mu;
double mean(double x[],int npts);
/* Determine variance. */
mu = mean(x,npts);
for (k=0; k<=n-1; k++)
sum += (x[k] - mu)*(x[k] - mu);
/* Return variance. */
return sum/(npts-1);
}
/*–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
/* This function returns the standard deviation of an array x */
/* with npts elements. */
double std_dev(double x[],int npts)
{
/* Declare function prototypes. */
double variance(double x[],int npts);
/* Return standard deviation. */
return sqrt(variance(x,npts));
/* This function returns the variance of an array x */
/* with npts elements. */
double variance(double x[],int npts)
{
/* Declare variables and function prototypes. */
int k;
double sum=0, mu;
double mean(double x[],int npts);
/* Determine variance. */
mu = mean(x,npts);
for (k=0; k<=n-1; k++)
sum += (x[k] - mu)*(x[k] - mu);
/* Return variance. */
return sum/(npts-1);
}
/*–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
/* This function returns the standard deviation of an array x */
/* with npts elements. */
double std_dev(double x[],int npts)
{
/* Declare function prototypes. */
double variance(double x[],int npts);
/* Return standard deviation. */
return sqrt(variance(x,npts));
Practice Problems
a. Write a function called rand_float using function prototype that uses the rand function to generate random floating point number between a specified range of values (in problem 1A for homework)
double rand_float(double interval_start, double interval_end);
b. Write program that uses rand_float function to generate sequences of random floating-point values between 4 and 10. Then compare the computed mean and variance with the computed theoretical values. As you use more random numbers, the computed values and the theoretical values should become closer. (1a for homework).
Custom Header File
To facilitate use of functions, they can be put in a custom header file called stat_lib.h and use a preprocessor directive to include the header file.
Homework
1. Complete the random number problem but with all four parts:
a. Write a program that uses the rand_float function developed previously to generate sequences of random floating-point values between 4 and 10. Then compare the computed mean and variance with the computed theoretical values. As you use more and more random numbers, the computed values and the theoretical values should become closer.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
double rand_float(double interval_start, double interval_end);
double rand_float(double interval_start, double interval_end)
{
float mynum=((float)rand())/(float)RAND_MAX;
float diff=interval_end - interval_start;
float r=mynum*diff;
return interval_start + r;
}
int main(void)
{
float mu;
srand(time(NULL));
for(int k=1; k<=20; k++)
{
printf("%1.3f",rand_float(4, 10));
printf("\n");
}
return 0;
}
Comments
Post a Comment