Exemplo n.º 1
0
def plotNull(subp, nu0=0.005, fontsize=5):
    obs = pd.read_pickle(utl.outpath + 'markov/neutral.obs.{}.pkl'.format(nu0))
    T = Markov.computeTransition(0, N=1000)

    dfplt = pd.concat([pd.Series({'scale': 10, 'xlim': [0.0, 0.01], 'ylim': [0, 1]}, name=(0.005, 1)),
                       pd.Series({'scale': 30, 'xlim': [0.06, 0.14], 'ylim': [0, 0.15]}, name=(0.1, 1)),
                       pd.Series({'scale': 30, 'xlim': [0.0, 0.015], 'ylim': [0, 0.3]}, name=(0.005, 10)),
                       pd.Series({'scale': 45, 'xlim': [0.0, 0.2], 'ylim': [0, 0.025]}, name=(0.1, 10)),
                      pd.Series({'scale':30, 'xlim':[0.0,0.03],'ylim': [0,0.2]},name=(0.005,100)),pd.Series({'scale':50, 'xlim':[0.00,0.4],'ylim': [0,0.004]},name=(0.1,100))
                         ],axis=1).T

    markov=T.loc[nu0].copy(True);markov.name='Markov Chain'
    xx=np.arange(0,1,0.00001)
    N=200; tau=1;h=2*nu0*(1-nu0);sig2=h*tau/N;brownian=stats.norm(nu0, sig2).pdf(xx);
    brownian=pd.Series(brownian,index=xx);brownian/=brownian.sum();brownian.name='Brownian Motion';brownian*=dfplt.loc[(nu0,tau)].scale
    pplt.setSize(plt.gca(), fontsize=fontsize)
    plt.subplot(3, 3, subp[0]);
    brownian.plot(color='r');
    markov.plot(color='b');
    o=pd.Series(obs.X[1].flatten()).value_counts().sort_index();o=o/o.sum();
    if nu0==0.1:
        counts,limits=np.histogram(obs.X[1].flatten(),bins=500,range=[0,1]);centers = 0.5*(limits[1:]+limits[:-1]);o=pd.Series(counts,index=centers);o=o/(obs.X.shape[1]*obs.X.shape[2]*4)
    o.plot(color='g')
    plt.xlim(dfplt.loc[(nu0, tau)].xlim);
    plt.ylim(dfplt.loc[(nu0, tau)].ylim);
    plt.locator_params(nbins=3)
    pplt.annotate(r'$s=0$, $\nu_0=${}, $\tau$={}'.format(nu0, tau), fontsize=fontsize)
    plt.ylabel(r'$P(\nu_\tau|\nu_0)$')
    tau=10
    for _ in range(9):
        markov=markov.dot(T)
    N=200;h=2*nu0*(1-nu0);sig2=h*tau/N;brownian=stats.norm(nu0, sig2).pdf(xx)
    brownian=pd.Series(brownian,index=xx);brownian/=brownian.sum();brownian.name='Brownian Motion';
    brownian*=dfplt.loc[(nu0,tau)].scale
    pplt.setSize(plt.gca(), fontsize=fontsize)
    plt.title('({})'.format(subptitle[subp[0] - 1]), fontsize=fontsize)
    plt.subplot(3, 3, subp[1]);
    brownian.plot(color='r');
    markov.plot(color='b');
    o=pd.Series(obs.X[10].flatten()).value_counts().sort_index();o=o/o.sum();
    if nu0==0.1:
        counts,limits=np.histogram(obs.X[10].flatten(),bins=100,range=[0,1]);centers = 0.5*(limits[1:]+limits[:-1]);o=pd.Series(counts,index=centers);o=o/(obs.X.shape[1]*obs.X.shape[2]*20)
    o.plot(color='g')
    plt.xlim(dfplt.loc[(nu0, tau)].xlim);
    plt.ylim(dfplt.loc[(nu0, tau)].ylim);
    plt.locator_params(nbins=3)
    pplt.annotate(r'$s=0$, $\nu_0=${}, $\tau$={}'.format(nu0, tau), loc=1, fontsize=fontsize)
    pplt.setSize(plt.gca(), fontsize=fontsize)



    tau=100
    for _ in range(90):
        markov=markov.dot(T)
    N=200;h=2*nu0*(1-nu0);sig2=h*tau/N;brownian=stats.norm(nu0, sig2).pdf(xx)
    brownian=pd.Series(brownian,index=xx);brownian/=brownian.sum();brownian.name='Brownian Motion';
    brownian*=dfplt.loc[(nu0,tau)].scale
    plt.title('({})'.format(subptitle[subp[1] - 1]), fontsize=fontsize)
    plt.subplot(3, 3, subp[2]);
    brownian.plot(color='r');
    markov.plot(color='b')
    o=pd.Series(obs.X[100].flatten()).value_counts().sort_index();o=o/o.sum();
    if nu0==0.1:
        counts,limits=np.histogram(obs.X[100].flatten(),bins=30,range=[0,1]);centers = 0.5*(limits[1:]+limits[:-1]);o=pd.Series(counts,index=centers);o=o/(obs.X.shape[1]*obs.X.shape[2]*60)
    o.name = 'Observation';
    o.plot(color='g')
    plt.xlim(dfplt.loc[(nu0, tau)].xlim);
    plt.ylim(dfplt.loc[(nu0, tau)].ylim);
    plt.locator_params(nbins=3)
    pplt.annotate(r'$s=0$, $\nu_0=${}, $\tau$={}'.format(nu0, tau), loc=1, fontsize=fontsize)
    if subp[2] == 3:
        plt.legend(loc='center right', fontsize=fontsize)
    pplt.setSize(plt.gca(), fontsize=fontsize)
    plt.title('({})'.format(subptitle[subp[2] - 1]), fontsize=fontsize)
