142 lines
3.6 KiB
C
142 lines
3.6 KiB
C
|
|
/**
|
|||
|
|
* @file eqpalg/distribution/Dist.h
|
|||
|
|
* @brief 确定分布类型的程序
|
|||
|
|
* 根据输入的数据特征确定分布类型
|
|||
|
|
* 并给出对应的上下限
|
|||
|
|
* @author Cat (null.null.null@qq.com)
|
|||
|
|
* @version 0.1
|
|||
|
|
* @date 2021-08-02
|
|||
|
|
*
|
|||
|
|
* Company: Baosight Co. Ltd.
|
|||
|
|
* DO NOT COPY/USE WITHOUT PERMISSION
|
|||
|
|
*
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
#pragma once
|
|||
|
|
#include <dlib/statistics.h>
|
|||
|
|
#include <boost/math/distributions/chi_squared.hpp>
|
|||
|
|
#include <boost/math/distributions/normal.hpp>
|
|||
|
|
#include <boost/math/distributions/students_t.hpp>
|
|||
|
|
#include <boost/math/distributions/skew_normal.hpp>
|
|||
|
|
#include <algorithm>
|
|||
|
|
#include <memory>
|
|||
|
|
#include <tuple>
|
|||
|
|
#include <vector>
|
|||
|
|
|
|||
|
|
#include "mix_cc/type/range.h"
|
|||
|
|
|
|||
|
|
namespace distribution {
|
|||
|
|
|
|||
|
|
using boost::math::quantile;
|
|||
|
|
/**
|
|||
|
|
* @brief 分布验证和置信区间获取
|
|||
|
|
* 先根据Prob确定出错的最大可能性
|
|||
|
|
* 再根据最大可能性、数据分布区间,得到详细的数据作用范围
|
|||
|
|
* 根据数据的真实分布情况,重新调整参数,直到报警率小于目标数值
|
|||
|
|
* 目前只支持高斯分布类型的分布
|
|||
|
|
*/
|
|||
|
|
class Dist {
|
|||
|
|
public:
|
|||
|
|
enum class DistTypes {
|
|||
|
|
unknown,
|
|||
|
|
student_t,
|
|||
|
|
normal,
|
|||
|
|
skew_normal
|
|||
|
|
}; ///< 预设三种分布
|
|||
|
|
constexpr static double test_prec = 0.01;
|
|||
|
|
|
|||
|
|
protected:
|
|||
|
|
DistTypes dist_type_; ///< 分布类型
|
|||
|
|
bool valid_; ///< 可用性
|
|||
|
|
dlib::running_stats<double> rs_; ///< dlib
|
|||
|
|
mix_cc::float_range_t legal_range_; ///< 合法分布区间
|
|||
|
|
std::string rule_id_;
|
|||
|
|
std::shared_ptr<boost::math::normal> normal_; ///< 正态分布
|
|||
|
|
std::shared_ptr<boost::math::students_t> students_t_; ///< T分布
|
|||
|
|
std::shared_ptr<boost::math::skew_normal> skew_normal_; ///< 偏态分布
|
|||
|
|
|
|||
|
|
std::vector<double> warning_sample_; ///< 报警样本
|
|||
|
|
|
|||
|
|
double predefied_prob_ = 0.975; ///< 预定义的置信概率 3σ的大概置信度
|
|||
|
|
|
|||
|
|
double prob_ = 0.5;
|
|||
|
|
|
|||
|
|
public:
|
|||
|
|
/**
|
|||
|
|
* @brief 构建分布检测
|
|||
|
|
*/
|
|||
|
|
Dist(/* args */);
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief Destroy the Dist object
|
|||
|
|
*/
|
|||
|
|
~Dist();
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 得到分布类型
|
|||
|
|
* @return DistTypes
|
|||
|
|
*/
|
|||
|
|
DistTypes get_distribution_type() const;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 设置置信概率
|
|||
|
|
* @param prob 置信概率
|
|||
|
|
* @return int
|
|||
|
|
*/
|
|||
|
|
int set_predefined_prob(double prob);
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief Get the shifted prob object
|
|||
|
|
* @return double
|
|||
|
|
*/
|
|||
|
|
double get_shifted_prob() const;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief
|
|||
|
|
* 测试合法数据的分布类型和理想的置信区局,该函数只会根据合法数据进行判断
|
|||
|
|
* @param rs dlib::running_stats
|
|||
|
|
* @param tmp_data My Param doc
|
|||
|
|
* @return int
|
|||
|
|
*/
|
|||
|
|
int auto_test(dlib::running_stats<double> rs,
|
|||
|
|
const std::vector<double>& tmp_data);
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 设置报警样本(可选,用来验证第二类错误)
|
|||
|
|
* @param warning_sample My Param doc
|
|||
|
|
* @return int
|
|||
|
|
*/
|
|||
|
|
int set_warning_sample_optional(const std::vector<double>& warning_sample);
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 分布是否具有合理类型
|
|||
|
|
* @return true
|
|||
|
|
* @return false
|
|||
|
|
*/
|
|||
|
|
bool valid() const;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief 得到合法范围大小
|
|||
|
|
* @return mix_cc::float_range_t
|
|||
|
|
*/
|
|||
|
|
mix_cc::float_range_t get_range() const;
|
|||
|
|
|
|||
|
|
protected:
|
|||
|
|
/**
|
|||
|
|
* @brief 得到第一类错误的概率
|
|||
|
|
* @param prob 置信概率
|
|||
|
|
* @param tmp_data 用来验证的临时数据
|
|||
|
|
* @return double
|
|||
|
|
*/
|
|||
|
|
double get_error_rate_type_1(const double& prob,
|
|||
|
|
const std::vector<double>& tmp_data);
|
|||
|
|
/**
|
|||
|
|
* @brief 得到指定置信概率的区间
|
|||
|
|
* @param prob 置信概率
|
|||
|
|
* @return mix_cc::float_range_t
|
|||
|
|
*/
|
|||
|
|
mix_cc::float_range_t get_range(double prob);
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
} // namespace distribution
|