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
25 #include <gsl/gsl_histogram.h>
33 /* Number of bins in which to divide data */
36 /* The approximate no of ticks on the y axis */
40 #define M_PI ( 22.0 / 7.0 )
44 static double gaussian(double x, double mu, double sigma ) ;
48 gaussian(double x, double mu, double sigma )
50 return (exp( - (( x - mu )* (x - mu) / (2.0 * sigma * sigma) ))
51 / ( sigma * sqrt( M_PI * 2.0) )) ;
56 /* Write the legend of the chart */
58 histogram_write_legend(struct chart *ch, const struct normal_curve *norm)
61 pl_savestate_r(ch->lp);
63 sprintf(buf,"N = %.2f",norm->N);
64 pl_move_r(ch->lp, ch->legend_left, ch->data_bottom);
65 pl_alabel_r(ch->lp,0,'b',buf);
67 sprintf(buf,"Mean = %.1f",norm->mean);
68 pl_fmove_r(ch->lp,ch->legend_left,ch->data_bottom + ch->font_size * 1.5);
69 pl_alabel_r(ch->lp,0,'b',buf);
71 sprintf(buf,"Std. Dev = %.2f",norm->stddev);
72 pl_fmove_r(ch->lp,ch->legend_left,ch->data_bottom + ch->font_size * 1.5 * 2);
73 pl_alabel_r(ch->lp,0,'b',buf);
75 pl_restorestate_r(ch->lp);
78 static void hist_draw_bar(struct chart *ch, const gsl_histogram *hist, int bar);
82 hist_draw_bar(struct chart *ch, const gsl_histogram *hist, int bar)
88 const size_t bins = gsl_histogram_bins(hist);
89 const double x_pos = (ch->data_right - ch->data_left) * bar / (double) bins ;
90 const double width = (ch->data_right - ch->data_left) / (double) bins ;
93 assert ( 0 == gsl_histogram_get_range(hist, bar, &lower, &upper));
95 assert( upper >= lower);
97 height = gsl_histogram_get(hist, bar) *
98 (ch->data_top - ch->data_bottom) / gsl_histogram_max_val(hist);
100 pl_savestate_r(ch->lp);
101 pl_move_r(ch->lp,ch->data_left, ch->data_bottom);
102 pl_fillcolorname_r(ch->lp, ch->fill_colour);
103 pl_filltype_r(ch->lp,1);
108 x_pos + width, height);
110 pl_restorestate_r(ch->lp);
114 snprintf(buf,5,"%g",(upper + lower) / 2.0);
115 draw_tick(ch, TICK_ABSCISSA,
116 x_pos + width / 2.0, buf);
126 histogram_plot(const gsl_histogram *hist,
127 const char *factorname,
128 const struct normal_curve *norm, short show_normal)
135 bins = gsl_histogram_bins(hist);
137 chart_initialise(&ch);
138 chart_write_title(&ch, _("HISTOGRAM"));
140 chart_write_ylabel(&ch, _("Frequency"));
141 chart_write_xlabel(&ch, factorname);
143 chart_write_yscale(&ch, 0, gsl_histogram_max_val(hist), 5);
145 for ( i = 0 ; i < bins ; ++i )
146 hist_draw_bar(&ch, hist, i);
148 histogram_write_legend(&ch, norm);
152 /* Draw the normal curve */
155 double x_min, x_max, not_used ;
156 double abscissa_scale ;
157 double ordinate_scale ;
160 gsl_histogram_get_range(hist, 0, &x_min, ¬_used);
161 gsl_histogram_get_range(hist, bins - 1, &x_max, ¬_used);
163 abscissa_scale = (ch.data_right - ch.data_left) / (x_max - x_min);
164 ordinate_scale = (ch.data_top - ch.data_bottom) /
165 gsl_histogram_max_val(hist) ;
167 pl_move_r(ch.lp, ch.data_left, ch.data_bottom);
168 for( d = ch.data_left;
170 d += (ch.data_right - ch.data_left) / 100.0)
172 const double x = (d - ch.data_left) / abscissa_scale + x_min ;
175 ch.data_bottom + norm->N * ordinate_scale *
176 gaussian(x, norm->mean, norm->stddev)