1 /*************************************************************************/
2 /*                                                                       */
3 /*                  Language Technologies Institute                      */
4 /*                     Carnegie Mellon University                        */
5 /*                        Copyright (c) 2001                             */
6 /*                        All Rights Reserved.                           */
7 /*                                                                       */
8 /*  Permission is hereby granted, free of charge, to use and distribute  */
9 /*  this software and its documentation without restriction, including   */
10 /*  without limitation the rights to use, copy, modify, merge, publish,  */
11 /*  distribute, sublicense, and/or sell copies of this work, and to      */
12 /*  permit persons to whom this work is furnished to do so, subject to   */
13 /*  the following conditions:                                            */
14 /*   1. The code must retain the above copyright notice, this list of    */
15 /*      conditions and the following disclaimer.                         */
16 /*   2. Any modifications must be clearly marked as such.                */
17 /*   3. Original authors' names are not deleted.                         */
18 /*   4. The authors' names are not used to endorse or promote products   */
19 /*      derived from this software without specific prior written        */
20 /*      permission.                                                      */
21 /*                                                                       */
22 /*  CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK         */
23 /*  DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING      */
24 /*  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT   */
25 /*  SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE      */
26 /*  FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES    */
27 /*  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN   */
28 /*  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,          */
29 /*  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF       */
30 /*  THIS SOFTWARE.                                                       */
31 /*                                                                       */
32 /*************************************************************************/
33 /*             Author:  Alan W Black (awb@cs.cmu.edu)                    */
34 /*               Date:  November 2001                                    */
35 /*************************************************************************/
36 /*                                                                       */
37 /*  sufficient statitsics                                                */
38 /*                                                                       */
39 /*************************************************************************/
40 
41 #include "cst_alloc.h"
42 #include "cst_ss.h"
43 #include "cst_math.h"
44 
new_ss()45 cst_ss *new_ss()
46 {
47     cst_ss *ss = cst_alloc(cst_ss,1);
48     return ss;
49 }
50 
delete_ss(cst_ss * ss)51 void delete_ss(cst_ss *ss)
52 {
53     cst_free(ss);
54 }
55 
ss_reset(cst_ss * ss)56 void ss_reset(cst_ss *ss)
57 {
58     ss->num_samples = 0;
59     ss->sum = 0;
60     ss->sumx = 0;
61 }
62 
ss_mean(cst_ss * ss)63 double ss_mean(cst_ss *ss)
64 {
65     if (ss->num_samples > 0)
66 	return ss->sum/ss->num_samples;
67     else
68 	return 0;
69 }
70 
ss_variance(cst_ss * ss)71 double ss_variance(cst_ss *ss)
72 {
73     if (ss->num_samples > 1)
74 	return ((ss->num_samples*ss->sumx)-(ss->sum*ss->sum))/
75 	    (ss->num_samples*(ss->num_samples-1));
76     else
77 	return 0;
78 }
79 
ss_stddev(cst_ss * ss)80 double ss_stddev(cst_ss *ss)
81 {
82     return sqrt(ss_variance(ss));
83 }
84 
ss_cummulate(cst_ss * ss,double a)85 void ss_cummulate(cst_ss *ss,double a)
86 {
87     ss->sum += a;
88     ss->sumx += a*a;
89     ss->num_samples++;
90 }
91 
ss_cummulate_n(cst_ss * ss,double a,double count)92 void ss_cummulate_n(cst_ss *ss,double a, double count)
93 {
94     ss->sum += a*count;
95     ss->sumx += (a*a)*count;
96     ss->num_samples += count;
97 }
98 
99