Skip to main content

Day 10: Statistics and Header Files

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;

}

Variance and Standard Deviation

Variance is one of the most important statistical measurements for a set of data. 
Figure 1.1
Random number sets
In both datasets, mean would be around 3, but data2 has more variance. 
Figure 1.2
Variance equation
xk-mu is difference between xk and mean aka deviation of xk from mean 
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));

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

Popular posts from this blog

Day 20: Structures, Programming with Pointers

Lecture Structures Structure defines set of data, but individual parts do not have to be the same type. Example 1.1 struct hurricane {  char name[10];  int year,category; }; Within a structure, variables and even arrays can be defined. Structures are also known as aggregate data types since multiple data values can be collected into a single data type. Individual values within a structure are called data members, and each member is given a name. In Example 1.1, the names of the data members are name, year, and category. To refer to a data member, the structure variable name followed by a period and a data member name is used.  Definition and Initialization Define structure. Keyword struct used to define name of structure (aka structure tag) and data members that are included in structure After structure defined, structure variables can be defined using declaration statements. Semicolon required after structure definition. Statements can appear before m...

Day 4: RGB Nightlight | Algorithm Development, Conditional Expressions, Selection Statements

Hardware: RGB Nightlight const int red=11; const int green=10; const int blue=9; const int button=4; boolean lastbutton=LOW; boolean currentbutton=LOW; int ledMode=0; void setup() {   // put your setup code here, to run once:   pinMode(button,INPUT);   pinMode(red,OUTPUT);   pinMode(green,OUTPUT);   pinMode(blue,OUTPUT); } boolean debounce(boolean last) {   boolean current=digitalRead(button);   if(last!=current)   {     delay(5);     current=digitalRead(button);   }   return current; } void setMode(int mode) {   //RED   if(mode==1)   {     digitalWrite(red,HIGH);     digitalWrite(green,LOW);     digitalWrite(blue,LOW);   }   if(mode==2)   {     //orange     digitalWrite(red,HIGH);     analogWrite(green,40);     digitalWrite(blue,LOW);...