Skip to content

Commit

Permalink
[DOC] Prettify spectral density plots
Browse files Browse the repository at this point in the history
  • Loading branch information
f-dangel committed Oct 20, 2024
1 parent ae36873 commit b823469
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 36 deletions.
98 changes: 63 additions & 35 deletions docs/examples/basic_usage/example_verification_spectral_density.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from numpy.linalg import eigh
from numpy.random import pareto, randn, seed
from scipy.sparse.linalg import aslinearoperator, eigsh
from tueplots import bundles

from curvlinops.outer import OuterProductLinearOperator
from curvlinops.papyan2020traces.spectrum import (
Expand Down Expand Up @@ -85,7 +86,7 @@ def create_matrix(dim: int = 2000) -> ndarray:
# and using the same hyperparameters as specified by the paper:

# spectral density hyperparameters
num_points = 1024
num_points = 200
ncv = 128
num_repeats = 10
kappa = 3
Expand Down Expand Up @@ -118,20 +119,33 @@ def create_matrix(dim: int = 2000) -> ndarray:
# and plot it with a histogram (same number of bins as in the paper) of the
# exact density:

plt.figure()
plt.xlabel("Eigenvalue")
plt.ylabel("Spectral density")

left, right = grid[0], grid[-1]
num_bins = 100
bins = linspace(left, right, num_bins, endpoint=True)
plt.hist(Y_evals, bins=bins, log=True, density=True, label="Exact")
# use `tueplots` to make the plot look pretty
plot_config = bundles.icml2024(column="half")

with plt.rc_context(plot_config):
plt.figure()
plt.xlabel(r"Eigenvalue $\lambda$")
plt.ylabel(r"Spectral density $\rho(\lambda)$")

left, right = grid[0], grid[-1]
num_bins = 40
bins = linspace(left, right, num_bins, endpoint=True)
plt.hist(
Y_evals,
bins=bins,
log=True,
density=True,
label="Exact",
edgecolor="white",
lw=0.5,
)

plt.plot(grid, density, label="Approximate")
plt.legend()
plt.plot(grid, density, label="Approximate")
plt.legend()

# same ylimits as in the paper
plt.ylim(bottom=1e-5, top=1e1)
# same ylimits as in the paper
plt.ylim(bottom=1e-5, top=1e1)
plt.savefig("toy_spectrum.pdf", bbox_inches="tight")

# %%
#
Expand Down Expand Up @@ -172,7 +186,7 @@ def create_matrix(dim: int = 2000) -> ndarray:

# %%
#
# Wit rank deflation
# With rank deflation
# ^^^^^^^^^^^^^^^^^^
#
# As you can see in the above plot, the spectrum consists of a bulk and three
Expand Down Expand Up @@ -288,7 +302,7 @@ def create_matrix_log_spectrum(dim: int = 500) -> ndarray:
# and using the same hyperparameters as specified by the paper:

# spectral density hyperparameters
num_points = 1024
num_points = 200
margin = 0.05
ncv = 256
num_repeats = 10
Expand Down Expand Up @@ -319,29 +333,43 @@ def create_matrix_log_spectrum(dim: int = 500) -> ndarray:
#
# Now we can visualize the results:

plt.figure()
plt.xlabel("Eigenvalue")
plt.ylabel("Spectral density")

Y_log_abs_evals = log(abs(Y_evals) + epsilon)

xlimits_no_margin = (Y_log_abs_evals.min(), Y_log_abs_evals.max())
width_no_margins = xlimits_no_margin[1] - xlimits_no_margin[0]
xlimits = [
xlimits_no_margin[0] - margin * width_no_margins,
xlimits_no_margin[1] + margin * width_no_margins,
]
# use `tueplots` to make the plot look pretty
plot_config = bundles.icml2024(column="half")

with plt.rc_context(plot_config):
plt.figure()
plt.xlabel(r"Absolute eigenvalue $\nu = |\lambda| + \epsilon$")
plt.ylabel(r"Spectral density $\rho(\log\nu)$")

Y_log_abs_evals = log(abs(Y_evals) + epsilon)

xlimits_no_margin = (Y_log_abs_evals.min(), Y_log_abs_evals.max())
width_no_margins = xlimits_no_margin[1] - xlimits_no_margin[0]
xlimits = [
xlimits_no_margin[0] - margin * width_no_margins,
xlimits_no_margin[1] + margin * width_no_margins,
]

plt.semilogx()
num_bins = 40
bins = logspace(*xlimits, num=num_bins, endpoint=True, base=e)
plt.hist(
exp(Y_log_abs_evals),
bins=bins,
log=True,
density=True,
label="Exact",
edgecolor="white",
lw=0.5,
)

plt.semilogx()
num_bins = 100
bins = logspace(*xlimits, num=num_bins, endpoint=True, base=e)
plt.hist(exp(Y_log_abs_evals), bins=bins, log=True, density=True, label="Exact")
plt.plot(grid, density, label="Approximate")

plt.plot(grid, density, label="Approximate")
# use same ylimits as in the paper
plt.ylim(bottom=1e-14, top=1e-2)
plt.legend()

# use same ylimits as in the paper
plt.ylim(bottom=1e-14, top=1e-2)
plt.legend()
plt.savefig("toy_log_spectrum.pdf", bbox_inches="tight")

# %%
#
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/basic_usage/example_visual_tour.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def logabs(mat, epsilon=1e-6):

with plt.rc_context(plot_config):
plot(logabs, transform_title="Logarithmic absolute entries")
plt.savefig("curvature_matrices_log_abs.pdf", bbox_inches="tight")
plt.savefig("curvature_matrices_log_abs.pdf", bbox_inches="tight")

# %%
#
Expand Down

0 comments on commit b823469

Please sign in to comment.