STK++ 0.9.13
STK_Stat_Online.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff, Université Lille 1, Inria
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the
16 Free Software Foundation, Inc.,
17 59 Temple Place,
18 Suite 330,
19 Boston, MA 02111-1307
20 USA
21
22 Contact : S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
23*/
24
25/*
26 * Project: stkpp::STatistiK::StatDesc
27 * Purpose: Compute elementary 1D statistics for all variables.
28 * Author: Serge Iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 **/
30
35#ifndef STK_STAT_ONLINE_H
36#define STK_STAT_ONLINE_H
37
38#include <STKernel.h>
39
40namespace STK
41{
42namespace Stat
43{
44
45template < class Array, class Type> struct Online;
49template < class Array>
50struct Online<Array, Real>
51{
52 typedef typename Array::Type Type_;
53 enum
54 {
56 };
58 inline Online(): mean_(), variance_(), iter_(0)
59 { STK_STATIC_ASSERT(value_, YOU_CANNOT_USED_THIS_TYPE_OF_DATA_WITH_THIS_OBJECT);
60 release();
61 }
63 inline Online(Range const& range): mean_(range), variance_(range), iter_(0)
64 { STK_STATIC_ASSERT(value_, YOU_CANNOT_USED_THIS_TYPE_OF_DATA_WITH_THIS_OBJECT);
65 release();
66 }
68 inline Online(Range const& rows, Range const& cols): mean_(rows, cols), variance_(rows, cols), iter_(0)
69 { STK_STATIC_ASSERT(value_, YOU_CANNOT_USED_THIS_TYPE_OF_DATA_WITH_THIS_OBJECT);
70 release();
71 }
73 inline Online( Online const& stat): mean_(stat.mean_), variance_(stat.variance_), iter_(stat.iter_)
74 {}
76 Array const& mean() const { return mean_;}
78 Array variance() const
79 { return iter_ == 0 ? STK::Arithmetic<Real>::infinity() : variance_/iter_;}
81 inline void resize(Range const& range)
82 { mean_.resize(range) = 0.; variance_.resize(range) = 0.; iter_ =0;}
84 inline void resize(Range const& rows, Range const& cols)
85 { mean_.resize(rows, cols) = 0.; variance_.resize(rows, cols) = 0.; iter_ =0;}
87 inline void release() { mean_ = 0.; variance_ = 0.; iter_ = 0;}
92 inline void update(Array const& x)
93 {
94 iter_++;
95 Array delta = x - mean_;
96 mean_ += delta/iter_;
97 variance_ = variance_ + delta.prod(x - mean_);
98 }
102 inline Online& operator=( Online const& other)
103 {
104 mean_ = other.mean_;
105 variance_ = other.variance_;
106 iter_ = other.iter_;
107 return *this;
108 }
110 Array mean_;
114 int iter_;
115};
116
120template <>
122{
124 Online(): mean_(0.), variance_(0.), iter_(0) {}
126 Online( Online const& stat): mean_(stat.mean_), variance_(stat.variance_), iter_(stat.iter_)
127 {}
129 Real const& mean() const { return mean_;}
132 { return iter_ == 0 ? STK::Arithmetic<Real>::infinity() : variance_/iter_;}
134 inline void release() { mean_ = 0.; variance_ = 0.; iter_ = 0;}
139 inline void update(Real const& value)
140 {
141 iter_++;
142 Real delta = value - mean_;
143 mean_ += delta/iter_;
144 variance_ = variance_ + delta*(value - mean_);
145 }
149 inline Online& operator=( Online const& other)
150 {
151 mean_ = other.mean_;
152 variance_ = other.variance_;
153 iter_ = other.iter_;
154 return *this;
155 }
161 int iter_;
162};
163
164} // namespace Stat
165
166} // namespace STK
167
168#endif /*STK_STAT_ONLINE_H */
#define STK_STATIC_ASSERT(COND, MSG)
This file include all the header files of the project STKernel.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
Index sub-vector region: Specialization when the size is unknown.
Definition STK_Range.h:265
double Real
STK fundamental type of Real values.
The namespace STK is the main domain space of the Statistical ToolKit project.
Arithmetic properties of STK fundamental types.
void resize(Range const &range)
initialize one dimensional arrays
int iter_
number of stored values
Array mean_
mean of the parameters
Online(Range const &rows, Range const &cols)
constructor for two dimensional arrays
void update(Array const &x)
update the parameters using the current estimated parameters
Online(Online const &stat)
copy constructor
Online & operator=(Online const &other)
overwrite the statistics with other.
void resize(Range const &rows, Range const &cols)
initialize two dimensional arrays
void release()
release the computed parameters
Online(Range const &range)
constructor for one dimensional arrays
void update(Real const &value)
update the parameters using the current estimated parameters
Online & operator=(Online const &other)
overwrite the statistics with other.
Real mean_
on line mean of the variable
void release()
release the computed parameters
int iter_
number of stored values
Real variance_
on line variance times n of the variable
Online(Online const &stat)
copy constructor
check if T and U are of the same type.