예제 #1
0
def test_convergence():
    prm = deepcopy(dft_prm)
    prm['n']['shape'] = (300, )
    prm['n']['death'] = 10**-14
    prm['growth']['std'] = 0  #10**-14
    kwargs = {}
    m = Model(parameters=prm, dynamics=dft_dyn, **kwargs)

    # code_debugger()
    tmax = 5000.
    m.evol(print_msg=1, tmax=tmax, tsample=tmax / 30., converge='force')
    trial = 0
    while np.sum(m.results['n'][-1] > 10**-10) < 2:
        print trial
        m.evol(print_msg=0, tmax=tmax, tsample=tmax / 30., converge='force')
        trial += 1
    plot(m.results['n'].index, m.results['n'].matrix, log='xy', hold=1)
    code_debugger()

    m.evol(print_msg=1, tmax=tmax, reseed=0, tsample=tmax / 30., converge=0)

    plot(m.results['n'].index,
         m.results['n'].matrix,
         log='xy',
         hold=1,
         marker='o',
         linestyle='None')
    plt.show()
    code_debugger()
예제 #2
0
def measure_continuum_cavity(model, measure, **kwargs):
    #from datatools import plot,scatter
    #scatter(model.data['niches'].matrix, model.results['n'].matrix[-1],hold=1)
    res = continuum_cavity_output(measure, **kwargs)
    measure.update(res)

    scatter(measure['continuum_ranks'], model.results['n'][-1], hold=1)
    scatter(measure['continuum_ranks'], (model.results['n'][-1] > 10**-10),
            hold=1,
            c='g')
    plt.show()
예제 #3
0
def test_noise():
    #NOISE EFFECT IS THOROUGHLY INDEPENDENT OF tsample EVER SINCE I SWITCHED TO dop853
    prm = deepcopy(dft_prm)
    prm['nnoise'] = {
        'type': 'noise',
        'variables': ['n'],
        'amplitude': 1,
        'sign': 1,
        'role': 'noise',
        'dynamics': 'noise',
        'rank': 'full',
        #'direction':'random',
    }
    prm['n']['shape'] = (3, )
    dyn = deepcopy(dft_dyn)
    dyn['noise'] = {
        'type': 'noise',
        'variables': [
            ('n', 'com'),
        ],
    }
    kwargs = {}
    m = Model(parameters=prm, dynamics=dyn, **kwargs)
    tmax = 20
    nstep = 10
    m.evol(print_msg=1, tmax=tmax, tsample=float(tmax) / nstep / 10.)
    from datatools import plt
    traj = m.results['n']
    noise = m.data['nnoise']
    plt.subplot(121)
    plot(traj.index, traj.matrix, hold=1, log='y')
    plt.subplot(122)
    plot(noise.index, noise.matrix[:, :2], hold=1, log='y')
    m.evol(print_msg=1, tmax=tmax, tsample=float(tmax) / nstep, reseed=0)
    traj = m.results['n']
    noise = m.data['nnoise']
    plt.subplot(121)
    plot(traj.index, traj.matrix, hold=1, linestyle='None', marker='o')
    plt.subplot(122)
    plot(noise.index,
         noise.matrix[:, :2],
         hold=1,
         log='y',
         linestyle='None',
         marker='o')

    plt.show()
