1 /* PSPP - computes sample statistics.
2 Copyright (C) 2004 Free Software Foundation, Inc.
3 Written by John Darrington <john@darrington.wattle.id.au>
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
29 /* Number of bins in which to divide data */
32 /* The approximate no of ticks on the y axis */
35 #define M_PI ( 22.0 / 7.0 )
38 static double gaussian(double x, double mu, double sigma ) ;
42 gaussian(double x, double mu, double sigma )
44 return (exp( - (( x - mu )* (x - mu) / (2.0 * sigma * sigma) ))
45 / ( sigma * sqrt( M_PI * 2.0) )) ;
50 /* Write the legend of the chart */
52 write_legend(struct chart *ch, struct normal_curve *norm)
55 pl_savestate_r(ch->lp);
57 sprintf(buf,"N = %.2f",norm->N);
58 pl_move_r(ch->lp, ch->legend_left, ch->data_bottom);
59 pl_alabel_r(ch->lp,0,'b',buf);
61 sprintf(buf,"Mean = %.1f",norm->mean);
62 pl_fmove_r(ch->lp,ch->legend_left,ch->data_bottom + ch->font_size * 1.5);
63 pl_alabel_r(ch->lp,0,'b',buf);
65 sprintf(buf,"Std. Dev = %.2f",norm->stddev);
66 pl_fmove_r(ch->lp,ch->legend_left,ch->data_bottom + ch->font_size * 1.5 * 2);
67 pl_alabel_r(ch->lp,0,'b',buf);
69 pl_restorestate_r(ch->lp);
76 If show_normal is non zero then superimpose a normal curve
79 draw_histogram(struct chart *ch,
80 const struct variable *var,
81 const struct freq_tab *frq_tab,
83 struct normal_curve *norm,
90 double x_min = DBL_MAX;
91 double x_max = -DBL_MAX;
92 double y_min = DBL_MAX;
93 double y_max = -DBL_MAX;
98 double ordinate_values[BINS];
100 struct hsh_iterator hi;
101 struct hsh_table *fh = frq_tab->data;
104 double interval_size = fabs(ch->data_right - ch->data_left) / ( BINS );
106 /* Find out the extremes of the x value
107 FIXME: These don't need to be calculated here, since the
108 calling routine should know them */
110 for ( frq = hsh_first(fh,&hi); frq != 0; frq = hsh_next(fh,&hi) )
112 if ( frq->v.f < x_min ) x_min = frq->v.f ;
113 if ( frq->v.f > x_max ) x_max = frq->v.f ;
116 double x_interval = fabs(x_max - x_min) / ( BINS - 1);
119 double abscissa_scale =
120 fabs( (ch->data_right - ch->data_left) / (x_max - x_min) ) ;
123 /* Find out the bin values */
124 for ( count = 0, d = x_min; d <= x_max ; d += x_interval, ++count )
129 for ( frq = hsh_first(fh,&hi); frq != 0; frq = hsh_next(fh,&hi) )
131 if ( frq->v.f >= d && frq->v.f < d + x_interval )
135 ordinate_values[count] = y ;
137 if ( y > y_max ) y_max = y ;
138 if ( y < y_min ) y_min = y;
141 y_tick = chart_rounded_tick( ( y_max - y_min ) / (double) (YTICKS - 1));
144 y_min = floor( y_min / y_tick ) * y_tick ;
145 y_max = ceil( y_max / y_tick ) * y_tick ;
147 double ordinate_scale =
148 fabs(ch->data_top - ch->data_bottom) / fabs(y_max - y_min) ;
151 /* Move to data bottom-left */
152 pl_move_r(ch->lp, ch->data_left, ch->data_bottom);
154 pl_savestate_r(ch->lp);
155 pl_fillcolorname_r(ch->lp, ch->fill_colour);
156 pl_filltype_r(ch->lp,1);
158 /* Draw the histogram */
159 for ( count = 0, d = x_min; d <= x_max ; d += x_interval, ++count )
161 const double x = count * interval_size ;
162 pl_savestate_r(ch->lp);
164 draw_tick (ch, TICK_ABSCISSA, x + (interval_size / 2.0 ), "%.1f",d);
166 pl_fboxrel_r(ch->lp, x, 0, x + interval_size,
167 ordinate_values[count] * ordinate_scale );
169 pl_restorestate_r(ch->lp);
172 pl_restorestate_r(ch->lp);
174 /* Put the y axis on */
175 for ( d = y_min; d <= y_max ; d += y_tick )
177 draw_tick (ch, TICK_ORDINATE, (d - y_min ) * ordinate_scale, "%g", d);
180 /* Write the abscissa label */
181 pl_move_r(ch->lp,ch->data_left, ch->abscissa_top);
182 pl_alabel_r(ch->lp,0,'t',var->label ? var->label : var->name);
185 /* Write the ordinate label */
186 pl_savestate_r(ch->lp);
187 pl_move_r(ch->lp,ch->data_bottom, ch->ordinate_right);
188 pl_textangle_r(ch->lp,90);
189 pl_alabel_r(ch->lp,0,0,"Frequency");
191 pl_restorestate_r(ch->lp);
194 chart_write_title(ch, title);
197 /* Write the legend */
198 write_legend(ch,norm);
203 /* Draw the normal curve */
206 pl_move_r(ch->lp, ch->data_left, ch->data_bottom);
207 for( d = ch->data_left;
208 d <= ch->data_right ;
209 d += (ch->data_right - ch->data_left) / 100.0)
211 const double x = (d - ch->data_left - interval_size / 2.0 ) /
212 abscissa_scale + x_min ;
214 pl_fcont_r(ch->lp, d,
216 norm->N * gaussian(x, norm->mean, norm->stddev)
220 pl_endpath_r(ch->lp);