# 算法

```
#!/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
```