예제 #4
0
def test_var():
    from scipy.linalg import solve_continuous_lyapunov as solve_lyapunov, norm, inv, eig
    prm = deepcopy(dft_prm)

    S = 100
    gamma = -1.
    sigmas = np.linspace(0.01, .5, 50)
    for sigma in sigmas:
        DOM = 10000
        while DOM > 0:
            #m=Model(prm)
            if gamma == 0:
                asymm = 0
            else:
                asymm = (1 - np.sqrt(1 - gamma**2)) / gamma

            aij = np.random.normal(0, 1., (S, S)) / np.sqrt(S)
            aij = (aij + asymm * aij.T) / np.sqrt(1 + asymm**2)
            np.fill_diagonal(aij, 0)
            print np.std(aij) * np.sqrt(S), np.mean(aij * aij.T) * S

            J = -sigma * aij
            np.fill_diagonal(J, -1)

            DOM = np.max(np.real(eig(J)[0]))

        N = np.abs(np.random.normal(1, 0.2, S))
        CIJ = solve_lyapunov(J, -np.diag(N))
        VarC = np.trace(CIJ) / S * 2
        AA = sigma**2 * ((aij**2).T * (N / np.add.outer(N, N))).T
        AG = sigma**2 * ((aij * aij.T) * (N / np.add.outer(N, N)))

        DEN = 1 - np.sum(AG, axis=1)
        RESULT = np.dot(inv(np.diag(DEN) - AA), .5 * np.ones(S))  #/Slive

        VarP = 1 / (1 - (1 + gamma) / 2 * sigma**2)
        #print VarP, sigma,RESULT
        #plot(np.logspace(-1,3),np.logspace(-1,3),hold=1)
        #scatter( np.sum(DEN*np.diag(CIJ)) ,np.sum(.5 + np.dot(AA,np.diag(CIJ) ) )  ,hold=1,log='xy')
        scatter(VarP, VarC, hold=1)
        scatter(VarP, np.mean(RESULT) * 2, hold=1, color='r', alpha=.6)
    plt.show()
예제 #5
0
def test_cavity_funcresp():
    from datatools import *
    from cavity_conv import *
    dic = {
        'S': 100.,
        'mu': 10.,
        'sigma': .3,
        'sigma_k': 1.,
        'gamma': .05,
        'Nc': 50.,
        'avgK': 1.
    }
    #S=dic['S']
    locals().update(dic)
    Nc = dic['Nc']

    print cavity_solve_props(**dic)
    N1, N2, v, phi, f = funcresp_cavity_solve(**dic)
    print N1, N2, v, phi, f  #v*phi/2.,
    avgN = N1 * phi
    q = N2 * phi / avgN**2
    h = (dic.get('avgK', 1) / avgN - mu) / sigma
    vv = v * sigma * phi
    print q, vv, h, phi
    print N1 * phi, np.sqrt(N2 * phi -
                            (N1 *
                             phi)**2), N1 * phi * S, N2 / (S * phi * N1**2)
    u = (1 - mu * 1. / S)

    res = []
    fs = np.linspace(0, 1, 20)
    for f in fs:
        utilde = u - gamma * v * (1 - f) * phi * sigma**2
        effvar = (1 - f)**2 * phi * sigma**2 * N2 + sigma_k**2
        mean = (avgK - mu * (phi * N1 * (1 - f) + f * Nc / S)) / utilde
        var = effvar / utilde**2
        res.append((mean, var))
    res = zip(*res)
    #plot(res[0],res[1],xs=fs)
    #return

    res = []
    comp = []
    Ncs = np.logspace(0, 2, 30)
    for Nc in Ncs:
        dic['Nc'] = Nc
        rs = []
        for trials in range(3):
            r = funcresp_cavity_solve(**dic)
            rs.append(r)
        rs = np.array(rs)
        mean = (avgK - mu * Nc / S) / u
        if (rs[:, -1] > 0.8).any() and (rs[:, -1] < 0.2).any():
            print rs.T[-2:]
        res.append(np.mean(rs, axis=0))

        N1, N2, v, phi, f = res[-1]
        dic2 = {}
        dic2.update(dic)
        dic2['sigma'] *= max(0.001, (1 - f))
        dic2['gamma'] /= np.clip((1 - f), np.abs(dic2['gamma']), 1.)
        dic2['mu'] *= (1 - f) + f * Nc / S / N1 / phi
        c = cavity_solve_props(**dic2)
        comp.append(
            (c['avgN'], c['q'], c['v'] / dic['sigma'] / c['phi'], c['phi']))

    res = np.array(zip(*res))
    comp = np.array(zip(*comp))

    plot(res[0] * res[3], comp[0], xs=Ncs, hold=1)
    plt.figure()
    plot(res[1] / res[0]**2 / res[3], comp[1], xs=Ncs, hold=1)
    plt.figure()
    plot(res[2], comp[2], xs=Ncs, hold=1)
    plt.figure()
    plot(res[3], comp[3], xs=Ncs, hold=1)
    plt.show()

    titles = ['N1', 'N2', 'v', 'phi', 'f']
    for r in res:
        plt.figure()
        plt.title(titles.pop(0))
        plot(Ncs, r, hold=1)
    plt.show()