Exemplo n.º 2
0
def plotAlternative(subp, s=0.1, fontsize=5):
    nu0=0.005
    obs = pd.read_pickle(utl.outpath + 'markov/T100.S{:03.0f}.obs.df'.format(s * 1000))
    T = Markov.computeTransition(s, 1000)

    dfplt= pd.concat([pd.Series({'scale':10, 'xlim':[0.0,0.01],'ylim': [0,0.2]},name=(0.005,1)),pd.Series({'scale':30, 'xlim':[0.06,0.14],'ylim': [0,0.15]},name=(0.1,1)),
                        pd.Series({'scale':30, 'xlim':[0.0,0.015],'ylim': [0,0.15]},name=(0.005,10)),pd.Series({'scale':45, 'xlim':[0.0,0.2],'ylim': [0,0.025]},name=(0.1,10)),
                      pd.Series({'scale':30, 'xlim':[0.0,1],'ylim': [0,0.01]},name=(0.005,100)),pd.Series({'scale':50, 'xlim':[0.00,0.4],'ylim': [0,0.004]},name=(0.1,100))
                         ],axis=1).T

    markov=T.loc[nu0].copy(True);markov.name='Markov Chain'

    plt.subplot(3, 3, subp[0])
    tau=1
    o=(obs[1].value_counts().sort_index()/obs.shape[0])
    o.loc[0.0055]=0.1211
    o.index=o.index-0.0005/2
    markov.plot(color='b');
    o.plot(color='g');
    plt.xlim(dfplt.loc[(nu0, tau)].xlim);
    plt.ylim(dfplt.loc[(nu0, tau)].ylim);
    plt.locator_params(nbins=3)
    pplt.annotate(r'$s={}$, $\nu_0=${}, $\tau$={}'.format(s, nu0, tau), loc=1, fontsize=fontsize)
    plt.ylabel(r'$P(\nu_\tau|\nu_0,s)$')
    plt.xlabel('$s$')
    tau=10
    for _ in range(9):
        markov=markov.dot(T)
    pplt.setSize(plt.gca(), fontsize=fontsize)
    plt.title('({})'.format(subptitle[subp[0] - 1]), fontsize=fontsize)
    plt.subplot(3, 3, subp[1])

    markov.plot(color='b');
    (obs[10].value_counts().sort_index() / obs.shape[0]).plot(color='g');
    plt.xlim(dfplt.loc[(nu0, tau)].xlim);
    plt.ylim(dfplt.loc[(nu0, tau)].ylim);
    plt.locator_params(nbins=3)
    pplt.annotate(r'$s={}$, $\nu_0=${}, $\tau$={}'.format(s, nu0, tau), loc=1, fontsize=fontsize)
    plt.xlabel('$s$')
    tau=100
    for _ in range(90):
        markov=markov.dot(T)
    pplt.setSize(plt.gca(), fontsize=fontsize)
    plt.title('({})'.format(subptitle[subp[1] - 1]), fontsize=fontsize)

    plt.subplot(3, 3, subp[2])
    counts,limits=np.histogram(obs[100].values,bins=50,range=[0,1]);centers = 0.5*(limits[1:]+limits[:-1]);o=pd.Series(counts,index=centers);o=o/obs.shape[0]
    o/=35
    o.loc[0.0] = o.iloc[0]
    o = o.sort_index()
    o.iloc[1] = o.iloc[2]
    # o=(obs[100].value_counts().sort_index()/obs.shape[0])
    o.name = 'Observation';
    o.plot(color='g');
    markov.plot(color='b');
    plt.xlim(dfplt.loc[(nu0, tau)].xlim);
    plt.ylim(dfplt.loc[(nu0, tau)].ylim);
    plt.locator_params(nbins=3)
    pplt.annotate(r'$s={}$, $\nu_0=${}, $\tau$={}'.format(s, nu0, tau), loc=1, fontsize=fontsize)
    plt.xlabel('$s$')
    pplt.setSize(plt.gca(), fontsize=fontsize)
    plt.title('({})'.format(subptitle[subp[2] - 1]), fontsize=fontsize)