Exemple #1
0
def test_tridiag() -> None:
    sizes = [100, 1000, 2000, 5000, 6000]
    for size in sizes:
        start = time.time()
        _generate_GOE_tridiagonal(size)
        duration = time.time() - start
        print(f"Time for tridiagonal (N = {size}): {duration}")

        start = time.time()
        generate_eigs(size)
        duration = time.time() - start
        print(f"Time for normal (N = {size}): {duration}")
Exemple #2
0
def test_brody_fit() -> None:
    for N in [100, 250, 500, 1000]:
        unfolded = Eigenvalues(generate_eigs(N)).unfold(degree=7)
        # test fitting via max spacing
        res = unfolded.fit_brody(method="spacing")
        spacings = res["spacings"]
        if -np.inf in spacings or np.inf in spacings:
            raise ValueError("Return spacings contains infinities.")
        ecdf = res["ecdf"]
        if np.sum(ecdf < 0) > 0 or np.sum(ecdf > 1):
            raise ValueError("Invalid values in empirical cdf.")
        brody_cdf = res["brody_cdf"]
        if np.sum(brody_cdf < 0) > 0 or np.sum(brody_cdf > 1):
            raise ValueError("Invalid values in brody cdf.")

        # test fitting via mle
        res = unfolded.fit_brody(method="mle")
        spacings = res["spacings"]
        if -np.inf in spacings or np.inf in spacings:
            raise ValueError("Return spacings contains infinities.")
        ecdf = res["ecdf"]
        if np.sum(ecdf < 0) > 0 or np.sum(ecdf > 1):
            raise ValueError("Invalid values in empirical cdf.")
        brody_cdf = res["brody_cdf"]
        if np.sum(brody_cdf < 0) > 0 or np.sum(brody_cdf > 1):
            raise ValueError("Invalid values in brody cdf.")
def test_trim_manual() -> None:
    vals = generate_eigs(2000)
    for i in range(20):
        m, n = np.sort(np.array(np.random.uniform(0, len(vals), 2), dtype=int))
        raw_trimmed = np.copy(vals[m:n])
        eigenvalues = Eigenvalues(vals)
        trimmed = eigenvalues.trim_manually(m, n)
        assert np.allclose(raw_trimmed, trimmed.vals)
Exemple #4
0
def test_unfold_compare() -> None:
    metrics: List[Metric] = ["msqd", "mad", "corr"]
    print("\n")
    print("=" * 80)
    print("Comparing a GOE matrix to GOE")
    print("=" * 80)
    eigs = Eigenvalues(generate_eigs(2000, seed=2))
    unfolded = eigs.unfold(degree=13)
    df = unfolded.ensemble_compare(ensemble=GOE,
                                   metrics=metrics,
                                   show_progress=True)
    print(df)

    print("\n")
    print("=" * 80)
    print("Comparing a Poisson / GDE matrix to GOE")
    print("=" * 80)
    eigs = Eigenvalues(generate_eigs(2000, kind="poisson", seed=2))
    unfolded = eigs.unfold(degree=13)
    df = unfolded.ensemble_compare(ensemble=GOE,
                                   metrics=metrics,
                                   show_progress=True)
    print(df)

    print("\n")
    print("=" * 80)
    print("Comparing a Poisson / GDE matrix to GOE")
    print("=" * 80)
    eigs = Eigenvalues(generate_eigs(2000, kind="poisson", seed=2))
    unfolded = eigs.unfold(degree=13)
    df = unfolded.ensemble_compare(ensemble=GOE,
                                   metrics=metrics,
                                   show_progress=True)
    print(df)

    print("\n")
    print("=" * 80)
    print("Comparing a Poisson / GDE matrix to Poisson / GDE")
    print("=" * 80)
    eigs = Eigenvalues(generate_eigs(2000, kind="poisson", seed=2))
    unfolded = eigs.unfold(degree=13)
    df = unfolded.ensemble_compare(ensemble=GDE,
                                   metrics=metrics,
                                   show_progress=True)
    print(df)
def test_trim_reports() -> None:
    eigs = Eigenvalues(generate_eigs(2000, seed=2))
    report = eigs.trim_report()
    best_smoothers, best_unfolds, best_indices, consistent_smoothers = (
        report.best_overall())
    assert np.array_equal(np.sort(consistent_smoothers),
                          np.sort(["poly_7", "poly_8", "poly_9"]))
    assert np.array_equal(best_indices, [(104, 1765), (231, 1765),
                                         (104, 2000)])

    report.plot_trim_steps(mode="test")
Exemple #6
0
def test_poisson() -> None:
    for i in range(1):
        vals = generate_eigs(5000, kind="poisson")
        unfolded = Eigenvalues(vals).unfold()
        unfolded.plot_nnsd(
            title="Poisson Spacing Test",
            bins=10,
            kde=True,
            mode="test",
            ensembles=["poisson"],
        )
Exemple #7
0
def test_unfold_methods() -> None:
    eigs = Eigenvalues(generate_eigs(500, seed=2))
    trimmed = eigs.get_best_trimmed()
    print("Trim starts and ends:")
    print(trimmed.vals[0])
    print(trimmed.vals[-1])
    assert np.allclose(trimmed.vals[0], -35.84918623729985)
    assert np.allclose(trimmed.vals[-1], 34.709818777689364)

    unfolded = eigs.trim_unfold_auto()
    print("Trim starts and ends:")
    print(unfolded.vals[0])
    print(unfolded.vals[-1])
    assert np.allclose(unfolded.vals[0], -2.473290621491799)
    assert np.allclose(unfolded.vals[-1], 504.2764217889801)