예제 #6
0
def test_noise_amplitude(**kwargs):
    #CONCLUSIONS: V ~ 1/2r works but requires very long tmax if r is not too large
    prm = {
        'n': {
            'type': 'variable',
            'axes': ('com', ),
            'death': -10000,
            'shape': (1, ),
            'mean': 0.,
            'std': 0,
        },
        'diag': {
            'type': 'matrix',
            'variables': ['n'],
            'role': 'diagonal',
            'mean': -2,
            'std': 0,
            'dynamics': 'lin',
        },
        # 'community': {
        #     'type': 'matrix',
        #     'variables': [('n', 'com'), ('n', 'com')],
        # 'role': 'interactions',
        #     'mean': 5.,
        #     'std': .5,
        #     'symmetry': .5,
        #     'dynamics': 'lin',
        # },
        'nnoise': {
            'type': 'noise',
            'variables': ['n'],
            'amplitude': 1,
            'role': 'noise',
            'dynamics': 'noise',
            'rank': 1,
            'direction': 0,
        }
    }
    dyn = {
        'lin': {
            'type': 'linear',
            'variables': [
                ('n', 'com'),
            ],
        },
        'noise': {
            'type': 'noise',
            'variables': [
                ('n', 'com'),
            ],
        }
    }
    m = Model(parameters=prm, dynamics=dyn, **kwargs)

    ts = np.linspace(0.02, 3, 50)
    dt = 0.1
    tmax = 30.
    from datatools import plot
    import scipy.integrate as scint
    for mode in (1, ):
        #MODE = 0 : Hand integration
        #MODE = 1 : model.evol with pregenerated noise
        res = []
        for t in ts:
            print t
            m.data['diag'][:] = -1. / t
            # print tmax
            nstep = tmax / dt
            if mode:
                m.evol(print_msg=1,
                       tmax=tmax,
                       tsample=dt,
                       death=-10000,
                       dftol=-10000)
                traj = m.results['n'].matrix
            else:
                m.data['nnoise'].generate(0, 2 * tmax, dt)
                noise = np.array([
                    m.data['nnoise'].get(t).matrix
                    for t in np.linspace(0, tmax, nstep)
                ])
                # xs=m.results['n'].index
                # plot(traj,xs=xs,hold=1)
                xs = np.linspace(0, tmax, nstep)[1:]
                traj = [0]
                t0 = 0

                def dxx(t, x):
                    # print m.data['nnoise'].get(t)[0]
                    return m.data['diag'][0] * x + np.random.normal(
                        0, 1) / np.sqrt(dt / 100)

                for t in xs[1:]:
                    x = traj[-1]
                    for tt in np.linspace(t0, t, 100):
                        x += dxx(tt, x) * (t - t0) / 100.
                    traj.append(x)
                    t0 = t
                # plot(traj,xs=xs)
                # print '  RATIO   ', np.var(traj),np.var(noise)
            res.append([np.var(traj) * 2 * np.abs(m.data['diag'][0])
                        ])  #,np.var(noise)])
        res = np.array(res)
        plot(res, xs=ts, log='y', hold=1)
    plt.show()
