コード例 #1
0
    def test_raw_runner_ito_1d(self):
        T = 0.5
        sde = Ito1D(particles=30000,
                    x0=0.0,
                    drift=drift,
                    diffusion=diffusion,
                    sigma=1,
                    t0=0.0,
                    dt=1e-2,
                    T=T)

        job = Job(sde=sde,
                  pdf=Simple1DHistogram(bins=100),
                  mode=Job.RAW,
                  settings={})

        result, execution_time = run(job, processes=3)
        pdf = result.distributions[-1]

        print(f"Parallel Execution Time: {execution_time}")

        x = np.linspace(pdf.lower_bound, pdf.upper_bound, 1000)
        y = pdf(x)

        plt.figure(figsize=(20, 10))
        plt.plot(x, y, label="Approximation")
        sb.distplot(np.random.normal(0, np.sqrt(T), size=10000), label="True")
        plt.show()
コード例 #2
0
    def test_virus_model(self):
        global has_been_sick

        has_been_sick = None

        T = 200
        dt = 1e-1
        sde = Exp1D(particles=50000,
                    x0=1.0,
                    drift=covid_drift,
                    diffusion=covid_diffusion,
                    lam=1.0,
                    t0=0.0,
                    dt=dt,
                    T=T)

        job = Job(sde=sde,
                  pdf=Simple1DHistogram(bins=50),
                  mode=Job.Video,
                  settings={"steps_per_frame": 5})

        result, execution_time = run(job, processes=4)

        tdplot = make_time_1dx_distplot_video(result.distributions,
                                              fps=10,
                                              dt=dt,
                                              steps_per_frame=5,
                                              save=True)
        tdplot = three_dee_plot(result.distributions, dt=dt, steps_per_frame=5)
        plt.show()
コード例 #3
0
    def test_cool_accuracy_plot(self):
        T = 1
        dt = 1e-2
        processes = 2

        dim = (4, 4)
        _N = np.logspace(3, 6, 4)
        _bins = np.array([5, 50, 200, 500])  # , 200, 500])

        X, Y = np.meshgrid(_N, _bins)

        Z = np.concatenate([X.reshape(-1, 1), Y.reshape(-1, 1)], axis=1)

        results = []
        for n, b in Z:
            N = int(n / processes)
            bins = int(b)

            sde = Ito1D(particles=N,
                        x0=0.0,
                        drift=drift,
                        diffusion=diffusion,
                        sigma=1,
                        t0=0.0,
                        dt=dt,
                        T=T)

            job = Job(sde=sde,
                      pdf=Simple1DHistogram(bins=bins),
                      mode=Job.RAW,
                      settings={})

            result, execution_time = run(job, processes=processes)

            pdf = result.distributions[-1]

            x = np.linspace(pdf.lower_bound, pdf.upper_bound, 10000)

            normal = Normal(mu=0, sigma=np.sqrt(T))
            kl_div = relative_entropy_1d(domain=x, p=normal, q=pdf)

            print(
                f"Parallel Execution Time: {execution_time} -- {n} -- {bins} --  {kl_div}"
            )

            results.append(kl_div)

        results = np.array(results).reshape(*dim)

        plt.figure(figsize=(10, 6))
        plt.title(f"KL-divergence heatmap", fontsize=20)
        print("results", results)
        ax = sb.heatmap(results, annot=True, yticklabels=_bins, xticklabels=_N)
        plt.ylabel("Bins", fontsize=15)
        plt.xlabel("N", fontsize=15)
        plt.tight_layout()
        plt.savefig("../images/kl-heatmap.png", bbox_inches="tight")
        plt.show()
コード例 #4
0
    def test_cool_sparseness_plot(self):
        dt = 1e-1
        processes = 2

        _N = 100000
        bins = 50

        _T = [1, 10, 100, 1000]  # 1000, 10000]

        results = []
        for T in _T:
            N = int(_N / processes)

            sde = Ito1D(particles=N,
                        x0=0.0,
                        drift=drift,
                        diffusion=diffusion,
                        sigma=1,
                        t0=0.0,
                        dt=dt,
                        T=T)

            job = Job(sde=sde,
                      pdf=Simple1DHistogram(bins=bins),
                      mode=Job.RAW,
                      settings={})

            result, execution_time = run(job, processes=processes)

            pdf = result.distributions[-1]

            x = np.linspace(-np.sqrt(T) * 3, np.sqrt(T) * 3, 100000)

            normal = Normal(mu=0, sigma=np.sqrt(T))
            kl_div = relative_entropy_1d(domain=x, p=normal, q=pdf)

            print(
                f"Parallel Execution Time: {execution_time} -- {_N} -- {bins} --  {kl_div}"
            )

            results.append(kl_div)

        results = np.array(results)

        plt.figure(figsize=(10, 6))
        plt.title(f"KL-divergence sparseness effect", fontsize=20)
        print("results", results)
        ax = sb.lineplot(_T, results)
        plt.ylabel("KL-divergence", fontsize=15)
        plt.xlabel("T", fontsize=15)
        plt.tight_layout()
        # plt.savefig("../images/kl-heatmap.png", bbox_inches="tight")
        plt.show()
コード例 #5
0
    def test_performance_1(self):
        T = 1
        dt = 1e-1
        processes = 4
        N = int(1e6)  # int(1e8 / 6)
        bins = 50
        sde = Ito1D(particles=N,
                    x0=0.0,
                    drift=drift,
                    diffusion=diffusion,
                    sigma=1,
                    t0=0.0,
                    dt=dt,
                    T=T)

        job = Job(sde=sde,
                  pdf=Simple1DHistogram(bins=bins),
                  mode=Job.RAW,
                  settings={})

        result, execution_time = run(job, processes=processes)

        pdf = result.distributions[-1]

        print(f"Parallel Execution Time: {execution_time}")

        x = np.linspace(pdf.lower_bound, pdf.upper_bound, 10000)
        y = pdf(x)

        normal = Normal(mu=0, sigma=np.sqrt(T))

        print(
            f"Expected -- approximation: {expected_1d(domain=x, pdf=pdf)} | true: {expected_1d(domain=x, pdf=normal)}"
        )
        print(
            f"Variance -- approximation: {variance_1d(domain=x, pdf=pdf)} | true: {variance_1d(domain=x, pdf=normal)}"
        )
        print(
            f"Relative Entropy: {relative_entropy_1d(domain=x, p=normal, q=pdf)}"
        )

        plt.figure(figsize=(10, 6))
        plt.title(f"T={T} dt={dt} N={N * processes} B={bins}", fontsize=20)
        plt.plot(x, y, label="Approximation")
        p = normal(x)
        sb.lineplot(x, p, label="True")
        plt.legend(fontsize=14)
        plt.xticks(fontsize=12)
        plt.yticks(fontsize=12)
        plt.tight_layout()
        plt.savefig("../images/many-bins-many-particles.png")
        plt.show()
コード例 #6
0
    def test_video_runner_exp1d(self):
        sde = Exp1D(particles=10000,
                    x0=0.0,
                    drift=drift,
                    diffusion=diffusion,
                    lam=1,
                    t0=0.0,
                    dt=1e-2,
                    T=2.0)

        job = Job(sde=sde,
                  pdf=Simple1DHistogram(bins=100),
                  mode=Job.Video,
                  settings={"steps_per_frame": 5})

        result, _ = run(job, 2)
        result = result.distributions

        make_time_1dx_distplot_video(result,
                                     fps=10,
                                     dt=1e-2,
                                     steps_per_frame=5)