🦋
Butterfly 用户手册
  • Introduction
  • 一 前言
  • 二 开始
    • 安装部署
    • 五分钟体验指南
    • 单机使用手册
    • 应用规范
      • handler specs
      • middleware specs
      • xingqiao_plugin specs
      • yiqiu_program specs
  • 三 客户端功能
    • MySQL 原生协议
    • MySQL ORM
    • Redis 原生协议
      • redis_config
      • redis_tls
    • Redis ORM
    • Redis mcpack
    • Localcache
    • Kazoo
  • 四 应用(通用服务)
    • API JSON 规范
    • 异步任务 BaiChuan(百川)
    • 任务调度 RuQi(如期)
    • 任务编排 XingQiao(星桥)
    • 配置管理 WuXing(五行)
    • 运筹决策 BaiCe(百策)
  • 五 部署运维
    • 单机容器化部署
    • 监控
    • 异常排查
      • CPU Load spike every 7 hours
    • 升级
    • 安全
    • 其他
  • 六 前端
    • butterfly_template
    • butterfly_fe
    • butterfly-admin(json2web)
      • amis
      • sso
      • pangu
    • NoahV
    • PyWebIO
  • 七 潘多拉魔盒
    • 装饰器
      • localcache_decorator
      • retry_decorator
      • custom_decorator
      • command2http_decorator
    • 算法
      • 算法-分位数
      • 算法-变异系数
    • 实用工具
      • host_util
      • shell_util
      • http_util
      • time_util
      • random_util
      • concurrent
      • jsonschema
      • blinker
      • toml
      • command_util
      • config_util
      • picobox
      • 对称加密
        • des
        • aes
      • ascii_art
        • ttable
        • chart
      • business_rules
      • python-mysql-replication
      • dict_util
    • 中间件
      • middleware_status
      • middleware_whitelist
    • test_handler.py
  • 八 最佳实践
    • 分布式架构
    • Code practice
    • Log practice
    • Daemon process
  • 附录
Powered by GitBook
On this page
  1. 七 潘多拉魔盒

算法

#!/usr/bin/python
# coding=utf8
"""
# Author: wangbin34
# Created Time : 2023-06-14 10:31:30

# File Name: lib_math.py
# Description:
    常用数学方法
"""
import math


def percentile(values, quantile):
    """
    百分位数

    Args:
        values: list, eg:[1,2,3,4]
        quantile: float, eg: 99.99
    """
    size = len(values)
    index = math.ceil((size * quantile) / 100)
    if index != 0:
        index = index - 1

    return sorted(values)[int(index)]


# Calculate the Arithmetic mean.
def mean(values):
    """
    平均值

    Args:
        values: list, eg:[1,2,3,4]
    """
    if not len(values):
        return 0

    arithmetic_mean = sum(values) / float(len(values))
    return arithmetic_mean

# Function to calculate the Standard Deviation.


def standard_deviation(values):
    """
    standard_deviation

    Args:
        values: list, eg:[1,2,3,4]
    """
    if not len(values):
        return 0

    value_mean = mean(values)
    value_sum = 0
    for value in values:
        value_sum += (value - value_mean)**2
    x = (value_sum / float(len(values))) ** 0.5
    return x


# Function to calculate the Coefficient of Variation
def coefficient_of_variation(values):
    """
    standard_deviation

    Args:
        values: list, eg:[1,2,3,4]
    """
    if not len(values):
        return 0

    value_mean = mean(values)
    coefficient_of_variation = (standard_deviation(values) / float(value_mean))
    return coefficient_of_variation
Previouscommand2http_decoratorNext算法-分位数

Last updated 1 month ago