예제 #7
0
def continuum_interactions(measure, rank, A, K):

    func = lambda x, a, b, c: continuum_quad(x, a, b, c)
    hist, dens = np.histogram(rank, bins=50, normed=1)
    bins = np.array((dens[1:] + dens[:-1])) / 2.
    Stot = len(rank)

    Sprm = np.array(
        [curve_fit(func, bins[hist > 0], np.log(hist[hist > 0] * Stot))[0]])
    measure['Sprm'] = Sprm
    measure['Sopt'] = (('dim', 1), )

    S = continuum_func(Sprm, **dict(measure['Sopt']))

    Kpos, Kneg = (K > 0), (K <= 0)

    avgKprm = np.array([
        curve_fit(func, np.array(rank), np.log(np.abs(ks)))[0] for ks in (
            np.clip(K, 0.00001, None),
            np.clip(K, None, -0.00001),
        )
    ])

    measure['avgKprm'] = avgKprm
    measure['avgKopt'] = (('sgn', (1, -1)), ('dim', 1))

    avgK = continuum_func(avgKprm, **dict(measure['avgKopt']))
    varKprm = np.array([
        curve_fit(func, np.array(rank[Ax]), np.log(
            (K[Ax] - avgK(rank[Ax]))**2))[0] for Ax in (Kpos, Kneg) if sum(Ax)
    ])

    measure['varKprm'] = varKprm
    measure['varKopt'] = (('dim', 1), )

    if 0:
        Rank = sorted(rank)
        print sinteg.quad(S, Rank[0], Rank[-1]), Stot
        from datatools import plot, scatter, plt
        plot(Rank, S(Rank), hold=1)
        scatter(bins, hist * Stot, xlabel='rank', ylabel="S", title='S')
        plot(Rank, avgK(Rank), hold=1)
        scatter(rank, K)

    func = lambda x, a, bx, cx, by, cy, bxy, cxy: continuum_quad(
        x, a, bx, cx, by, cy, bxy, cxy)

    measure['ranks_min'] = np.min(rank)
    measure['ranks_max'] = np.max(rank)

    ranks = np.tile(rank, (len(rank), 1))
    shape = ranks.shape
    xs = ranks.ravel()
    ys = ranks.T.ravel()
    A[np.abs(A) < 10**-5] = 0
    Apos = (A.ravel() > 0)
    Aneg = (A.ravel() < 0)

    muprm = np.array([
        curve_fit(func, np.array((xs[Ax], ys[Ax])),
                  np.log(np.abs(A).ravel()[Ax]))[0] for Ax in (Apos, Aneg)
        if np.sum(Ax) > 7
    ])

    #print mupopt,mumopt
    mu = continuum_func(muprm, sgn=ifelse(np.sum(Aneg) > 7, -1, 1))

    measure['muprm'] = muprm
    measure['muopt'] = (('sgn', -1), )

    if 1:
        from datatools import scatter3d, plt
        Ranks = np.tile(sorted(rank), (len(rank), 1))
        XS = Ranks
        YS = Ranks.T

    if 0:
        scatter3d(xs[::],
                  ys[::],
                  np.log(np.abs(A).ravel()[::]),
                  hold=1,
                  alpha=0.3,
                  c='r')
        plt.gca().plot_wireframe(
            XS, YS,
            np.log(np.abs(mu(XS.ravel(), YS.ravel()))).reshape(shape))
        plt.show()

    sigs = np.clip((A.ravel() - mu(xs, ys))**2, 10**-5, None)
    sigprm = np.array([
        curve_fit(func, np.array((xs[Ax], ys[Ax])), np.log(sigs[Ax]))[0]
        for Ax in ((A.ravel() != 0), )
    ])  #(Apos,Aneg)])

    measure['sigmaprm'] = sigprm
    sigma = continuum_func(sigprm, sgn=1)

    if 0:
        scatter3d(xs[:], ys[:], np.log(sigs[:]), hold=1, alpha=0.3, c='r')
        plt.gca().plot_wireframe(
            XS, YS,
            np.log(sigma(XS.ravel(), YS.ravel())).reshape(shape))
        plt.show()

    gam = (A.ravel() - mu(xs, ys)) * (A.T.ravel() - mu(ys, xs)) / np.sqrt(
        sigma(xs, ys), sigma(ys, xs))
    gammprm = np.array([
        curve_fit(func,
                  np.array((xs[Ax], ys[Ax])),
                  np.log(np.abs(gam[Ax])),
                  method='trf',
                  loss='soft_l1')[0] for Ax in (Apos, Aneg)
    ])

    measure['gammaprm'] = gammprm
    gamma = continuum_func(gammprm, sgn=[-1, -1], mode='exp')

    if 0:
        gam = np.clip(gam, -1, 1)
        #scatter3d(xs[::],ys[::], gam,hold=1,alpha=0.3,c=gam)
        scatter3d(0, 0, 0, hold=1)
        plt.gca().plot_wireframe(XS, YS,
                                 gamma(XS.ravel(), YS.ravel()).reshape(shape))
        plt.show()