summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: bba6703)
raw | patch | inline | side by side (parent: bba6703)
author | oetiker <oetiker@a5681a0c-68f1-0310-ab6d-d61299d08faa> | |
Thu, 4 Aug 2005 05:08:04 +0000 (05:08 +0000) | ||
committer | oetiker <oetiker@a5681a0c-68f1-0310-ab6d-d61299d08faa> | |
Thu, 4 Aug 2005 05:08:04 +0000 (05:08 +0000) |
-- Trent Burkard <tburkard with tangentis.com>
git-svn-id: svn://svn.oetiker.ch/rrdtool/branches/1.2/program@669 a5681a0c-68f1-0310-ab6d-d61299d08faa
git-svn-id: svn://svn.oetiker.ch/rrdtool/branches/1.2/program@669 a5681a0c-68f1-0310-ab6d-d61299d08faa
doc/rrdgraph_rpn.pod | patch | blob | history | |
src/rrd_graph.c | patch | blob | history | |
src/rrd_graph.h | patch | blob | history |
diff --git a/doc/rrdgraph_rpn.pod b/doc/rrdgraph_rpn.pod
index 74ac4ad40751ad898c783bd0185034c7673a0507..1aaef65302d556e087acf66b36bf1e40adb6f726 100644 (file)
--- a/doc/rrdgraph_rpn.pod
+++ b/doc/rrdgraph_rpn.pod
Example: C<VDEF:perc95=mydata,95,PERCENT>
+=item LSLSLOPE, LSLINT, LSLCORREL
+
+Return the parameters for a B<L>east B<S>quares B<L>ine I<(y = mx +b)>
+which approximate the provided dataset. LSLSLOPE is the slope I<(m)> of
+the line related to the COUNT position of the data. LSLINT is the
+y-intercept I<(b)>, which happens also to be the first data point on the
+graph. LSLCORREL is the Correlation Coefficient (also know as Pearson's
+Product Moment Correlation Coefficient). It will range from 0 to +/-1
+and represents the quality of fit for the approximation.
+
+Example: C<VDEF:slope=mydata,LSLSLOPE>
+
=back
=head1 SEE ALSO
diff --git a/src/rrd_graph.c b/src/rrd_graph.c
index 2a13cb54e8ed62bf461b1914b3be57d31fcad833..3ce39598c8b5d28e7d54ce3f87f4fc9e30d4916f 100644 (file)
--- a/src/rrd_graph.c
+++ b/src/rrd_graph.c
else if (!strcmp("TOTAL", func)) gdes->vf.op = VDEF_TOTAL;
else if (!strcmp("FIRST", func)) gdes->vf.op = VDEF_FIRST;
else if (!strcmp("LAST", func)) gdes->vf.op = VDEF_LAST;
+ else if (!strcmp("LSLSLOPE", func)) gdes->vf.op = VDEF_LSLSLOPE;
+ else if (!strcmp("LSLINT", func)) gdes->vf.op = VDEF_LSLINT;
+ else if (!strcmp("LSLCORREL",func)) gdes->vf.op = VDEF_LSLCORREL;
else {
rrd_set_error("Unknown function '%s' in VDEF '%s'\n"
,func
case VDEF_TOTAL:
case VDEF_FIRST:
case VDEF_LAST:
+ case VDEF_LSLSLOPE:
+ case VDEF_LSLINT:
+ case VDEF_LSLCORREL:
if (isnan(param)) {
gdes->vf.param = DNAN;
gdes->vf.val = DNAN;
dst->vf.when = src->start + (step+1)*src->step;
}
break;
+ case VDEF_LSLSLOPE:
+ case VDEF_LSLINT:
+ case VDEF_LSLCORREL:{
+ /* Bestfit line by linear least squares method */
+
+ int cnt=0;
+ double SUMx, SUMy, SUMxy, SUMxx, SUMyy, slope, y_intercept, correl ;
+ SUMx = 0; SUMy = 0; SUMxy = 0; SUMxx = 0; SUMyy = 0;
+
+ for (step=0;step<steps;step++) {
+ if (finite(data[step*src->ds_cnt])) {
+ cnt++;
+ SUMx += step;
+ SUMxx += step * step;
+ SUMxy += step * data[step*src->ds_cnt];
+ SUMy += data[step*src->ds_cnt];
+ SUMyy += data[step*src->ds_cnt]*data[step*src->ds_cnt];
+ };
+ }
+
+ slope = ( SUMx*SUMy - cnt*SUMxy ) / ( SUMx*SUMx - cnt*SUMxx );
+ y_intercept = ( SUMy - slope*SUMx ) / cnt;
+ correl = (SUMxy - (SUMx*SUMy)/cnt) / sqrt((SUMxx - (SUMx*SUMx)/cnt)*(SUMyy - (SUMy*SUMy)/cnt));
+
+ if (cnt) {
+ if (dst->vf.op == VDEF_LSLSLOPE) {
+ dst->vf.val = slope;
+ dst->vf.when = cnt*src->step;
+ } else if (dst->vf.op == VDEF_LSLINT) {
+ dst->vf.val = y_intercept;
+ dst->vf.when = cnt*src->step;
+ } else if (dst->vf.op == VDEF_LSLCORREL) {
+ dst->vf.val = correl;
+ dst->vf.when = cnt*src->step;
+ };
+
+ } else {
+ dst->vf.val = DNAN;
+ dst->vf.when = 0;
+ }
+ }
+ break;
}
return 0;
}
diff --git a/src/rrd_graph.h b/src/rrd_graph.h
index 9ced52ee71fb5e60f59b564f9c72e24d75564d8e..01b6799c72c2f669795955d504ef6907ad2b5f82 100644 (file)
--- a/src/rrd_graph.h
+++ b/src/rrd_graph.h
,VDEF_TOTAL /* average multiplied by time */
,VDEF_FIRST /* first non-unknown value and time */
,VDEF_LAST /* last non-unknown value and time */
+ ,VDEF_LSLSLOPE /* least squares line slope */
+ ,VDEF_LSLINT /* least squares line y_intercept */
+ ,VDEF_LSLCORREL /* least squares line correlation coefficient */
};
enum text_prop_en { TEXT_PROP_DEFAULT=0, /* default settings */
TEXT_PROP_TITLE, /* properties for the title */