stat mode

in c

Source Code

``````// Patrick Yevsukov

// 2013 CC BY-NC-SA 4.0 http://patrick.yevsukov.com

// github.com/PatrickYevsukov/Statistics-Calculator

#include "stat_mode.h"

double *
StatMode(double *sample, int *sample_size)
{
int ii = 0;

int kk = 0;

int num_modes = 0;

int previous_freq = 0;

int current_freq = 1; // Every value must occur at least once

double *modes = NULL;

modes = (double *) malloc(MAX_SAMPLE * sizeof *modes);

for (ii = 0; ii <= *sample_size - 1; ii++)
{
if (sample[ii] == sample[ii + 1])
{
current_freq++;
}
else
{
if (current_freq > previous_freq)
{
kk = 0; // Reset

previous_freq = current_freq;

modes[kk] = sample[ii];
}

else if (current_freq == previous_freq)
{
kk++;

modes[kk] = sample[ii];
}

current_freq = 1; // Reset
}
}

num_modes = kk + 1;

return modes;
}

// I devised this algorithm to identify the statistical mode or
// modes of a dataset. It requires one pass through the set to
// identify the set's mode or modes.

// This algorithm works by keeping track of the number of times a
// unique value occurs in the set. If a unique value has occurred
// more times than the ones before it, it is added to the list of
// modes. As the loop continues all values which occur this number
// of times are added to the list of modes, unless another unique
// value occurs more times than the currently identified modes. If
// this happens, the mode array indexing variable is reset to zero
// and the previously identified values are overwritten by the new
// modes. When finished, the mode array indexing variable will be
// one less than the number of modes.

// This algorithm requires a set of values in which all instances
// of a unique value are listed next to one another.
``````
``````// Patrick Yevsukov

// 2013 CC BY-NC-SA 4.0 http://patrick.yevsukov.com

// github.com/PatrickYevsukov/Statistics-Calculator

#include <stdio.h>
#include <stdlib.h>

#define MAX_SAMPLE 25

double *
StatMode(double *, int *);
``````
``````// Patrick Yevsukov

// 2013 CC BY-NC-SA 4.0 http://patrick.yevsukov.com

// github.com/PatrickYevsukov/Statistics-Calculator

#include "stat_mode.h"
#include "stat_mode_test.h"

int
main(int argc, char **argv)
{
TestStatMode();

return EXIT_SUCCESS;
}

void
TestStatMode()
{
int ii = 0;

int jj = 0;

int eight = EIGHT;

int eleven = ELEVEN;

double *result = NULL;

double size_eight_samples[THREE][EIGHT] = {

{0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0},
{0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7},
{1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0}
};

double size_eleven_samples[ELEVEN][ELEVEN] = {

{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 1.0, 2.0, 2.0, 2.0, 3.0, 4.0, 5.0, 6.0},
{0.0, 1.0, 2.0, 3.0, 4.0, 4.0, 4.0, 5.0, 6.0, 6.0, 6.0},
{0.0, 0.0, 0.0, 0.0, 2.0, 2.0, 2.0, 3.0, 4.0, 5.0, 6.0},
{0.0, 1.0, 2.0, 3.0, 4.0, 4.0, 4.0, 5.0, 5.0, 5.0, 5.0},
{0.0, 1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0, 5.0, 5.0},
{0.0, 0.0, 1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0, 5.0},
{0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 4.0},
{0.0, 0.0, 0.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 4.0},
{0.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0, 4.0},
{0.0, 0.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0, 6.0}
};

double size_eight_modes[THREE][ELEVEN] = {

// First element of the array is the number of modes

// The remaining elements are padded out with zeroes

{8.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 0.0, 0.0},
{8.0, 0.0, 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 0.0, 0.0},
{4.0, 1.0, 2.0, 3.0, 4.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}
};

double size_eleven_modes[ELEVEN][ELEVEN] = {

// First element of the array is the number of modes

// The remaining elements are padded out with zeroes

{1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{2.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{2.0, 4.0, 6.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{1.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{5.0, 1.0, 2.0, 3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{5.0, 0.0, 1.0, 2.0, 3.0, 4.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{3.0, 0.0, 1.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{2.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{2.0, 2.0, 4.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
{2.0, 2.0, 3.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}
};

for(ii = 0; ii < THREE; ii++)
{
printf("Sample Set %i: ", ii);

for(jj = 0; jj < EIGHT; jj++)
{
printf("%.1f ", size_eight_samples[ii][jj]);
}

printf("\n");

result = StatMode(size_eight_samples[ii], &eight);

printf("Result Set %i: ", ii);

for(jj = 0; jj < size_eight_modes[ii][0]; jj++)
{
printf("%.1f ", result[jj]);

assert(result[jj] == size_eight_modes[ii][jj + 1]);
}

printf("\n\n");
}

printf("\n");

for(ii = 0; ii < ELEVEN; ii++)
{
printf("Sample Set %i: ", ii);

for(jj = 0; jj < ELEVEN; jj++)
{
printf("%.1f ", size_eleven_samples[ii][jj]);
}

printf("\n");

result = StatMode(size_eleven_samples[ii], &eleven);

printf("Result Set %i: ", ii);

for(jj = 0; jj < size_eleven_modes[ii][0]; jj++)
{
printf("%.1f ", result[jj]);

assert(result[jj] == size_eleven_modes[ii][jj + 1]);
}

printf("\n\n");
}
}
``````
``````// Patrick Yevsukov

// 2013 CC BY-NC-SA 4.0 http://patrick.yevsukov.com

// github.com/PatrickYevsukov/Statistics-Calculator

#include <assert.h>

#define THREE 3
#define EIGHT 8
#define ELEVEN 11

void
TestStatMode(void);
``````