拟合分布,拟合优度,p值。是否可以使用Scipy(Python)实现这一点?

拟合分布,拟合优度,p值。是否可以使用Scipy(Python)实现这一点?,python,numpy,scipy,statistics,probability,Python,Numpy,Scipy,Statistics,Probability,简介:我是一名生物信息学家。在我对所有人类基因(约20000个)进行的分析中,我搜索一个特定的短序列基序,以检查每个基因中该基序出现的次数 基因以四个字母(a、T、G、C)的线性序列“书写”。例如:CGTAGGGGTTTAC。。。这是四个字母的遗传密码,就像每个细胞的秘密语言,这是DNA储存信息的方式 我怀疑某些基因中特定短基序序列(AGTGGAC)的频繁重复在细胞的特定生化过程中至关重要。由于这个基序本身很短,用计算工具很难区分基因中真正的功能性例子和那些偶然看起来相似的例子。为了避免这个问题

简介:我是一名生物信息学家。在我对所有人类基因(约20000个)进行的分析中,我搜索一个特定的短序列基序,以检查每个基因中该基序出现的次数

基因以四个字母(a、T、G、C)的线性序列“书写”。例如:CGTAGGGGTTTAC。。。这是四个字母的遗传密码,就像每个细胞的秘密语言,这是DNA储存信息的方式

我怀疑某些基因中特定短基序序列(AGTGGAC)的频繁重复在细胞的特定生化过程中至关重要。由于这个基序本身很短,用计算工具很难区分基因中真正的功能性例子和那些偶然看起来相似的例子。为了避免这个问题,我获取了所有基因的序列,并将它们连接成一个字符串并进行了洗牌。存储每个原始基因的长度。然后,对于每个原始序列长度,通过重复地从串联序列中随机选取a、T、G或C并将其转移到随机序列来构建随机序列。这样,产生的随机序列集具有相同的长度分布,以及整体A、T、G、C组成。然后我在这些随机序列中搜索主题。我执行了1000次这个程序,并对结果取平均值

  • 15000个不包含给定基序的基因
  • 5000个包含1个基序的基因
  • 3000个包含2个基序的基因
  • 1000个包含3个基序的基因
  • 1个包含6个基序的基因
因此,即使对真正的遗传密码进行了1000次随机分组,也没有任何基因的基序超过6个。但在真正的遗传密码中,有一些基因含有超过20个出现的基序,这表明这些重复可能是功能性的,不太可能纯粹是偶然发现如此丰富的基序

问题:

我想知道在我的分布中找到一个模体出现20次的基因的概率。所以我想知道偶然发现这种基因的概率。我想用Python实现这一点,但我不知道如何实现

我可以用Python进行这样的分析吗

任何帮助都将不胜感激

您将找到所有已实现的连续分布函数的列表。每一个都有,返回相应的形状参数

即使您不知道要使用哪个发行版,也可以同时尝试许多发行版,并选择一个更适合您的数据的发行版,如下面的代码所示。请注意,如果您不知道分布情况,则可能很难拟合样本

import matplotlib.pyplot as plt
import scipy
import scipy.stats
size = 20000
x = scipy.arange(size)
# creating the dummy sample (using beta distribution)
y = scipy.int_(scipy.round_(scipy.stats.beta.rvs(6,2,size=size)*47))
# creating the histogram
h = plt.hist(y, bins=range(48))

dist_names = ['alpha', 'beta', 'arcsine',
              'weibull_min', 'weibull_max', 'rayleigh']

for dist_name in dist_names:
    dist = getattr(scipy.stats, dist_name)
    params = dist.fit(y)
    arg = params[:-2]
    loc = params[-2]
    scale = params[-1]
    if arg:
        pdf_fitted = dist.pdf(x, *arg, loc=loc, scale=scale) * size
    else:
        pdf_fitted = dist.pdf(x, loc=loc, scale=loc) * size
    plt.plot(pdf_fitted, label=dist_name)
    plt.xlim(0,47)
plt.legend(loc='upper left')
plt.show()

参考资料:


请注意,您已对问题进行了实质性修改。是否可以将此问题还原为您原来的问题,并为所有新细节添加一个清晰的“更新”部分?或者只是一个新问题?感谢您可能会问这个问题,我问一个新问题:上面的代码抛出了下面的消息错误:“AtditError:‘模块’对象在“DIST= GETATOR(SISPY.STATS,DistyNoX)”语句上没有属性“ARCHINEWEBULLLY MIN”。我的版本是:scipy是0.13.3,numpy是1.8.0,matplotlib是1.3.1。@srodriguex谢谢!有一个小的打字错误,我刚把它修好it@SaulloCastro如何将此
fit()
函数应用于3D曲面拟合,我刚刚使用
scipy.linalg.lstsq
实现了此功能?如何确认我对数据的拟合良好。谢谢。这个例子已经不能正常工作了,你能更新它吗?@Khris我会找时间来做的。如果您已经知道什么与新的SciPy包不兼容,请随意编辑。。。
import numpy as np
import pandas as pd
import scipy.stats as st
import statsmodels.api as sm
import matplotlib as mpl
import matplotlib.pyplot as plt
import math
import random

