1 /*
2 * =====================================================================================
3 *
4 * Filename: stats.h
5 *
6 * Description: simple distribution execise
7 *
8 * Version: 1.0
9 * Created: 04/15/2011 06:12:42 PM
10 * Revision: none
11 * Compiler: gcc
12 *
13 * Author: Ruben Safir,
14 * Company:
15 *
16 * =====================================================================================
17 */
18 #ifndef STATS_H
19 #define STATS_H
20 #include <iostream>
21 #include <fstream>
22 #include <climits>
23 #include <cmath>
24 #include <stdlib.h>
25 #include <iomanip>
26 #include "linklist.h"
27
28 /*
29 * =====================================================================================
30 * Class: Distribution
31 * Description: Keeps Track of Distribution of 6's (or anything else) in a series of List
32 * =====================================================================================
33 */
34
35
36 namespace stats{
37
38
39 template<class T>
40 class Distribution {
41 template<T> friend std::ostream & operator<<(std::ostream &, const Distribution<T>&);
42 public:
43 /* ==================== LIFECYCLE ======================================= */
44 Distribution (T descr, int occurances = 0);
45 //Distribution():freq(NULL), occurances(0){};
46 Distribution(){};
47 // Distribution ( const Distribution &other ); /* copy constructor */
48 // ~Distribution (); /* destructor */
49 /* ==================== ACCESSORS ======================================= */
50 T description()const{ return freq;}
51 int population()const { return occurances; }
52 /* ==================== MUTATORS ======================================= */
53 void increase_occ(){ ++occurances; std::cout << "description " << freq << " occurances " << occurances << std::endl; }
54 void descrease_occ(){ --occurances; }
55 /* ==================== OPERATORS ======================================= */
56 //Distribution& operator = ( const Distribution &other ); /* assignment operator */
57 int operator()(){
58 return freq;
59 }
60 bool operator==(Distribution &tmp){
61 if(this->freq == tmp.freq)
62 return true;
63 return false;
64 }
65
66 bool operator<(Distribution &tmp){
67 if(freq < tmp.freq)
68 return true;
69 return false;
70 }
71 chainlist::List< stats::Distribution<T> > * tally; //a list of distribution talleys
72
73 float stddev(chainlist::List<stats::Distribution<T> > *);
74
75
76
77
78 protected:
79 /* ==================== DATA MEMBERS ======================================= */
80 private:
81 /* ==================== DATA MEMBERS ======================================= */
82 T freq; //description of unique identifier of a sample point in a List
83 int occurances; //description of how many times a frequency was found in a list
84 }; /* ----- end of class Distribution ----- */
85
86
87 template<typename T>
88 std::ostream & operator << ( std::ostream & os, const Distribution<T> & obj )
89 {
90 T desc = obj.description();
91 int pop = obj.population();
92 os << "The Identification of " << desc << " was seen " << pop ;
93 return os;
94 } /* ----- end of function operator << ----- */
95
96 template<typename T>
97 Distribution<T>::Distribution(T descr, int occ): occurances(occ){
98 freq = descr;
99 }
100
101
102 // routinines not part of the distribution class
103
104 /* Routine to determin the mean value of populations in a list of distribribution members */
105 template <typename T>
106 float mean_list(chainlist::List< Distribution<T> > * tally);
107
108 /* Routine to go though a single list and add it to an existing distribution table */
109 template<typename T>
110 void mount_individual_data_point(chainlist::List<T> * tabulate, chainlist::List<stats::Distribution<T> > * table);
111
112 /* Routine to find all the occurances of a type in a list of lists */
113 template<typename T>
114 void take_tally(chainlist::List<T> *,chainlist::List<stats::Distribution<T> > *);
115
116
117 //calculation standard deviation of distribution list
118 //
119 template<typename T>
120 float stddev(chainlist::List<stats::Distribution<T> > * tally){
121 float dev, vari = 0;
122 //walk through the list
123 if( tally->endd() == 0 ){
124 std::cout << "Empty List" << std::endl;
125 exit(0);
126 }
127 float mean = mean_list(tally);
128 tally->cursor() = tally->front();
129
130 while(tally->cursor() != tally->endd() ){
131 vari += pow (tally->cursor()->value()->population() - mean, 2 );
132 tally->cursor() = tally->cursor()->next();
133 }
134 vari += pow (tally->cursor()->value()->population() - mean, 2 );
135 dev = sqrt( (vari / tally->size()) );
136
137
138
139
140
141 return dev;
142 }
143
144
145
146
147
148
149
150 template<typename T>
151 void take_tally(chainlist::List<T> * tabulate, chainlist::List<stats::Distribution<T> > * table){
152 for(tabulate->cursor()=tabulate->front();tabulate->cursor() != tabulate->endd(); tabulate->cursor( tabulate->cursor()->next() ) ){ //build distribution list
153 mount_individual_data_point(tabulate, table);
154 }
155 //we are at the end of tabulate
156 mount_individual_data_point(tabulate, table);
157 table->sort(*table);
158 }
159
160 template<typename T>
161 void mount_individual_data_point(chainlist::List<T> * tabulate, chainlist::List<stats::Distribution<T> > * table){
162 T val;
163 stats::Distribution<T> * j;
164 val = *(tabulate->cursor()->value()); //get a value
165 table->cursor()= table->front(); //check to see if the distribution list exists
166 if(!table->cursor()){ // if not add a distribution table to the List of distributions
167 j = new stats::Distribution<T> (val);
168 table->insert(*j ); //now we have at least one
169 delete j;
170 j=table->cursor()->value();//and increased its population
171 j->increase_occ();
172 }else{
173 //otherwise search for a distribution node described as value
174 table->find_value(val);
175 if( table->cursor() ){
176 j=table->cursor()->value();//and increase its population
177 j->increase_occ();
178 }else{//otherwise add a new node
179 j = new stats::Distribution<T> (val);
180 table->insert( *j ); //now we have one for that value
181 delete j;
182 j=table->cursor()->value();//and increased its population
183 j->increase_occ();
184 }
185 }
186 }
187
188
189
190
191 template <typename T>
192 float mean_list(chainlist::List< Distribution<T> > * tally){
193 if(tally->endd() == 0){
194 std::cout << "Empty List" << std::endl;
195 return 0.0;
196 }
197
198 int sum = 0;
199
200 tally->cursor() = tally->front();
201 while(tally->cursor() != tally->endd() ){
202 sum += tally->cursor()->value()->population() ;
203 tally->cursor(tally->cursor()->next());
204 }
205 sum += tally->cursor()->value()->population() ;
206
207 return sum/(tally->size());
208 }
209
210
211
212
213
214 }
215 #endif /* STATS_H */