20 #ifndef _STATFACTORY_H_ 21 #define _STATFACTORY_H_ 32 #include <gsl/gsl_fit.h> 33 #include <gsl/gsl_errno.h> 34 #include <gsl/gsl_spline.h> 35 #include <gsl/gsl_rng.h> 36 #include <gsl/gsl_randist.h> 37 #include <gsl/gsl_cdf.h> 38 #include <gsl/gsl_statistics.h> 46 enum INTERPOLATION_TYPE {undefined=0,linear=1,polynomial=2,cspline=3,cspline_periodic=4,akima=5,akima_periodic=6};
48 enum DISTRIBUTION_TYPE {uniform=1,gaussian=2};
52 INTERPOLATION_TYPE getInterpolationType(
const std::string interpolationType){
53 std::map<std::string, INTERPOLATION_TYPE> m_interpMap;
55 return m_interpMap[interpolationType];
57 DISTRIBUTION_TYPE getDistributionType(
const std::string distributionType){
58 std::map<std::string, DISTRIBUTION_TYPE> m_distMap;
60 return m_distMap[distributionType];
62 static void allocAcc(gsl_interp_accel *&acc){
63 acc = gsl_interp_accel_alloc ();
66 static void getSpline(
const std::string type,
int size, gsl_spline *& spline){
67 std::map<std::string, INTERPOLATION_TYPE> m_interpMap;
69 switch(m_interpMap[type]){
71 spline=gsl_spline_alloc(gsl_interp_polynomial,size);
74 spline=gsl_spline_alloc(gsl_interp_cspline,size);
76 case(cspline_periodic):
77 spline=gsl_spline_alloc(gsl_interp_cspline_periodic,size);
80 spline=gsl_spline_alloc(gsl_interp_akima,size);
83 spline=gsl_spline_alloc(gsl_interp_akima_periodic,size);
87 spline=gsl_spline_alloc(gsl_interp_linear,size);
92 static int initSpline(gsl_spline *spline,
const double *x,
const double *y,
int size){
93 return gsl_spline_init (spline, x, y, size);
95 static double evalSpline(gsl_spline *spline,
double x, gsl_interp_accel *acc){
96 return gsl_spline_eval (spline, x, acc);
99 static gsl_rng* getRandomGenerator(
unsigned long int theSeed){
100 const gsl_rng_type * T;
104 r = gsl_rng_alloc (gsl_rng_mt19937);
105 gsl_rng_set(r,theSeed);
108 void getNodataValues(std::vector<double>& nodatav)
const{nodatav=m_noDataValues;};
109 bool isNoData(
double value)
const{
110 if(m_noDataValues.empty())
113 return find(m_noDataValues.begin(),m_noDataValues.end(),value)!=m_noDataValues.end();
115 unsigned int pushNodDataValue(
double noDataValue){
116 if(find(m_noDataValues.begin(),m_noDataValues.end(),noDataValue)==m_noDataValues.end())
117 m_noDataValues.push_back(noDataValue);
118 return m_noDataValues.size();
120 unsigned int setNoDataValues(std::vector<double> vnodata){
121 m_noDataValues=vnodata;
122 return m_noDataValues.size();
124 double getRandomValue(
const gsl_rng* r,
const std::string type,
double a=0,
double b=1)
const{
125 std::map<std::string, DISTRIBUTION_TYPE> m_distMap;
128 switch(m_distMap[type]){
130 randValue = a+gsl_rng_uniform(r);
134 randValue = a+gsl_ran_gaussian(r, b);
141 template<
class T> T mymin(
const typename std::vector<T>& v)
const;
142 template<
class T> T mymax(
const typename std::vector<T>& v)
const;
143 template<
class T> T mymin(
const typename std::vector<T>& v, T minConstraint)
const;
144 template<
class T> T mymax(
const typename std::vector<T>& v, T maxConstraint)
const;
145 template<
class T> T absmin(
const typename std::vector<T>& v)
const;
146 template<
class T> T absmax(
const typename std::vector<T>& v)
const;
149 template<
class T>
typename std::vector<T>::const_iterator mymin(
const typename std::vector<T>& v,
typename std::vector<T>::const_iterator begin,
typename std::vector<T>::const_iterator end)
const;
150 template<
class T>
typename std::vector<T>::iterator mymin(
const typename std::vector<T>& v,
typename std::vector<T>::iterator begin,
typename std::vector<T>::iterator end)
const;
151 template<
class T>
typename std::vector<T>::const_iterator mymin(
const typename std::vector<T>& v,
typename std::vector<T>::const_iterator begin,
typename std::vector<T>::const_iterator end, T minConstraint)
const;
152 template<
class T>
typename std::vector<T>::iterator mymin(
const typename std::vector<T>& v,
typename std::vector<T>::iterator begin,
typename std::vector<T>::iterator end, T minConstraint)
const;
153 template<
class T>
typename std::vector<T>::const_iterator mymax(
const std::vector<T>& v,
typename std::vector<T>::const_iterator begin,
typename std::vector<T>::const_iterator end)
const;
154 template<
class T>
typename std::vector<T>::iterator mymax(
const std::vector<T>& v,
typename std::vector<T>::iterator begin,
typename std::vector<T>::iterator end)
const;
155 template<
class T>
typename std::vector<T>::const_iterator mymax(
const std::vector<T>& v,
typename std::vector<T>::const_iterator begin,
typename std::vector<T>::const_iterator end, T maxConstraint)
const;
156 template<
class T>
typename std::vector<T>::iterator mymax(
const std::vector<T>& v,
typename std::vector<T>::iterator begin,
typename std::vector<T>::iterator end, T maxConstraint)
const;
157 template<
class T>
typename std::vector<T>::const_iterator absmin(
const std::vector<T>& v,
typename std::vector<T>::const_iterator begin,
typename std::vector<T>::const_iterator end)
const;
158 template<
class T>
typename std::vector<T>::const_iterator absmax(
const std::vector<T>& v,
typename std::vector<T>::const_iterator begin,
typename std::vector<T>::const_iterator end)
const;
160 template<
class T>
void minmax(
const std::vector<T>& v,
typename std::vector<T>::const_iterator begin,
typename std::vector<T>::const_iterator end, T& theMin, T& theMax)
const;
161 template<
class T> T sum(
const std::vector<T>& v)
const;
162 template<
class T>
double mean(
const std::vector<T>& v)
const;
163 template<
class T>
void eraseNoData(std::vector<T>& v)
const;
164 template<
class T>
unsigned int nvalid(
const std::vector<T>& v)
const;
165 template<
class T> T median(
const std::vector<T>& v)
const;
166 template<
class T>
double var(
const std::vector<T>& v)
const;
167 template<
class T>
double moment(
const std::vector<T>& v,
int n)
const;
168 template<
class T>
double cmoment(
const std::vector<T>& v,
int n)
const;
169 template<
class T>
double skewness(
const std::vector<T>& v)
const;
170 template<
class T>
double kurtosis(
const std::vector<T>& v)
const;
171 template<
class T>
void meanVar(
const std::vector<T>& v,
double& m1,
double& v1)
const;
172 template<
class T1,
class T2>
void scale2byte(
const std::vector<T1>& input, std::vector<T2>& output,
unsigned char lbound=0,
unsigned char ubound=255)
const;
173 template<
class T>
void distribution(
const std::vector<T>& input,
typename std::vector<T>::const_iterator begin,
typename std::vector<T>::const_iterator end, std::vector<double>& output,
int nbin, T &minimum, T &maximum,
double sigma=0,
const std::string &filename=
"")
const;
174 template<
class T>
void distribution(
const std::vector<T>& input, std::vector<double>& output,
int nbin,
double sigma=0,
const std::string &filename=
"")
const{distribution(input,input.begin(),input.end(),output,nbin,0,0,sigma,filename);};
175 template<
class T>
void distribution2d(
const std::vector<T>& inputX,
const std::vector<T>& inputY, std::vector< std::vector<double> >& output,
int nbin, T& minX, T& maxX, T& minY, T& maxY,
double sigma=0,
const std::string& filename=
"")
const;
176 template<
class T>
void cumulative (
const std::vector<T>& input,
typename std::vector<T>::const_iterator begin,
typename std::vector<T>::const_iterator end, std::vector<int>& output,
int nbin, T &minimum, T &maximum)
const;
177 template<
class T>
void percentiles (
const std::vector<T>& input,
typename std::vector<T>::const_iterator begin,
typename std::vector<T>::const_iterator end, std::vector<T>& output,
int nbin, T &minimum, T &maximum,
const std::string &filename=
"")
const;
178 template<
class T> T percentile(
const std::vector<T>& input,
typename std::vector<T>::const_iterator begin,
typename std::vector<T>::const_iterator end,
double percent, T minimum=0, T maximum=0)
const;
179 template<
class T>
void signature(
const std::vector<T>& input,
double& k,
double& alpha,
double& beta,
double e)
const;
180 void signature(
double m1,
double m2,
double& k,
double& alpha,
double& beta,
double e)
const;
181 template<
class T>
void normalize(
const std::vector<T>& input, std::vector<double>& output)
const;
182 template<
class T>
void normalize_pct(std::vector<T>& input)
const;
183 template<
class T>
double rmse(
const std::vector<T>& x,
const std::vector<T>& y)
const;
184 template<
class T>
double nrmse(
const std::vector<T>& x,
const std::vector<T>& y)
const;
185 template<
class T>
double cvrmse(
const std::vector<T>& x,
const std::vector<T>& y)
const;
186 template<
class T>
double correlation(
const std::vector<T>& x,
const std::vector<T>& y,
int delay=0)
const;
188 template<
class T>
double gsl_covariance(
const std::vector<T>& x,
const std::vector<T>& y)
const;
189 template<
class T>
double cross_correlation(
const std::vector<T>& x,
const std::vector<T>& y,
int maxdelay, std::vector<T>& z)
const;
190 template<
class T>
double linear_regression(
const std::vector<T>& x,
const std::vector<T>& y,
double &c0,
double &c1)
const;
191 template<
class T>
double linear_regression_err(
const std::vector<T>& x,
const std::vector<T>& y,
double &c0,
double &c1)
const;
192 template<
class T>
void interpolateNoData(
const std::vector<double>& wavelengthIn,
const std::vector<T>& input,
const std::string& type, std::vector<T>& output,
bool verbose=
false)
const;
193 template<
class T>
void interpolateUp(
const std::vector<double>& wavelengthIn,
const std::vector<T>& input,
const std::vector<double>& wavelengthOut,
const std::string& type, std::vector<T>& output,
bool verbose=
false)
const;
194 template<
class T>
void interpolateUp(
const std::vector<double>& wavelengthIn,
const std::vector< std::vector<T> >& input,
const std::vector<double>& wavelengthOut,
const std::string& type, std::vector< std::vector<T> >& output,
bool verbose=
false)
const;
197 template<
class T>
void interpolateUp(
const std::vector<T>& input, std::vector<T>& output,
int nbin)
const;
198 template<
class T>
void nearUp(
const std::vector<T>& input, std::vector<T>& output)
const;
199 template<
class T>
void interpolateUp(
double* input,
int dim, std::vector<T>& output,
int nbin);
200 template<
class T>
void interpolateDown(
const std::vector<T>& input, std::vector<T>& output,
int nbin)
const;
201 template<
class T>
void interpolateDown(
double* input,
int dim, std::vector<T>& output,
int nbin);
204 static void initMap(std::map<std::string, INTERPOLATION_TYPE>& m_interpMap){
206 m_interpMap[
"linear"]=linear;
207 m_interpMap[
"polynomial"]=polynomial;
208 m_interpMap[
"cspline"]=cspline;
209 m_interpMap[
"cspline_periodic"]=cspline_periodic;
210 m_interpMap[
"akima"]=akima;
211 m_interpMap[
"akima_periodic"]=akima_periodic;
213 static void initDist(std::map<std::string, DISTRIBUTION_TYPE>& m_distMap){
215 m_distMap[
"gaussian"]=gaussian;
216 m_distMap[
"uniform"]=uniform;
218 std::vector<double> m_noDataValues;
222 template<
class T>
inline typename std::vector<T>::const_iterator StatFactory::mymin(
const std::vector<T>& v,
typename std::vector<T>::const_iterator begin,
typename std::vector<T>::const_iterator end)
const 225 typename std::vector<T>::const_iterator tmpIt;
226 for(
typename std::vector<T>::const_iterator it = begin; it!=end; ++it){
240 else if(m_noDataValues.size())
241 return m_noDataValues[0];
243 std::string errorString=
"Error: no valid data found";
248 template<
class T>
inline typename std::vector<T>::iterator StatFactory::mymin(
const std::vector<T>& v,
typename std::vector<T>::iterator begin,
typename std::vector<T>::iterator end)
const 251 typename std::vector<T>::iterator tmpIt;
252 for (
typename std::vector<T>::const_iterator it = begin; it!=end; ++it){
266 else if(m_noDataValues.size())
267 return m_noDataValues[0];
269 std::string errorString=
"Error: no valid data found";
274 template<
class T>
inline typename std::vector<T>::const_iterator StatFactory::mymin(
const std::vector<T>& v,
typename std::vector<T>::const_iterator begin,
typename std::vector<T>::const_iterator end, T minConstraint)
const 277 typename std::vector<T>::const_iterator tmpIt;
278 T minValue=minConstraint;
279 for (
typename std::vector<T>::const_iterator it = begin; it!=end; ++it){
283 if((minConstraint<=*it)&&(*it<minValue)){
298 else if(m_noDataValues.size())
299 return m_noDataValues[0];
301 std::string errorString=
"Error: no valid data found";
306 template<
class T>
inline typename std::vector<T>::iterator StatFactory::mymin(
const std::vector<T>& v,
typename std::vector<T>::iterator begin,
typename std::vector<T>::iterator end, T minConstraint)
const 309 typename std::vector<T>::iterator tmpIt;
310 T minValue=minConstraint;
311 for (
typename std::vector<T>::const_iterator it = begin; it!=end; ++it){
315 if((minConstraint<=*it)&&(*it<minValue)){
321 if(*it<minConstraint)
330 else if(m_noDataValues.size())
331 return m_noDataValues[0];
333 std::string errorString=
"Error: no valid data found";
338 template<
class T>
inline typename std::vector<T>::const_iterator StatFactory::mymax(
const std::vector<T>& v,
typename std::vector<T>::const_iterator begin,
typename std::vector<T>::const_iterator end)
const 341 typename std::vector<T>::const_iterator tmpIt;
342 for (
typename std::vector<T>::iterator it = begin; it!=end; ++it){
356 else if(m_noDataValues.size())
357 return m_noDataValues[0];
359 std::string errorString=
"Error: no valid data found";
364 template<
class T>
inline typename std::vector<T>::iterator StatFactory::mymax(
const std::vector<T>& v,
typename std::vector<T>::iterator begin,
typename std::vector<T>::iterator end)
const 367 typename std::vector<T>::iterator tmpIt;
368 for (
typename std::vector<T>::iterator it = begin; it!=end; ++it){
386 template<
class T>
inline typename std::vector<T>::const_iterator StatFactory::mymax(
const std::vector<T>& v,
typename std::vector<T>::const_iterator begin,
typename std::vector<T>::const_iterator end, T maxConstraint)
const 389 typename std::vector<T>::const_iterator tmpIt;
390 T maxValue=maxConstraint;
391 for (
typename std::vector<T>::const_iterator it = begin; it!=end; ++it){
395 if((maxConstraint>=*it)&&(*it>maxValue)){
401 if(*it>maxConstraint)
414 template<
class T>
inline typename std::vector<T>::iterator StatFactory::mymax(
const std::vector<T>& v,
typename std::vector<T>::iterator begin,
typename std::vector<T>::iterator end, T maxConstraint)
const 417 typename std::vector<T>::iterator tmpIt=v.end();
418 T maxValue=maxConstraint;
419 for (
typename std::vector<T>::const_iterator it = begin; it!=end; ++it){
423 if((maxConstraint>=*it)&&(*it>maxValue)){
442 template<
class T>
inline T StatFactory::mymin(
const std::vector<T>& v)
const 446 std::string errorString=
"Error: vector is empty";
450 for (
typename std::vector<T>::const_iterator it = v.begin(); it!=v.end(); ++it){
464 else if(m_noDataValues.size())
465 return m_noDataValues[0];
467 std::string errorString=
"Error: no valid data found";
472 template<
class T>
inline T StatFactory::mymin(
const std::vector<T>& v, T minConstraint)
const 475 T minValue=minConstraint;
476 for (
typename std::vector<T>::const_iterator it = v.begin(); it!=v.end(); ++it){
480 if((minConstraint<=*it)&&(*it<minValue))
492 else if(m_noDataValues.size())
493 return m_noDataValues[0];
495 std::string errorString=
"Error: no valid data found";
500 template<
class T>
inline T StatFactory::mymax(
const std::vector<T>& v)
const 504 std::string errorString=
"Error: vector is empty";
508 for (
typename std::vector<T>::const_iterator it = v.begin(); it!=v.end(); ++it){
522 else if(m_noDataValues.size())
523 return m_noDataValues[0];
525 std::string errorString=
"Error: no valid data found";
530 template<
class T>
inline T StatFactory::mymax(
const std::vector<T>& v, T maxConstraint)
const 533 T maxValue=maxConstraint;
534 for (
typename std::vector<T>::const_iterator it = v.begin(); it!=v.end(); ++it){
538 if((*it<=maxConstraint)&&(*it>maxValue))
550 else if(m_noDataValues.size())
551 return m_noDataValues[0];
553 std::string errorString=
"Error: no valid data found";
558 template<
class T>
inline typename std::vector<T>::const_iterator StatFactory::absmin(
const std::vector<T>& v,
typename std::vector<T>::const_iterator begin,
typename std::vector<T>::const_iterator end)
const 562 std::string errorString=
"Error: vector is empty";
565 typename std::vector<T>::const_iterator tmpIt;
566 for (
typename std::vector<T>::const_iterator it = begin; it!=end; ++it){
570 if(abs(*tmpIt)>abs(*it))
584 template<
class T>
inline T StatFactory::absmin(
const std::vector<T>& v)
const 586 typename std::vector<T>::const_iterator tmpIt;
587 tmpIt=absmin(v, v.begin(), v.end());
590 else if(m_noDataValues.size())
591 return m_noDataValues[0];
593 std::string errorString=
"Error: no valid data found";
598 template<
class T>
inline typename std::vector<T>::const_iterator StatFactory::absmax(
const std::vector<T>& v,
typename std::vector<T>::const_iterator begin,
typename std::vector<T>::const_iterator end)
const 601 typename std::vector<T>::const_iterator tmpIt;
602 for (
typename std::vector<T>::const_iterator it = begin; it!=end; ++it){
606 if(abs(*tmpIt)<abs(*it))
620 template<
class T>
inline T StatFactory::absmax(
const std::vector<T>& v)
const 622 typename std::vector<T>::const_iterator tmpIt;
623 tmpIt=absmax(v, v.begin(), v.end());
626 else if(m_noDataValues.size())
627 return m_noDataValues[0];
629 std::string errorString=
"Error: no valid data found";
634 template<
class T>
inline void StatFactory::minmax(
const std::vector<T>& v,
typename std::vector<T>::const_iterator begin,
typename std::vector<T>::const_iterator end, T& theMin, T& theMax)
const 636 bool isConstraint=(theMax>theMin);
637 double minConstraint=theMin;
638 double maxConstraint=theMax;
640 for (
typename std::vector<T>::const_iterator it = begin; it!=end; ++it){
645 if(*it<minConstraint)
647 if(*it>maxConstraint)
657 if(*it<minConstraint)
659 if(*it>maxConstraint)
668 if(m_noDataValues.size()){
669 theMin=m_noDataValues[0];
670 theMax=m_noDataValues[0];
673 std::string errorString=
"Error: no valid data found";
679 template<
class T>
inline T StatFactory::sum(
const std::vector<T>& v)
const 682 typename std::vector<T>::const_iterator it;
684 for (it = v.begin(); it!= v.end(); ++it){
692 else if(m_noDataValues.size())
693 return m_noDataValues[0];
695 std::string errorString=
"Error: no valid data found";
700 template<
class T>
inline double StatFactory::mean(
const std::vector<T>& v)
const 702 typename std::vector<T>::const_iterator it;
704 unsigned int validSize=0;
705 for (it = v.begin(); it!= v.end(); ++it){
712 return static_cast<double>(tmpSum)/validSize;
713 else if(m_noDataValues.size())
714 return m_noDataValues[0];
716 std::string errorString=
"Error: no valid data found";
721 template<
class T>
inline void StatFactory::eraseNoData(std::vector<T>& v)
const 723 if(m_noDataValues.size()){
724 typename std::vector<T>::iterator it=v.begin();
734 template<
class T>
unsigned int StatFactory::nvalid(
const std::vector<T>& v)
const{
735 std::vector<T> tmpV=v;
740 template<
class T> T StatFactory::median(
const std::vector<T>& v)
const 742 std::vector<T> tmpV=v;
745 sort(tmpV.begin(),tmpV.end());
747 return tmpV[tmpV.size()/2];
749 return 0.5*(tmpV[tmpV.size()/2-1]+tmpV[tmpV.size()/2]);
751 else if(m_noDataValues.size())
752 return m_noDataValues[0];
754 std::string errorString=
"Error: no valid data found";
759 template<
class T>
double StatFactory::var(
const std::vector<T>& v)
const 761 typename std::vector<T>::const_iterator it;
762 unsigned int validSize=0;
765 for (it = v.begin(); it!= v.end(); ++it){
777 else if(m_noDataValues.size())
778 return m_noDataValues[0];
780 std::string errorString=
"Error: no valid data found";
785 template<
class T>
double StatFactory::moment(
const std::vector<T>& v,
int n)
const 787 unsigned int validSize=0;
788 typename std::vector<T>::const_iterator it;
791 for(it = v.begin(); it!= v.end(); ++it){
799 else if(m_noDataValues.size())
800 return m_noDataValues[0];
802 std::string errorString=
"Error: no valid data found";
808 template<
class T>
double StatFactory::cmoment(
const std::vector<T>& v,
int n)
const 810 unsigned int validSize=0;
811 typename std::vector<T>::const_iterator it;
814 for(it = v.begin(); it!= v.end(); ++it){
822 else if(m_noDataValues.size())
823 return m_noDataValues[0];
825 std::string errorString=
"Error: no valid data found";
830 template<
class T>
double StatFactory::skewness(
const std::vector<T>& v)
const 833 return cmoment(v,3)/pow(var(v),1.5);
836 template<
class T>
double StatFactory::kurtosis(
const std::vector<T>& v)
const 839 double m2=cmoment(v,2);
840 double m4=cmoment(v,4);
844 template<
class T>
void StatFactory::meanVar(
const std::vector<T>& v,
double& m1,
double& v1)
const 846 typename std::vector<T>::const_iterator it;
847 unsigned int validSize=0;
851 for (it = v.begin(); it!= v.end(); ++it){
863 else if(m_noDataValues.size()){
864 m1=m_noDataValues[0];
865 v1=m_noDataValues[0];
868 std::string errorString=
"Error: no valid data found";
873 template<
class T1,
class T2>
void StatFactory::scale2byte(
const std::vector<T1>& input, std::vector<T2>& output,
unsigned char lbound,
unsigned char ubound)
const 875 output.resize(input.size());
876 T1 minimum=mymin(input);
877 T1 maximum=mymax(input);
878 if(minimum>=maximum){
879 std::string errorString=
"Error: no valid data found";
882 double scale=(ubound-lbound)/(maximum-minimum);
884 for (
int i=0;i<input.size();++i){
885 output[i]=scale*(input[i]-(minimum))+lbound;
889 template<
class T>
void StatFactory::distribution(
const std::vector<T>& input,
typename std::vector<T>::const_iterator begin,
typename std::vector<T>::const_iterator end, std::vector<double>& output,
int nbin, T &minimum, T &maximum,
double sigma,
const std::string &filename)
const 893 minmax(input,begin,end,minimum,maximum);
894 if(minimum<maximum&&minimum>minValue)
896 if(minimum<maximum&&maximum<maxValue)
903 if(maximum<=minimum){
904 std::ostringstream s;
905 s<<
"Error: could not calculate distribution (min>=max)";
909 std::string errorString=
"Error: nbin not defined";
913 std::string errorString=
"Error: no valid data found";
916 if(output.size()!=nbin){
918 for(
int i=0;i<nbin;output[i++]=0);
921 typename std::vector<T>::const_iterator it;
922 for(it=begin;it!=end;++it){
936 for(
int ibin=0;ibin<nbin;++ibin){
937 double icenter=minimum+
static_cast<double>(maximum-minimum)*(ibin+0.5)/nbin;
938 double thePdf=gsl_ran_gaussian_pdf(*it-icenter, sigma);
939 output[ibin]+=thePdf;
946 else if(*it>minimum && *it<maximum)
947 theBin=
static_cast<int>(
static_cast<double>((nbin-1)*(*it)-minimum)/(maximum-minimum));
956 std::string errorString=
"Error: no valid data found";
959 else if(!filename.empty()){
960 std::ofstream outputfile;
961 outputfile.open(filename.c_str());
963 std::ostringstream s;
964 s<<
"Error opening distribution file , " << filename;
967 for(
int ibin=0;ibin<nbin;++ibin)
968 outputfile << minimum+static_cast<double>(maximum-minimum)*(ibin+0.5)/nbin <<
" " <<
static_cast<double>(output[ibin])/input.size() << std::endl;
973 template<
class T>
void StatFactory::distribution2d(
const std::vector<T>& inputX,
const std::vector<T>& inputY, std::vector< std::vector<double> >& output,
int nbin, T& minX, T& maxX, T& minY, T& maxY,
double sigma,
const std::string& filename)
const 976 std::ostringstream s;
977 s<<
"Error: inputX is empty";
980 if(inputX.size()!=inputY.size()){
981 std::ostringstream s;
982 s<<
"Error: inputX is empty";
985 unsigned long int npoint=inputX.size();
987 minmax(inputX,inputX.begin(),inputX.end(),minX,maxX);
989 std::ostringstream s;
990 s<<
"Error: could not calculate distribution (minX>=maxX)";
994 minmax(inputY,inputY.begin(),inputY.end(),minY,maxY);
996 std::ostringstream s;
997 s<<
"Error: could not calculate distribution (minY>=maxY)";
1001 std::ostringstream s;
1002 s<<
"Error: nbin must be larger than 1";
1005 output.resize(nbin);
1006 for(
int i=0;i<nbin;++i){
1007 output[i].resize(nbin);
1008 for(
int j=0;j<nbin;++j)
1013 for(
unsigned long int ipoint=0;ipoint<npoint;++ipoint){
1014 if(inputX[ipoint]==maxX)
1017 binX=
static_cast<int>(
static_cast<double>(inputX[ipoint]-minX)/(maxX-minX)*nbin);
1018 if(inputY[ipoint]==maxY)
1021 binY=
static_cast<int>(
static_cast<double>(inputY[ipoint]-minY)/(maxY-minY)*nbin);
1023 std::ostringstream s;
1024 s<<
"Error: binX is smaller than 0";
1027 if(output.size()<=binX){
1028 std::ostringstream s;
1029 s<<
"Error: output size must be larger than binX";
1033 std::ostringstream s;
1034 s<<
"Error: binY is smaller than 0";
1037 if(output.size()<=binY){
1038 std::ostringstream s;
1039 s<<
"Error: output size must be larger than binY";
1049 for(
int ibinX=0;ibinX<nbin;++ibinX){
1050 double centerX=minX+
static_cast<double>(maxX-minX)*ibinX/nbin;
1051 double pdfX=gsl_ran_gaussian_pdf(inputX[ipoint]-centerX, sigma);
1052 for(
int ibinY=0;ibinY<nbin;++ibinY){
1054 double centerY=minY+
static_cast<double>(maxY-minY)*ibinY/nbin;
1055 double pdfY=gsl_ran_gaussian_pdf(inputY[ipoint]-centerY, sigma);
1056 output[ibinX][binY]+=pdfX*pdfY;
1061 ++output[binX][binY];
1063 if(!filename.empty()){
1064 std::ofstream outputfile;
1065 outputfile.open(filename.c_str());
1067 std::ostringstream s;
1068 s<<
"Error opening distribution file , " << filename;
1071 for(
int binX=0;binX<nbin;++binX){
1072 outputfile << std::endl;
1073 for(
int binY=0;binY<nbin;++binY){
1075 if(nbin==maxX-minX+1)
1076 binValueX=minX+binX;
1078 binValueX=minX+
static_cast<double>(maxX-minX)*(binX+0.5)/nbin;
1080 if(nbin==maxY-minY+1)
1081 binValueY=minY+binY;
1083 binValueY=minY+
static_cast<double>(maxY-minY)*(binY+0.5)/nbin;
1085 value=
static_cast<double>(output[binX][binY])/npoint;
1086 outputfile << binValueX <<
" " << binValueY <<
" " << value << std::endl;
1096 template<
class T>
void StatFactory::percentiles (
const std::vector<T>& input,
typename std::vector<T>::const_iterator begin,
typename std::vector<T>::const_iterator end, std::vector<T>& output,
int nbin, T &minimum, T &maximum,
const std::string &filename)
const 1098 if(maximum<=minimum)
1099 minmax(input,begin,end,minimum,maximum);
1100 if(maximum<=minimum){
1101 std::ostringstream s;
1102 s<<
"Error: maximum must be at least minimum";
1106 std::ostringstream s;
1107 s<<
"Error: nbin must be larger than 1";
1111 std::ostringstream s;
1112 s<<
"Error: input is empty";
1115 output.resize(nbin);
1116 std::vector<T> inputSort;
1117 inputSort.assign(begin,end);
1118 typename std::vector<T>::iterator vit=inputSort.begin();
1119 while(vit!=inputSort.end()){
1120 if(*vit<minimum||*vit>maximum)
1121 inputSort.erase(vit);
1125 eraseNoData(inputSort);
1126 std::sort(inputSort.begin(),inputSort.end());
1127 vit=inputSort.begin();
1128 std::vector<T> inputBin;
1129 for(
int ibin=0;ibin<nbin;++ibin){
1131 while(inputBin.size()<inputSort.size()/nbin&&vit!=inputSort.end()){
1132 inputBin.push_back(*vit);
1135 if(inputBin.size()){
1136 output[ibin]=mymax(inputBin);
1139 if(!filename.empty()){
1140 std::ofstream outputfile;
1141 outputfile.open(filename.c_str());
1143 std::ostringstream s;
1144 s<<
"error opening distribution file , " << filename;
1147 for(
int ibin=0;ibin<nbin;++ibin)
1148 outputfile << ibin*100.0/nbin <<
" " << static_cast<double>(output[ibin])/input.size() << std::endl;
1154 template<
class T> T StatFactory::percentile(
const std::vector<T>& input,
typename std::vector<T>::const_iterator begin,
typename std::vector<T>::const_iterator end,
double percent, T minimum, T maximum)
const 1157 std::ostringstream s;
1158 s<<
"Error: input is empty";
1161 std::vector<double> inputSort;
1162 inputSort.assign(begin,end);
1163 typename std::vector<double>::iterator vit=inputSort.begin();
1164 while(vit!=inputSort.end()){
1165 if(maximum>minimum){
1166 if(*vit<minimum||*vit>maximum)
1167 inputSort.erase(vit);
1172 eraseNoData(inputSort);
1173 std::sort(inputSort.begin(),inputSort.end());
1174 return gsl_stats_quantile_from_sorted_data(&(inputSort[0]),1,inputSort.size(),percent/100.0);
1177 template<
class T>
void StatFactory::signature(
const std::vector<T>& input,
double&k,
double& alpha,
double& beta,
double e)
const 1179 double m1=moment(input,1);
1180 double m2=moment(input,2);
1181 signature(m1,m2,k,alpha,beta,e);
1185 template<
class T>
void StatFactory::normalize(
const std::vector<T>& input, std::vector<double>& output)
const{
1186 double total=sum(input);
1188 output.resize(input.size());
1189 for(
int index=0;index<input.size();++index)
1190 output[index]=input[index]/total;
1197 template<
class T>
void StatFactory::normalize_pct(std::vector<T>& input)
const{
1198 double total=sum(input);
1200 typename std::vector<T>::iterator it;
1201 for(it=input.begin();it!=input.end();++it)
1202 *it=100.0*(*it)/total;
1206 template<
class T>
double StatFactory::rmse(
const std::vector<T>& x,
const std::vector<T>& y)
const{
1207 if(x.size()!=y.size()){
1208 std::ostringstream s;
1209 s<<
"Error: x and y not equal in size";
1213 std::ostringstream s;
1214 s<<
"Error: x is empty";
1218 for(
int isample=0;isample<x.size();++isample){
1219 if(isNoData(x[isample])||isNoData(y[isample]))
1221 double e=x[isample]-y[isample];
1228 template<
class T>
double StatFactory::nrmse(
const std::vector<T>& x,
const std::vector<T>& y)
const{
1229 if(x.size()!=y.size()){
1230 std::ostringstream s;
1231 s<<
"Error: x and y not equal in size";
1235 std::ostringstream s;
1236 s<<
"Error: x is empty";
1239 std::vector<T> tmpX=x;
1241 std::vector<T> tmpY=y;
1243 double maxY=mymax(y);
1244 double minY=mymin(y);
1245 double rangeY=maxY-minY;
1247 for(
int isample=0;isample<x.size();++isample){
1248 double e=x[isample]-y[isample];
1251 return sqrt(mse)/rangeY;
1255 template<
class T>
double StatFactory::cvrmse(
const std::vector<T>& x,
const std::vector<T>& y)
const{
1256 if(x.size()!=y.size()){
1257 std::ostringstream s;
1258 s<<
"Error: x and y not equal in size";
1262 std::ostringstream s;
1263 s<<
"Error: x is empty";
1266 std::vector<T> tmpX=x;
1268 std::vector<T> tmpY=y;
1270 double maxY=mymax(tmpY);
1271 double minY=mymin(tmpY);
1272 double rangeY=maxY-minY;
1274 for(
int isample=0;isample<x.size();++isample){
1275 double e=x[isample]-y[isample];
1278 return sqrt(mse)/mean(tmpY);
1285 template<
class T>
double StatFactory::gsl_covariance(
const std::vector<T>& x,
const std::vector<T>& y)
const{
1286 return(gsl_stats_covariance(&(x[0]),1,&(y[0]),1,x.size()));
1290 template<
class T>
double StatFactory::correlation(
const std::vector<T>& x,
const std::vector<T>& y,
int delay)
const{
1296 meanVar(x,meanX,varX);
1297 meanVar(y,meanY,varY);
1298 double denom = sqrt(varX*varY);
1303 for (
int i=0;i<x.size();++i) {
1305 if (j < 0 || j >= y.size())
1307 else if(isNoData(x[i])||isNoData(y[j]))
1312 std::ostringstream s;
1313 s<<
"Error: i must be positive";
1317 std::ostringstream s;
1318 s<<
"Error: i must be smaller than x.size()";
1322 std::ostringstream s;
1323 s<<
"Error: j must be positive";
1327 std::ostringstream s;
1328 s<<
"Error: j must be smaller than y.size()";
1331 sXY += (x[i] - meanX) * (y[j] - meanY);
1335 double minSize=(x.size()<y.size())?x.size():y.size();
1336 return(sXY / denom / (minSize-1));
1338 else if(m_noDataValues.size())
1339 return m_noDataValues[0];
1341 std::string errorString=
"Error: no valid data found";
1350 template<
class T>
double StatFactory::cross_correlation(
const std::vector<T>& x,
const std::vector<T>& y,
int maxdelay, std::vector<T>& z)
const{
1352 double sumCorrelation=0;
1353 for (
int delay=-maxdelay;delay<maxdelay;delay++) {
1354 z.push_back(correlation(x,y,delay));
1355 sumCorrelation+=z.back();
1357 return sumCorrelation;
1361 template<
class T>
double StatFactory::linear_regression(
const std::vector<T>& x,
const std::vector<T>& y,
double &c0,
double &c1)
const{
1362 if(x.size()!=y.size()){
1363 std::ostringstream s;
1364 s<<
"Error: x and y not equal in size";
1368 std::ostringstream s;
1369 s<<
"Error: x is empty";
1376 gsl_fit_linear(&(x[0]),1,&(y[0]),1,x.size(),&c0,&c1,&cov00,&cov01,&cov11,&sumsq);
1377 return (1-sumsq/var(y)/(y.size()-1));
1381 template<
class T>
double StatFactory::linear_regression_err(
const std::vector<T>& x,
const std::vector<T>& y,
double &c0,
double &c1)
const{
1382 if(x.size()!=y.size()){
1383 std::ostringstream s;
1384 s<<
"Error: x and y not equal in size";
1388 std::ostringstream s;
1389 s<<
"Error: x is empty";
1396 gsl_fit_linear(&(x[0]),1,&(y[0]),1,x.size(),&c0,&c1,&cov00,&cov01,&cov11,&sumsq);
1397 return sqrt((sumsq)/(y.size()));
1403 template<
class T>
void StatFactory::interpolateNoData(
const std::vector<double>& wavelengthIn,
const std::vector<T>& input,
const std::string& type, std::vector<T>& output,
bool verbose)
const{
1404 if(wavelengthIn.empty()){
1405 std::ostringstream s;
1406 s<<
"Error: wavelengthIn is empty";
1409 std::vector<double> wavelengthOut=wavelengthIn;
1410 std::vector<T> validIn=input;
1411 if(input.size()!=wavelengthIn.size()){
1412 std::ostringstream s;
1413 s<<
"Error: x and y not equal in size";
1416 int nband=wavelengthIn.size();
1419 if(m_noDataValues.size()){
1420 typename std::vector<T>::iterator itValue=validIn.begin();
1421 typename std::vector<T>::iterator itWavelength=wavelengthOut.begin();
1422 while(itValue!=validIn.end()&&itWavelength!=wavelengthOut.end()){
1423 if(isNoData(*itValue)){
1424 validIn.erase(itValue);
1425 wavelengthOut.erase(itWavelength);
1432 if(validIn.size()>1){
1434 interpolateUp(wavelengthOut, validIn, wavelengthIn, type, output, verbose);
1447 template<
class T>
void StatFactory::interpolateUp(
const std::vector<double>& wavelengthIn,
const std::vector<T>& input,
const std::vector<double>& wavelengthOut,
const std::string& type, std::vector<T>& output,
bool verbose)
const{
1448 if(wavelengthIn.empty()){
1449 std::ostringstream s;
1450 s<<
"Error: wavelengthIn is empty";
1453 if(input.size()!=wavelengthIn.size()){
1454 std::ostringstream s;
1455 s<<
"Error: input and wavelengthIn not equal in size";
1458 if(wavelengthOut.empty()){
1459 std::ostringstream s;
1460 s<<
"Error: wavelengthOut is empty";
1463 int nband=wavelengthIn.size();
1465 gsl_interp_accel *acc;
1468 getSpline(type,nband,spline);
1470 assert(&(wavelengthIn[0]));
1471 assert(&(input[0]));
1472 int status=initSpline(spline,&(wavelengthIn[0]),&(input[0]),nband);
1474 std::string errorString=
"Could not initialize spline";
1477 for(
int index=0;index<wavelengthOut.size();++index){
1478 if(wavelengthOut[index]<*wavelengthIn.begin()){
1479 output.push_back(*(input.begin()));
1482 else if(wavelengthOut[index]>wavelengthIn.back()){
1483 output.push_back(input.back());
1486 double dout=evalSpline(spline,wavelengthOut[index],acc);
1487 output.push_back(dout);
1489 gsl_spline_free(spline);
1490 gsl_interp_accel_free(acc);
1525 template<
class T>
void StatFactory::interpolateUp(
const std::vector<T>& input, std::vector<T>& output,
int nbin)
const 1528 std::ostringstream s;
1529 s<<
"Error: input is empty";
1533 std::ostringstream s;
1534 s<<
"Error: nbin must be larger than 0";
1538 int dim=input.size();
1539 for(
int i=0;i<dim;++i){
1541 double left=input[i];
1543 double right=(i<dim-1)? input[i+1]:input[i];
1544 deltaX=(right-left)/static_cast<double>(nbin);
1545 for(
int x=0;x<nbin;++x){
1546 output.push_back(left+x*deltaX);
1550 output.push_back(input.back());
1555 template<
class T>
void StatFactory::nearUp(
const std::vector<T>& input, std::vector<T>& output)
const 1558 std::ostringstream s;
1559 s<<
"Error: input is empty";
1562 if(output.size()<input.size()){
1563 std::ostringstream s;
1564 s<<
"Error: output size is smaller than input size";
1567 int dimInput=input.size();
1568 int dimOutput=output.size();
1570 for(
int iin=0;iin<dimInput;++iin){
1571 for(
int iout=0;iout<dimOutput/dimInput;++iout){
1572 int indexOutput=iin*dimOutput/dimInput+iout;
1573 if(indexOutput>=output.size()){
1574 std::ostringstream s;
1575 s<<
"Error: indexOutput must be smaller than output.size()";
1578 output[indexOutput]=input[iin];
1584 template<
class T>
void StatFactory::interpolateUp(
double* input,
int dim, std::vector<T>& output,
int nbin)
1587 std::ostringstream s;
1588 s<<
"Error: nbin must be larger than 0";
1592 for(
int i=0;i<dim;++i){
1594 double left=input[i];
1596 double right=(i<dim-1)? input[i+1]:input[i];
1597 deltaX=(right-left)/static_cast<double>(nbin);
1598 for(
int x=0;x<nbin;++x){
1599 output.push_back(left+x*deltaX);
1603 output.push_back(input[dim-1]);
1608 template<
class T>
void StatFactory::interpolateDown(
const std::vector<T>& input, std::vector<T>& output,
int nbin)
const 1611 std::ostringstream s;
1612 s<<
"Error: input is empty";
1616 std::ostringstream s;
1617 s<<
"Error: nbin must be larger than 0";
1621 int dim=input.size();
1623 output.push_back(input[0]);
1624 for(
int i=1;i<dim;++i){
1629 output.push_back(input[i]);
1635 template<
class T>
void StatFactory::interpolateDown(
double* input,
int dim, std::vector<T>& output,
int nbin)
1638 std::ostringstream s;
1639 s<<
"Error: nbin must be larger than 0";
1644 output.push_back(input[0]);
1645 for(
int i=1;i<dim;++i){
1650 output.push_back(input[i]);