def test_init_sanity() -> None:
    eigs = Eigenvalues(generate_eigs(1000))
    report = eigs.trim_report(
        max_iters=9,
        poly_degrees=[5, 7, 9],
        spline_degrees=[],
        spline_smooths=[],
        show_progress=True,
    )
    assert np.allclose(report._untrimmed, eigs.original_eigenvalues)
    assert isinstance(report.summary, pd.DataFrame)
    assert isinstance(report._trim_iters, list)
    assert isinstance(report._trim_iters[0], TrimIter)
    path = Path(".") / "trim_report.csv"
    report.to_csv(path)
    assert path.exists()
    path.unlink()
    report.plot_trim_steps(mode="test")
def test_plot_rigidity() -> None:
    # good fit for max_L=50 when using generate_eigs(10000)
    # good fit for max_L=55 when using generate_eigs(20000)
    # not likely to be good fit for max_L beyond 20 for generate_eigs(1000)
    # L good | len(eigs) |     percent
    # -----------------------------------
    # 30-40  |    2000   |
    # 30-50  |    8000   |  0.375 - 0.625
    # 50-70  |   10000   |    0.5 - 0.7
    #   50   |   20000   |      0.25
    eigs = Eigenvalues(generate_eigs(2000, log=True))
    unfolded = eigs.unfold(smoother="poly", degree=19)

    unfolded.plot_nnsd(mode="test")
    # unfolded.plot_next_nnsd(mode="test")
    unfolded.plot_level_variance(
        L=np.arange(0.5, 100, 0.2), mode="test", ensembles=["goe", "poisson"]
    )
    unfolded.plot_spectral_rigidity(
        L=np.arange(1, 200, 0.5), c_iters=10000, mode="test"
    )
Exemple #10
0
def test_brody_plot() -> None:
    # test GOE eigs
    bw = 0.2
    # bw = "scott"
    mode: PlotMode = "test"
    ensembles = ["goe", "poisson"]
    for N in [100, 250, 500, 1000]:
        _configure_sbn_style()
        fig, axes = plt.subplots(2, 2)
        for i in range(4):
            eigs = generate_eigs(N)
            Eigenvalues(eigs).unfold(degree=7).plot_nnsd(
                brody=True,
                kde_bw=bw,
                title=f"GOE N={N}",
                ensembles=ensembles,
                mode="return",
                fig=fig,
                axes=axes.flat[i],
            )
        _handle_plot_mode(mode, fig, axes)

    # test time series
    _configure_sbn_style()
    fig, axes = plt.subplots(2, 2)
    for i in range(4):
        eigs = np.linalg.eigvalsh(
            np.corrcoef(np.random.standard_normal([1000, 250])))
        Eigenvalues(eigs).unfold(degree=7).plot_nnsd(
            brody=True,
            brody_fit="mle",
            mode=mode,
            title=f"t-series (untrimmed)(MLE)",
            ensembles=ensembles,
            kde_bw=bw,
            fig=fig,
            axes=axes.flat[i],
        )
    _handle_plot_mode(mode, fig, axes)

    _configure_sbn_style()
    fig, axes = plt.subplots(2, 2)
    for i in range(4):
        eigs = np.linalg.eigvalsh(
            np.corrcoef(np.random.standard_normal([1000, 250])))
        Eigenvalues(eigs).unfold(degree=7).plot_nnsd(
            brody=True,
            brody_fit="spacings",
            mode=mode,
            title=f"t-series (untrimmed)(spacings)",
            ensembles=ensembles,
            kde_bw=bw,
            fig=fig,
            axes=axes.flat[i],
        )
    _handle_plot_mode(mode, fig, axes)

    _configure_sbn_style()
    fig, axes = plt.subplots(2, 2)
    for i in range(4):
        eigs = np.linalg.eigvalsh(
            np.corrcoef(np.random.standard_normal([1000, 250])))
        eigs = eigs[eigs > 100 * np.abs(eigs.min())]
        Eigenvalues(eigs).unfold(degree=7).plot_nnsd(
            brody=True,
            brody_fit="spacings",
            mode=mode,
            title="t-series (trimmed)(spacings)",
            ensembles=ensembles,
            kde_bw=bw,
            fig=fig,
            axes=axes.flat[i],
        )
    _handle_plot_mode(mode, fig, axes)

    _configure_sbn_style()
    fig, axes = plt.subplots(2, 2)
    for i in range(4):
        eigs = np.linalg.eigvalsh(
            np.corrcoef(np.random.standard_normal([1000, 250])))
        eigs = eigs[eigs > 100 * np.abs(eigs.min())]
        Eigenvalues(eigs).unfold(degree=7).plot_nnsd(
            brody=True,
            brody_fit="mle",
            mode=mode,
            title="t-series (trimmed)(MLE)",
            ensembles=ensembles,
            kde_bw=bw,
            fig=fig,
            axes=axes.flat[i],
        )
    _handle_plot_mode(mode, fig, axes)

    if mode != "test":
        plt.show()