GaussDB(DWS) DMS历史监控数据聚合算法java实现

举报
pchatcq 发表于 2021/01/18 14:22:41 2021/01/18
【摘要】 GaussDB(DWS) DMS在性能监控时,有聚合数据的需求,JDK自带的SummaryStatistics类自带求和、最大值、最小值、平均值等聚合算法,要实现均值、方差、峰值、偏度等数据聚合方式,需要一个继承SummaryStatistics子类,并在子类中实现相关的算法。代码示例如下:import java.util.DoubleSummaryStatistics;/** * @Aut...

GaussDB(DWS) DMS在性能监控时,有聚合数据的需求,JDK自带的SummaryStatistics类自带求和、最大值、最小值、平均值等聚合算法,要实现均值、方差、峰值、偏度等数据聚合方式,需要一个继承SummaryStatistics子类,并在子类中实现相关的算法。

代码示例如下:

import java.util.DoubleSummaryStatistics;

/**
 * @Description: Double流统计算法
 */
public class AggregationDoubleStatistics extends DoubleSummaryStatistics {

    private double m1;
    private double m2;
    private double m3;
    private double m4;

    @Override
    public void accept(double x) {
        super.accept(x);

        long n = getCount();

        //δ
        double delta = x - m1;
        //δ / n
        double deltaN = delta / n;
        //δ^2 / n
        double delta2N = delta * deltaN;
        //δ^2 / n^2
        double delta2N2 = deltaN * deltaN;
        //δ^3 / n^2
        double delta3N2 = delta2N * deltaN;
        //δ^4 / n^3
        double delta4N3 = delta3N2 * deltaN;

        m4 += (n - 1) * (n * n - 3 * n + 3) * delta4N3
                + 6 * m2 * delta2N2
                - 4 * m3 * deltaN;
        m3 += (n - 1) * (n - 2) * delta3N2
                - 3 * m2 * deltaN;
        m2 += (n - 1) * delta2N;
        m1 += deltaN;
    }

    @Override
    public void combine(DoubleSummaryStatistics other) {
        throw new UnsupportedOperationException(
                "Can't combine a standard DoubleSummaryStatistics with this class");
    }

    public void combine(MoreDoubleStatistics other) {
        MoreDoubleStatistics s1 = this;
        MoreDoubleStatistics s2 = other;

        long n1 = s1.n();
        long n2 = s2.n();
        long n = n1 + n2;

        //δ
        double delta = s2.m1 - s1.m1;
        //δ / n
        double deltaN = delta / n;
        //δ^2 / n
        double delta2N = delta * deltaN;
        //δ^2 / n^2
        double delta2N2 = deltaN * deltaN;
        //δ^3 / n^2
        double delta3N2 = delta2N * deltaN;
        //δ^4 / n^3
        double delta4N3 = delta3N2 * deltaN;

        this.m4 = s1.m4 + s2.m4 + n1 * n2 * (n1 * n1 - n1 * n2 + n2 * n2) * delta4N3
                + 6.0 * (n1 * n1 * s2.m2 + n2 * n2 * s1.m2) * delta2N2
                + 4.0 * (n1 * s2.m3 - n2 * s1.m3) * deltaN;

        this.m3 = s1.m3 + s2.m3 + n1 * n2 * (n1 - n2) * delta3N2
                + 3.0 * (n1 * s2.m2 - n2 * s1.m2) * deltaN;

        this.m2 = s1.m2 + s2.m2 + n1 * n2 * delta2N;

        this.m1 = s1.m1 + n2 * delta;

        super.combine(other);
    }

    private long n() {
        return getCount();
    }

    /**
     * 计算流的方差
     * @return
     */
    public double getVariance() {
        return n() <= 1 ? 0 : m2 / (n() - 1);
    }

    /**
     * 计算流的标准差
     * @return
     */
    public double getStdDev() {
        return Math.sqrt(getVariance());
    }

    /**
     * 计算流的偏度
     * @return
     */
    public double getSkewness() {
        return m2 == 0 ? 0 : Math.sqrt(n()) * m3 / Math.pow(m2, 1.5);
    }

    /**
     * 计算流的峰度
     * @return
     */
    public double getKurtosis() {
        return m2 == 0 ? 0 : n() * m4 / (m2 * m2) - 3.0;
    }
}



使用示例:

private void getAggregatedData(List<Double> list) {

    MoreDoubleStatistics doubleStatistics =  list
            .stream()
            .mapToDouble(t -> t)
            .collect(MoreDoubleStatistics::new, MoreDoubleStatistics::accept, MoreDoubleStatistics::combine);

    double maxAggregatedData = doubleStatistics.getMax();
    double minAggregatedData = doubleStatistics.getMin();
    double avgAggregatedData = doubleStatistics.getAverage();
    double sumAggregatedData =  doubleStatistics.getSum();
    double varAggregatedData =  doubleStatistics.getVariance();
}



【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。