mpl.style.use("ggplot")

def danoes_formula(data):
    """
    DANOE'S FORMULA
    https://en.wikipedia.org/wiki/Histogram#Doane's_formula
    """
    N = len(data)
    skewness = st.skew(data)
    sigma_g1 = math.sqrt((6*(N-2))/((N+1)*(N+3)))
    num_bins = 1 + math.log(N,2) + math.log(1+abs(skewness)/sigma_g1,2)
    num_bins = round(num_bins)
    return num_bins

def plot_histogram(data, results, n):
    ## n first distribution of the ranking
    N_DISTRIBUTIONS = {k: results[k] for k in list(results)[:n]}

    ## Histogram of data
    plt.figure(figsize=(10, 5))
    plt.hist(data, density=True, ec='white', color=(63/235, 149/235, 170/235))
    plt.title('HISTOGRAM')
    plt.xlabel('Values')
    plt.ylabel('Frequencies')

    ## Plot n distributions
    for distribution, result in N_DISTRIBUTIONS.items():
        # print(i, distribution)
        sse = result[0]
        arg = result[1]
        loc = result[2]
        scale = result[3]
        x_plot = np.linspace(min(data), max(data), 1000)
        y_plot = distribution.pdf(x_plot, loc=loc, scale=scale, *arg)
        plt.plot(x_plot, y_plot, label=str(distribution)[32:-34] + ": " + str(sse)[0:6], color=(random.uniform(0, 1), random.uniform(0, 1), random.uniform(0, 1)))
    
    plt.legend(title='DISTRIBUTIONS', bbox_to_anchor=(1.05, 1), loc='upper left')
    plt.show()

def fit_data(data):
    ## st.frechet_r,st.frechet_l: are disbled in current SciPy version
    ## st.levy_stable: a lot of time of estimation parameters
    ALL_DISTRIBUTIONS = [        
        st.alpha,st.anglit,st.arcsine,st.beta,st.betaprime,st.bradford,st.burr,st.cauchy,st.chi,st.chi2,st.cosine,
        st.dgamma,st.dweibull,st.erlang,st.expon,st.exponnorm,st.exponweib,st.exponpow,st.f,st.fatiguelife,st.fisk,
        st.foldcauchy,st.foldnorm, st.genlogistic,st.genpareto,st.gennorm,st.genexpon,
        st.genextreme,st.gausshyper,st.gamma,st.gengamma,st.genhalflogistic,st.gilbrat,st.gompertz,st.gumbel_r,
        st.gumbel_l,st.halfcauchy,st.halflogistic,st.halfnorm,st.halfgennorm,st.hypsecant,st.invgamma,st.invgauss,
        st.invweibull,st.johnsonsb,st.johnsonsu,st.ksone,st.kstwobign,st.laplace,st.levy,st.levy_l,
        st.logistic,st.loggamma,st.loglaplace,st.lognorm,st.lomax,st.maxwell,st.mielke,st.nakagami,st.ncx2,st.ncf,
        st.nct,st.norm,st.pareto,st.pearson3,st.powerlaw,st.powerlognorm,st.powernorm,st.rdist,st.reciprocal,
        st.rayleigh,st.rice,st.recipinvgauss,st.semicircular,st.t,st.triang,st.truncexpon,st.truncnorm,st.tukeylambda,
        st.uniform,st.vonmises,st.vonmises_line,st.wald,st.weibull_min,st.weibull_max,st.wrapcauchy
    ]
    
    MY_DISTRIBUTIONS = [st.beta, st.expon, st.norm, st.uniform, st.johnsonsb, st.gennorm, st.gausshyper]

    ## Calculae Histogram
    num_bins = danoes_formula(data)
    frequencies, bin_edges = np.histogram(data, num_bins, density=True)
    central_values = [(bin_edges[i] + bin_edges[i+1])/2 for i in range(len(bin_edges)-1)]

    results = {}
    for distribution in MY_DISTRIBUTIONS:
        ## Get parameters of distribution
        params = distribution.fit(data)
        
        ## Separate parts of parameters
        arg = params[:-2]
        loc = params[-2]
        scale = params[-1]
    
        ## Calculate fitted PDF and error with fit in distribution
        pdf_values = [distribution.pdf(c, loc=loc, scale=scale, *arg) for c in central_values]
        
        ## Calculate SSE (sum of squared estimate of errors)
        sse = np.sum(np.power(frequencies - pdf_values, 2.0))
        
        ## Build results and sort by sse
        results[distribution] = [sse, arg, loc, scale]
        
    results = {k: results[k] for k in sorted(results, key=results.get)}
    return results
        
def main():
    ## Import data
    data = pd.Series(sm.datasets.elnino.load_pandas().data.set_index('YEAR').values.ravel())
    results = fit_data(data)
    plot_histogram(data, results, 5)

if __name__ == "__main__":
    main()