Skip to content

Commit

Permalink
Add functionality to use empirical quantiles on demand.
Browse files Browse the repository at this point in the history
  • Loading branch information
quantgirluk committed Dec 11, 2024
1 parent b405da5 commit 53a02bc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
12 changes: 10 additions & 2 deletions aleatory/processes/base_analytical.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,15 @@ def plot(self, n, N, title=None, **fig_kw):
return figure

def _draw_paths(
self, n, N, marginal=False, envelope=False, type=None, title=None, **fig_kw
self,
n,
N,
marginal=False,
envelope=False,
type=None,
title=None,
empirical_envelope=False,
**fig_kw,
):
self.simulate(n, N)
expectations = self._process_expectation()
Expand All @@ -150,7 +158,7 @@ def _draw_paths(
upper = expectations + 3.0 * stds
lower = expectations - 3.0 * stds
else:
if marginal_available:
if marginal_available and empirical_envelope == False:
marginals = [self.get_marginal(t) for t in self.times[1:]]
upper = [self.initial] + [m.ppf(0.005) for m in marginals]
lower = [self.initial] + [m.ppf(0.995) for m in marginals]
Expand Down
37 changes: 29 additions & 8 deletions aleatory/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ def draw_paths_horizontal(
colormap="RdYlBu_r",
colorspos=None,
mode="linear",
estimate_quantiles=False,
**fig_kw,
):
cm = plt.colormaps[colormap]
Expand Down Expand Up @@ -220,9 +221,19 @@ def draw_paths_horizontal(
label=r"$\overline{X_T}$",
)
ax2.legend()

elif marginal and marginalT:
marginaldist = marginalT
x = np.linspace(marginaldist.ppf(0.001), marginaldist.ppf(0.999), 100)
# lower_q = marginaldist.ppf(0.001)
# upper_q = marginaldist.ppf(0.999)

if estimate_quantiles:
lower_val = np.min(last_points)
upper_val = np.max(last_points)
else:
lower_val = marginaldist.ppf(0.001)
upper_val = marginaldist.ppf(0.999)
x = np.linspace(lower_val, upper_val, 100)
ax2.plot(
marginaldist.pdf(x), x, "-", lw=1.75, alpha=0.6, label="$X_T$ pdf"
)
Expand All @@ -235,17 +246,19 @@ def draw_paths_horizontal(
ax2.set_title("$X_T$")

for i in range(N):
if mode == "points":
if mode == "linear":
ax1.plot(times, paths[i], "-", lw=1.0, color=cm(colors[i]))
elif mode == "points":
ax1.scatter(times, paths[i], s=7, color=cm(colors[i]))
elif mode == "steps":
ax1.step(times, paths[i], color=cm(colors[i]), where="post")
elif mode in ["steps+points", "points+steps"]:
ax1.step(times, paths[i], color=cm(colors[i]), where="post")
ax1.scatter(times, paths[i], s=7, color=cm(colors[i]))
elif mode == "linear":
ax1.plot(times, paths[i], "-", lw=1.0, color=cm(colors[i]))
else:
raise ValueError("mode must be 'points', 'steps', or 'linear'.")
raise ValueError(
"mode must be 'linear', 'points', 'steps', 'steps+points'."
)

if expectations is not None:
ax1.plot(times, expectations, "--", lw=1.75, label="$E[X_t]$")
Expand All @@ -272,13 +285,17 @@ def draw_paths_horizontal(

if mode == "linear":
ax1.plot(times, path, "-", color=cm(color), lw=0.75)
elif mode == "steps":
ax1.step(times, path, color=cm(color), where="post")
elif mode in ["points"]:
ax1.scatter(times, path, s=7, color=cm(color))
elif mode == "steps":
ax1.step(times, path, color=cm(color), where="post")
elif mode in ["steps+points", "points+steps"]:
ax1.step(times, path, color=cm(color), where="post")
ax1.scatter(times, path, s=7, color=cm(color))
else:
raise ValueError(
"mode must be 'linear', 'points', 'steps', 'steps+points'."
)
if expectations is not None:
ax1.plot(times, expectations, "--", lw=1.75, label="$E[X_t]$")
ax1.legend()
Expand Down Expand Up @@ -354,7 +371,11 @@ def draw_paths_vertical(
)
elif marginal and marginalT:
marginaldist = marginalT
x = np.linspace(marginaldist.ppf(0.001), marginaldist.ppf(0.999), 100)

lower_val = np.min(last_points)
upper_val = np.max(last_points)
x = np.linspace(lower_val, upper_val, 100)
# x = np.linspace(marginaldist.ppf(0.001), marginaldist.ppf(0.999), 100)
ax2.plot(
x, marginaldist.pdf(x), "-", lw=1.75, alpha=0.6, label="$X_T$ pdf"
)
Expand Down

0 comments on commit 53a02bc

Please sign in to comment.