简体   繁体   中英

How to compute the Mass distribution in Python?

Mass distribution is defined as follows.

质量分布

f is the probability density function of a continuous variable.

Given a set of data values, which are saved in a list, how to approximate this function? Since the integrals in the numerator and the denominator are identical to the expected value of a distribution, can we use the sample mean based approach as follows?

def get_mass_distribution(values):
    x = np.linspace(0, max(values), max(values))
    mean = sum(values)/len(values)
    mass = []
    values.sort()
    for i in range(len(values)):
        mass.append(sum(values[0:i+1])/(mean*(i+1)))

    return x, mass

You should use trapezoidal rule to approximate this integral.

def get_mass_distribution(data):
    a = np.array(data)
    ag = st.gaussian_kde(a)

    denom_integral = trapezoidal(ag, 0, max(data), max(data)*10)

    Fm = [0]

    x = []
    k = 0
    while(k < max(data)):
        x.append(k)
        k = k+1


    for i in x[1:]:
        enum_integral = trapezoidal(ag, 0, i, i*10)
        Fm.append(enum_integral/denom_integral)

    return x, Fm

def trapezoidal(ag, a, b, n):
    h = float(b - a) / n
    s = 0.0
    s += a*ag(a)[0]/2.0
    for i in range(1, n):
        s += (a + i*h)*ag(a + i*h)[0]
    s += b*ag(b)[0]/2.0
    return s * h

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM