100 lines
2.9 KiB
C++
100 lines
2.9 KiB
C++
|
|
/*********************************************************************
|
||
|
|
*
|
||
|
|
* 文 件: NormalDistribution.cpp
|
||
|
|
*
|
||
|
|
* 版权所有: Shanghai Baosight Software Co., Ltd.
|
||
|
|
* zoufuzhou
|
||
|
|
*********************************************************************/
|
||
|
|
|
||
|
|
#include <eqpalg/stat_tools/NormalDistribution.h>
|
||
|
|
#include <boost/accumulators/accumulators.hpp>
|
||
|
|
#include <boost/accumulators/statistics/stats.hpp>
|
||
|
|
#include <boost/accumulators/statistics/mean.hpp>
|
||
|
|
#include <boost/accumulators/statistics/moment.hpp>
|
||
|
|
#include <boost/accumulators/statistics/variance.hpp>
|
||
|
|
#include <boost/math/distibutions/normal.hpp>
|
||
|
|
#include <iDA/iDA.h>
|
||
|
|
#include <log4cplus/LOG.h>
|
||
|
|
#include <dao/DbStandardDBAX.h>
|
||
|
|
|
||
|
|
using namespace std;
|
||
|
|
using namespace boost::accumulators;
|
||
|
|
extern iDA::Connection cn;
|
||
|
|
using namespace log4cplus;
|
||
|
|
|
||
|
|
|
||
|
|
NormalDistribution::NormalDistribution(void)
|
||
|
|
{
|
||
|
|
m_avg = 0;
|
||
|
|
m_stddev = 0;
|
||
|
|
}
|
||
|
|
NormalDistribution::NormalDistribution(const vector<double> vdat)
|
||
|
|
{
|
||
|
|
this->ReSet(vdat);
|
||
|
|
}
|
||
|
|
|
||
|
|
void NormalDistribution::ReSet(const vector<double> vdat)
|
||
|
|
{
|
||
|
|
// accumulator_set<double, stats<tag::mean, tag::moment<2> > > acc;
|
||
|
|
accumulator_set<double, stats<tag::mean, tag::variance > > acc;
|
||
|
|
for(int i=0; i<vdat.size(); i++)
|
||
|
|
{
|
||
|
|
acc(vdat[i]);
|
||
|
|
}
|
||
|
|
m_avg = mean(acc);
|
||
|
|
m_stddev = sqrt(variance(acc));//boost::accumulators::moment<2>(acc);
|
||
|
|
}
|
||
|
|
|
||
|
|
//int NormalDistribution::ReadDBSample(const string& where)
|
||
|
|
//{
|
||
|
|
// LOG d("NormalDistribution::ReadDBSample");
|
||
|
|
// if( where.length() == 0) return -1;
|
||
|
|
// accumulator_set<double, stats<tag::mean, tag::moment<2> > > acc;
|
||
|
|
//
|
||
|
|
// string sql = "select data,datajson from T_RULE_SAMPLE WHERE "+where;
|
||
|
|
//
|
||
|
|
// iDA::Command cmd;
|
||
|
|
// cmd.SetConnection( &cn);
|
||
|
|
// cmd.SetCommandText( sql);
|
||
|
|
// try{
|
||
|
|
// cmd.Execute();
|
||
|
|
// }catch( iDA::Exception &e)
|
||
|
|
// {
|
||
|
|
// d.Error()<<sql<<endl;
|
||
|
|
// d.Error()<<e.ErrMsg()<<endl;
|
||
|
|
// return (-1);
|
||
|
|
// }
|
||
|
|
//
|
||
|
|
// while( cmd.FetchNext())
|
||
|
|
// {
|
||
|
|
// acc(cmd.Field(1).AsDouble());
|
||
|
|
// }
|
||
|
|
//
|
||
|
|
// m_avg = mean(acc);
|
||
|
|
// m_stddev = boost::accumulators::moment<2>(acc);
|
||
|
|
// return 0;
|
||
|
|
//}
|
||
|
|
|
||
|
|
int NormalDistribution::Analysis(double probability,ConfidenceInterval* cfdi)
|
||
|
|
{
|
||
|
|
LOG d("NormalDistribution::Analysis",AUTO_CATCH_PID);
|
||
|
|
d.Debug() << "m_avg:" << m_avg <<" m_stddev:" << m_stddev << std::endl;
|
||
|
|
if(probability < 0 || probability > 1 || m_stddev == 0)
|
||
|
|
{
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
boost::math::normal_distibution<> normal(m_avg, m_stddev);
|
||
|
|
d.Debug() << "Prob" << probability << std::endl;
|
||
|
|
// cfdi->up = boost::math::quantile(normal,probability) + m_stddev*1.0; // add 0.2 avg to up in order to increase mistake alarm
|
||
|
|
// cfdi->low = 2.0*m_avg - cfdi->up - m_stddev*1.0;// sub 0.2 avg to up in order to increase mistake alarm
|
||
|
|
cfdi->up = boost::math::quantile(normal,probability);
|
||
|
|
cfdi->low = 2.0*m_avg - cfdi->up;
|
||
|
|
double diff = fabs(cfdi->up-cfdi->low);
|
||
|
|
cfdi->up += diff*0.1;
|
||
|
|
cfdi->low -= diff*0.1;
|
||
|
|
cfdi->probability = probability;
|
||
|
|
cfdi->avg = m_avg;
|
||
|
|
cfdi->std_dev = m_stddev;
|
||
|
|
return 0;
|
||
|
|
}
|