Пример #1
0
def daft_hier():

    # create the PGM
    pgm = daft.PGM(shape=[7, 2.5],
                   origin=[0, 0],
                   grid_unit=4,
                   label_params={'fontsize': 18})

    # priors
    pgm.add_node(daft.Node("beta_parent_mu", r"$\mu_{parent}$", 1, 1, scale=2))
    pgm.add_node(
        daft.Node("beta_parent_sd", r"$\sigma_{parent}$", 2, 2.2, scale=2))
    pgm.add_node(daft.Node("beta_mfr_mu", r"$\mu_{mfr}$", 2, 1, scale=2))
    pgm.add_node(daft.Node("beta_mfr_sd", r"$\sigma_{mfr}$", 3, 2.2, scale=2))
    pgm.add_node(daft.Node("beta_mfr", r"$\beta_{mfr}$", 3, 1, scale=2))
    pgm.add_node(daft.Node("beta", r"$\beta$", 4, 2.2, scale=2))

    # latent variable.
    pgm.add_node(daft.Node("mu", r"$\beta X_{n}$", 4, 1, scale=2))

    # noise
    pgm.add_node(daft.Node("epsilon", r"$\epsilon$", 6.2, 1, scale=2))

    # observed data
    pgm.add_node(daft.Node("y", r"$y_n$", 5, 1, scale=2, observed=True))

    # edges
    pgm.add_edge("beta_parent_mu", "beta_mfr_mu")
    pgm.add_edge("beta_parent_sd", "beta_mfr_mu")
    pgm.add_edge("beta_mfr_mu", "beta_mfr")
    pgm.add_edge("beta_mfr_sd", "beta_mfr")
    pgm.add_edge("beta_mfr", "mu")
    pgm.add_edge("beta", "mu")
    pgm.add_edge("mu", "y")
    pgm.add_edge("epsilon", "y")

    # plates
    pgm.add_plate(
        daft.Plate([3.5, 0.6, 2, 0.9], label=r"$n \in 1:N$", shift=-0.1))

    pgm.add_plate(
        daft.Plate([2.5, 0.5, 3.1, 1.1],
                   label=r"$mfr \in 1:N_{mfr}$",
                   shift=-0.1))

    pgm.add_plate(
        daft.Plate([1.5, 0.4, 4.2, 1.3],
                   label=r"$parent \in 1:N_{parent}$",
                   shift=-0.1))

    pgm.render()
    plt.show()
Пример #2
0
def make_anova_graph(group_observed=True,
                     value_observed=False,
                     params_known=True):

    group = daft.Node("$g$", "$g$", x=3, y=6, observed=group_observed, scale=3)
    value = daft.Node("$y$", "$y$", x=6, y=6, observed=value_observed, scale=3)
    group_params = daft.Node("$A$",
                             "$A$",
                             observed=params_known,
                             x=3,
                             y=3,
                             scale=3)
    shared_param1 = daft.Node("$\sigma$",
                              "$\sigma$",
                              observed=params_known,
                              x=4.5,
                              y=9,
                              scale=3)
    shared_param2 = daft.Node(r"$\mu_{G}$",
                              r"$\mu_{G}$",
                              observed=params_known,
                              x=2.5,
                              y=9,
                              scale=3)

    nodes = [group, value, group_params, shared_param1, shared_param2]
    edges = [("$g$", "$y$"), ("$A$", "$y$"), ("$\sigma$", "$y$"),
             (r"$\mu_{G}$", "$y$")]

    graph = daft.PGM(shape=(9, 12), label_params={"fontsize": "xx-large"})

    within_group_plate = daft.Plate([1.5, 4.5, 6, 3],
                                    label="$N$",
                                    position="bottom right",
                                    rect_params={"lw": 2})
    between_group_plate = daft.Plate([1, 1, 7, 7],
                                     label="$K$",
                                     position="bottom right",
                                     rect_params={"lw": 2})

    plates = [within_group_plate, between_group_plate]

    [graph.add_node(node) for node in nodes]
    [graph.add_edge(*edge, head_width=0.5, lw=4) for edge in edges]
    [graph.add_plate(plate) for plate in plates]

    shared_util.clean_plates(graph)

    graph.render()
Пример #3
0
def make_bivariate_graph(param, obs, observed=False, plate=False, scale=2):
    arrow_params = {"linewidth": 2, "head_width": 0.25}
    if param == "sigma":
        param_string = r"$\sigma$"
    elif param == "mu":
        param_string = r"$\mu$"
    else:
        param_string = param

    param_node = daft.Node("param", param_string, 1, 2.5, scale=scale)
    obs_node = daft.Node("obs", obs, 3.5, 2.5, scale=scale, observed=observed)

    obs_plate = daft.Plate([2.5, 1.5, 2, 2],
                           label=r"$N$",
                           position="bottom right")

    bivariate_model_graph = daft.PGM([5, 5],
                                     line_width=2,
                                     label_params={"fontsize": 32})

    bivariate_model_graph.add_node(param_node)
    bivariate_model_graph.add_node(obs_node)

    bivariate_model_graph.add_edge("param", "obs", **arrow_params)

    if plate:
        bivariate_model_graph.add_plate(obs_plate)

        # Avoid fill with blue in newer versions of matplotlib
        bivariate_model_graph._plates[0].bbox["fc"] = "white"

    bivariate_model_graph.render()
    return bivariate_model_graph
Пример #4
0
def weakLensing():
    pgm = daft.PGM([4.7, 2.35], origin=[-1.35, 2.2])

    pgm.add_node(daft.Node("Omega", r"$\Omega$", -1, 4))
    pgm.add_node(daft.Node("rho", r"$\rho$", 0, 4))
    pgm.add_node(
        daft.Node("obs", r"$\epsilon^{\mathrm{obs}}_n$", 1, 4, observed=True))
    pgm.add_node(daft.Node("alpha", r"$\alpha$", 3, 4))
    pgm.add_node(daft.Node("true", r"$\epsilon^{\mathrm{true}}_n$", 2, 4))
    pgm.add_node(daft.Node("sigma", r"$\sigma_n$", 1, 3))
    pgm.add_node(daft.Node("Sigma", r"$\Sigma$", 0, 3))
    pgm.add_node(daft.Node("x", r"$x_n$", 2, 3, observed=True))

    pgm.add_plate(daft.Plate([0.5, 2.25, 2, 2.25], label=r"galaxies $n$"))

    pgm.add_edge("Omega", "rho")
    pgm.add_edge("rho", "obs")
    pgm.add_edge("alpha", "true")
    pgm.add_edge("true", "obs")
    pgm.add_edge("x", "obs")
    pgm.add_edge("Sigma", "sigma")
    pgm.add_edge("sigma", "obs")

    pgm.render()

    pgm.figure.savefig("weaklensing.pdf")
Пример #5
0
def daft_pooled():

    # create the PGM
    pgm = daft.PGM(shape=[4, 2.5],
                   origin=[0, 0],
                   grid_unit=4,
                   label_params={'fontsize': 18})

    # priors
    pgm.add_node(daft.Node("beta", r"$\beta$", 1, 2, scale=2))

    # Latent variable.
    pgm.add_node(daft.Node("mu", r"$\beta X_{n}$", 1, 1, scale=2))

    # noise
    pgm.add_node(daft.Node("epsilon", r"$\epsilon$", 3, 1, scale=2))

    # observed data
    pgm.add_node(daft.Node("y", r"$y_n$", 2, 1, scale=2, observed=True))

    # edges
    pgm.add_edge("beta", "mu")
    pgm.add_edge("mu", "y")
    pgm.add_edge("epsilon", "y")

    # plate
    pgm.add_plate(
        daft.Plate([0.5, 0.6, 2, 0.9], label=r"$n \in 1:N$", shift=-0.1))

    pgm.render()
    plt.show()
Пример #6
0
def pgm_cepheids():

    # Instantiate a PGM.
    pgm = daft.PGM([2.9, 2.7], origin=[0.3, 0.3], grid_unit=2.6, node_unit=1.3, observed_style="inner")

    # Model parameters:
    pgm.add_node(daft.Node("a", r"$a$", 1.0, 2.6))
    pgm.add_node(daft.Node("b", r"$b$", 2.0, 2.6))

    # Latent variable - intrinsic magnitude:
    pgm.add_node(daft.Node("m", r"$m_k$", 1.5, 1.4, fixed=True, offset=(0,-20)))

    # Data - observed magnitude:
    pgm.add_node(daft.Node("mobs", r"$m^{\rm obs}_k$", 2.5, 1.4, observed=True))

    # Constants - magnitude errors and log Periods:
    pgm.add_node(daft.Node("logP", r"$\log_{10} P_k$", 0.9, 1.4, fixed=True, offset=(-3,1)))
    pgm.add_node(daft.Node("merr", r"$\sigma_k$", 1.9, 0.9, fixed=True, offset=(-3,2)))

    # Add in the edges.
    pgm.add_edge("a", "m")
    pgm.add_edge("b", "m")
    pgm.add_edge("logP", "m")
    pgm.add_edge("merr", "mobs")
    pgm.add_edge("m", "mobs")

    # And a plate for the pixels
    pgm.add_plate(daft.Plate([0.5, 0.7, 2.5, 1.4], label=r"cepheids $k$", shift=-0.1))

    # Render and save.
    pgm.render()
    pgm.figure.savefig("pgms_cepheids.png", dpi=300)

    return
Пример #7
0
def make_normal_model_graph():

    arrow_params = {"linewidth": 2, "head_width": 0.25}

    mu_node = daft.Node("mu", r"$\mu$", 1, 2.5, scale=2)
    x_node = daft.Node("x", "x", 3.5, 2.5, scale=2, observed=True)

    x_plate = daft.Plate([2.5, 1.5, 2, 2],
                         label=r"$N$",
                         position="bottom right")

    normal_model_graph = daft.PGM([5, 5],
                                  line_width=2,
                                  label_params={"fontsize": 32})

    normal_model_graph.add_node(mu_node)
    normal_model_graph.add_node(x_node)

    normal_model_graph.add_edge("mu", "x", **arrow_params)

    normal_model_graph.add_plate(x_plate)

    # Avoid fill with blue in newer versions of matplotlib
    normal_model_graph._plates[0].bbox["fc"] = "white"

    normal_model_graph.render()
    return normal_model_graph
def plot_gmm_plate(filename="gmm.png", dpi=100):
    pgm = daft.PGM([3.0, 2.5], origin=(0, 0))
    pgm.add_node(daft.Node("theta", r"$\mathbf{\theta}$", 1, 2, fixed=True))
    pgm.add_node(daft.Node("ti", r"$\mathbf{t}_i$", 1, 1))
    pgm.add_node(daft.Node("xi", r"$\mathbf{x}_i$", 2, 1, observed=True))
    pgm.add_edge("theta", "ti")
    pgm.add_edge("theta", "xi")
    pgm.add_edge("ti", "xi")
    pgm.add_plate(daft.Plate([0.4, 0.5, 2.2, 1.0], label=r"$N$"))
    ax = pgm.render()
    ax.text(0.8, 0.5, 'Gaussian mixture model')
    pgm.savefig(filename, dpi=dpi)
Пример #9
0
def make_figure_8p5():
    """Create a graph like figure 8.5."""
    pgm = daft.PGM([3.5, 2.5], origin=[-0.3, -0.3])
    pgm.add_node(daft.Node("s2", r"$\sigma^2$", 0.2, 0.2, fixed=True))
    pgm.add_node(daft.Node("xn", r"$x_n$", 1.5, 1.6, fixed=True))
    pgm.add_node(daft.Node("tn", r"$t_n$", 1.5, 0.2, observed=True))
    pgm.add_node(daft.Node("w", r"$\boldsymbol{w}$", 2.5, 0.2))
    pgm.add_node(daft.Node("alpha", r"$\alpha$", 2.5, 1.6, fixed=True))
    pgm.add_plate(daft.Plate([0.8, 0.0, 1.2, 1.9], label=r"$N$", shift=-0.1))
    pgm.add_edge("s2", "tn")
    pgm.add_edge("xn", "tn")
    pgm.add_edge("w", "tn")
    pgm.add_edge("alpha", "w")
    pgm.render()
    pgm.figure.savefig("figures/fig_8p5.pdf")
def draw():

    # Instantiate a PGM.
    pgm = daft.PGM([3.3, 3.0],
                   origin=[0.3, 0.3],
                   grid_unit=2.6,
                   node_unit=1.3,
                   observed_style="inner")

    # Model parameters:
    pgm.add_node(daft.Node("m", r"$m$", 0.8, 2.8))
    pgm.add_node(daft.Node("b", r"$b$", 1.8, 2.8))

    # Latent variable - intrinsic or true y:
    pgm.add_node(
        daft.Node("ytrue",
                  r"$y^{\rm true}_k$",
                  1.3,
                  1.4,
                  fixed=True,
                  offset=(10, -25)))

    # Data - observed y:
    pgm.add_node(daft.Node("y", r"$y_k$", 2.5, 1.4, observed=True))

    # Constants - x and errors:
    pgm.add_node(
        daft.Node("sigma", r"$\sigma_k$", 1.9, 0.9, fixed=True,
                  offset=(-3, 0)))
    pgm.add_node(daft.Node("x", r"$x_k$", 0.8, 1.1, fixed=True))

    # Add in the edges.
    pgm.add_edge("m", "ytrue")
    pgm.add_edge("x", "ytrue")
    pgm.add_edge("b", "ytrue")
    pgm.add_edge("sigma", "y")
    pgm.add_edge("ytrue", "y")

    # And a plate for the pixels
    pgm.add_plate(
        daft.Plate([0.5, 0.7, 2.7, 1.4], label=r"datapoints $k$", shift=-0.1))

    # Render and save.
    pgm.render()
    pgm.figure.savefig("straightline_pgm.png", dpi=300)

    return
Пример #11
0
def pgm(path):
    from matplotlib import rc
    rc("font", family="serif", size=12)

    import daft

    pgm = daft.PGM([3.3, 3.1], origin=[0.4, 0.3])

    # First round
    pgm.add_node(daft.Node("alpha", r"$\alpha$", 1, 3, fixed=True))
    pgm.add_node(daft.Node("theta", r"$\theta$", 1, 2))
    pgm.add_node(daft.Node("X", r"$X$", 1, 1, observed=True))

    # Second rounds
    pgm.add_node(daft.Node("beta", r"$\beta$", 3, 3, fixed=True))
    pgm.add_node(daft.Node("pi", r"$\pi_{AB}$", 3, 2))
    pgm.add_node(daft.Node("Y", r"$Y_{AB}$", 3, 1, observed=True))

    # Middle part
    pgm.add_node(daft.Node("S", r"$S_{AB}$", 2, 2.5))
    pgm.add_node(daft.Node("T", r"$T$", 2, 1.5))

    pgm.add_edge("alpha", "theta")
    pgm.add_edge("theta", "X")

    pgm.add_edge("beta", "pi")
    pgm.add_edge("pi", "Y")
    pgm.add_edge("theta", "T")
    pgm.add_edge("theta", "S")
    pgm.add_edge("S", "T")
    pgm.add_edge("pi", "T")

    # First round polls
    #pgm.add_plate(daft.Plate([0.5, 0.5, 1, 1]))

    # Second round polls
    #pgm.add_plate(daft.Plate([2.5, 0.5, 1, 1]))

    # Second rounds
    pgm.add_plate(daft.Plate([2.4, 0.4, 1.2, 2.2]))

    # Render and save.
    pgm.render()
    pgm.figure.savefig(path, dpi=300)
Пример #12
0
def create_plate(x,
                 y,
                 width,
                 height,
                 label,
                 label_offset=None,
                 bbox=None,
                 **kwargs):
    if label_offset is None:
        label_offset = [4, 4]
    if bbox is None:
        bbox = {'fc': 'None', 'ec': 'None'}

    plate = daft.Plate([x, y, width, height],
                       label,
                       label_offset=label_offset,
                       bbox=bbox,
                       **kwargs)
    return plate
Пример #13
0
def make_science_model_graph(observed=True, plate=False):

    arrow_params = {"linewidth": 2, "head_width": 0.25}
    label_params = {"fontsize": "small"}

    hypothesis_node = daft.Node(
        "hypothesis",
        r"$H_0$",
        1.5,
        2.5,
        scale=2.2,  #aspect=1.5,
        label_params=label_params)
    result_node = daft.Node("result",
                            "Result",
                            4,
                            2.5,
                            scale=2.2,
                            observed=observed,
                            label_params=label_params)

    result_plate = daft.Plate([2.5, 1.5, 2, 2],
                              label=r"$N$",
                              position="bottom right")

    science_model_graph = daft.PGM([5, 5],
                                   line_width=2,
                                   label_params={"fontsize": 32})

    science_model_graph.add_node(hypothesis_node)
    science_model_graph.add_node(result_node)

    science_model_graph.add_edge("hypothesis", "result", **arrow_params)

    if plate:
        science_model_graph.add_plate(result_plate)

        # Avoid fill with blue in newer versions of matplotlib
        science_model_graph._plates[0].bbox["fc"] = "white"

    science_model_graph.render()
    plt.tight_layout()
    return science_model_graph
Пример #14
0
def make_figure_8p7():
    """Create a graph like figure 8.7."""
    pgm = daft.PGM([5, 4], origin=[-0.3, -0.3])
    pgm.add_node(
        daft.Node("s2", r"$\sigma^2$", 1.0, 0.2, fixed=True, offset=[-9.,
                                                                     -7.]))
    pgm.add_node(daft.Node("xn", r"$x_n$", 1.0, 2.6, fixed=True))
    pgm.add_node(daft.Node("tn", r"$t_n$", 1.0, 1.2, observed=True))
    pgm.add_node(daft.Node("w", r"$\boldsymbol{w}$", 3.5, 1.2))
    pgm.add_node(daft.Node("alpha", r"$\alpha$", 3.5, 2.6, fixed=True))
    pgm.add_node(daft.Node("t_hat", r"$\hat t$", 3.5, 0.2))
    pgm.add_node(daft.Node("x_hat", r"$\hat x$", 4.5, 0.2, fixed=True))
    pgm.add_plate(daft.Plate([0.2, 0.8, 1.3, 2.1], label=r"$N$", shift=-0.1))
    pgm.add_edge("s2", "tn")
    pgm.add_edge("xn", "tn")
    pgm.add_edge("w", "tn")
    pgm.add_edge("alpha", "w")
    pgm.add_edge("w", "t_hat")
    pgm.add_edge("s2", "t_hat")
    pgm.add_edge("x_hat", "t_hat")
    pgm.render()
    pgm.figure.savefig("figures/fig_8p7.pdf")
def inverse():

    # Instantiate a PGM.
    pgm = daft.PGM([3.3, 3.0],
                   origin=[0.3, 0.3],
                   grid_unit=2.6,
                   node_unit=1.3,
                   observed_style="inner")

    # Model parameters.
    pgm.add_node(daft.Node("theta", r"$\theta$", 1.7, 2.8))

    # Latent variable: Poisson mean in kth pixel
    pgm.add_node(
        daft.Node("mean", r"$\mu_k$", 1.7, 1.7, fixed=True, offset=(-12, 0)))

    # Constants - ex and pb
    pgm.add_node(daft.Node("ex", r"ex$_k$", 0.9, 1.7, fixed=True))
    pgm.add_node(
        daft.Node("pb", r"pb$_k$", 1.3, 1.1, fixed=True, offset=(-8, 0)))

    # Data - counts in kth pixel:
    pgm.add_node(daft.Node("counts", r"$N_k$", 2.7, 1.7, observed=True))

    # Add in the edges.
    pgm.add_edge("theta", "mean")
    pgm.add_edge("ex", "mean")
    pgm.add_edge("pb", "mean")
    pgm.add_edge("mean", "counts")

    # And a plate for the pixels
    pgm.add_plate(
        daft.Plate([0.5, 0.7, 2.7, 1.7], label=r"pixels $k$", shift=-0.1))

    # Render and save.
    pgm.render()
    pgm.figure.savefig("cluster_pgm_inverse.png", dpi=300)

    return
Пример #16
0
import daft

# Instantiate the PGM.
pgm = daft.PGM([2.2, 3.55], origin=[-0.6, 0.3])

# Hierarchical parameters.
pgm.add_node(daft.Node("alpha", r"$\alpha$", 1, 3.5, fixed=True))
pgm.add_node(daft.Node("theta", r"$\theta$", 1, 2.5))
pgm.add_node(daft.Node("sigma", r"$\Sigma$", 1, 1))

# Latent variable.
pgm.add_node(daft.Node("X", r"$X_n$", 0, 2))

# Data.
pgm.add_node(daft.Node("x", r"$x_n$", 0, 1, observed=True))

# Add in the edges.
pgm.add_edge("alpha", "theta")
pgm.add_edge("theta", "X")
pgm.add_edge("sigma", "x")
pgm.add_edge("X", "x")

# And a plate.
pgm.add_plate(daft.Plate([-0.5, 0.5, 1, 2], label=r"stars $n$", shift=-0.1))

# Render and save.
pgm.render()
pgm.figure.savefig("xdstars.pdf")
pgm.figure.savefig("xdstars.png", dpi=150)
Пример #17
0
import daft

# Instantiate the PGM.
pgm = daft.PGM([2.3, 2.05], origin=[0.3, 0.3], line_width=2.5)

# Hierarchical parameters.
pgm.add_node(daft.Node("alpha", r"$\boldsymbol{\alpha}$", 0.5, 2, fixed=True))
pgm.add_node(daft.Node("beta", r"$\boldsymbol{\beta}$", 1.5, 2))

# Latent variable.
pgm.add_node(daft.Node("w", r"$\boldsymbol{w_n}$", 1, 1))

# Data.
pgm.add_node(daft.Node("x", r"$\boldsymbol{x_n}$", 2, 1, observed=True))

# Add in the edges.
pgm.add_edge("alpha", "beta")
pgm.add_edge("beta", "w")
pgm.add_edge("w", "x")
pgm.add_edge("beta", "x")

# And a plate.
pgm.add_plate(daft.Plate([0.5, 0.5, 2, 1], label=r"$\boldsymbol{n = 1, \cdots, N}$",
    shift=-0.1))

# Render and save.
pgm.render(interactive=True)
pgm.figure.savefig("thicklines.pdf")
pgm.figure.savefig("thicklines.png", dpi=150)
Пример #18
0
"""

from matplotlib import rc
rc("font", family="serif", size=12)
rc("text", usetex=True)

import daft

pgm = daft.PGM([8, 6.75], origin=[0.5, 0.5], grid_unit=4., node_unit=1.4)

# Start with the plates.
tweak = 0.02
rect_params = {"lw": 2}
pgm.add_plate(
    daft.Plate([1.5 + tweak, 0.5 + tweak, 6.0 - 2 * tweak, 3.75 - 2 * tweak],
               label=r"\Large telescope+camera+filter multiplets",
               rect_params=rect_params))
pgm.add_plate(
    daft.Plate([2.5 + tweak, 1.0 + tweak, 4.0 - 2 * tweak, 2.75 - 2 * tweak],
               label=r"\Large images",
               rect_params=rect_params))
pgm.add_plate(
    daft.Plate([3.5 + tweak, 1.5 + tweak, 2.0 - 2 * tweak, 1.75 - 2 * tweak],
               label=r"\Large pixel patches",
               rect_params=rect_params))
pgm.add_plate(
    daft.Plate([1.0 + tweak, 4.25 + tweak, 3.5 - 2 * tweak, 1.75 - 2 * tweak],
               label=r"\Large stars",
               rect_params=rect_params))
pgm.add_plate(
    daft.Plate([5.5 + tweak, 4.25 + tweak, 2.5 - 2 * tweak, 1.75 - 2 * tweak],
Пример #19
0
rc("text", usetex=True)

import daft

pgm = daft.PGM([3, 3], origin=[0.6, 0.3])

pgm.add_node(daft.Node(u"sprop", r"$\Theta_\star$", 2, 3))
pgm.add_node(daft.Node(u"pprop", r"$\Theta_\mathrm{p}$", 3, 3))

pgm.add_node(daft.Node(u"star", r"$\star_n$", 2, 2))
pgm.add_node(daft.Node(u"planet", r"$\mathrm{p}_n$", 3, 2))
pgm.add_node(daft.Node(u"select", r"S", 1, 2))

pgm.add_node(daft.Node(u"obs", r"$q_n$", 2, 1, observed=True))
pgm.add_node(daft.Node(u"on", r"$\mathbf{X}_n$", 3, 1, observed=True))

pgm.add_edge(u"sprop", u"star")
pgm.add_edge(u"pprop", u"planet")

pgm.add_edge(u"star", u"on")
pgm.add_edge(u"star", u"obs")

pgm.add_edge(u"planet", u"obs")
pgm.add_edge(u"planet", u"on")
pgm.add_edge(u"select", u"obs")

pgm.add_plate(daft.Plate([1.5, 0.45, 2, 2], label=r"planets $n$"))

pgm.render()
pgm.figure.savefig("simple.pdf")
Пример #20
0
"""

from matplotlib import rc
rc("font", family="serif", size=12)
rc("text", usetex=True)

import daft

pgm = daft.PGM([4.7, 2.35], origin=[-1.35, 2.2])
pgm.add_node(daft.Node("Omega", r"$\Omega$", -1, 4))
pgm.add_node(daft.Node("gamma", r"$\gamma$", 0, 4))
pgm.add_node(
    daft.Node("obs", r"$\epsilon^{\mathrm{obs}}_n$", 1, 4, observed=True))
pgm.add_node(daft.Node("alpha", r"$\alpha$", 3, 4))
pgm.add_node(daft.Node("true", r"$\epsilon^{\mathrm{true}}_n$", 2, 4))
pgm.add_node(daft.Node("sigma", r"$\sigma_n$", 1, 3))
pgm.add_node(daft.Node("Sigma", r"$\Sigma$", 0, 3))
pgm.add_node(daft.Node("x", r"$x_n$", 2, 3, observed=True))
pgm.add_plate(daft.Plate([0.5, 2.25, 2, 2.25], label=r"galaxies $n$"))
pgm.add_edge("Omega", "gamma")
pgm.add_edge("gamma", "obs")
pgm.add_edge("alpha", "true")
pgm.add_edge("true", "obs")
pgm.add_edge("x", "obs")
pgm.add_edge("Sigma", "sigma")
pgm.add_edge("sigma", "obs")
pgm.render(interactive=True)
pgm.figure.savefig("weaklensing.pdf")
pgm.figure.savefig("weaklensing.png", dpi=150)
Пример #21
0
pgm.add_edge("noise model", "noise")
pgm.add_edge("shear field", "true shear")
pgm.add_edge("gal locs", "x")
pgm.add_edge("intrinsic shape", "int ellip")

pgm.add_edge("noise", "obs ellip")
pgm.add_edge("true shear", "obs ellip")
pgm.add_edge("int ellip", "obs ellip")
pgm.add_edge("true shear", "obs ellip")
pgm.add_edge("PSFx", "obs ellip")

pgm.add_edge('atmosphere', 'PSFy')
pgm.add_edge('galaxy model', 'y')
pgm.add_edge('atmosphere', 'PSFx')

pgm.add_plate(
    daft.Plate([0.5, 1.5, 4.5, 2],
               label=r"$j = 1, \cdots, N_{\rm gal}$",
               shift=-0.1))

pgm.add_plate(
    daft.Plate([5.2, 2.5, 2.3, 1],
               label=r"$i = 1, \cdots, N_{\rm star}$",
               shift=-0.1))

# Render and save.
pgm.render()
pgm.figure.savefig("lensing.pdf", dpi=150)

#plt.show()
Пример #22
0
rc("font", family="serif", size=8)
rc("text", usetex=True)

# find this!
import daft

# Colors for different distributions! see Wiecki paper
gamma_color = {"ec": "#46a546"}
normal_color = {"ec": "#f89406"}
halfnormal_color = {"ec": "#0000cd"}
invlogit_color = {"ec": "#d02090"}

pgm = daft.PGM([6, 6])
pgm.add_node(daft.Node("x", r"$x_{(p,s,t)}$", 3, 3.5, observed=True))
# plates - xstart, ystart, xsize, ysize
pgm.add_plate(daft.Plate([2.45, 3, 1.1, 1], label=r"$t = 1, ..., $trials"))

pgm.add_node(daft.Node("z", r"$z_{(p,s)}$", 2, 3.2,
                       plot_params=invlogit_color))
pgm.add_edge("z", "x")
pgm.add_node(daft.Node("mz", r"$\mu_{z(p)}$", 1, 3.2,
                       plot_params=normal_color))
pgm.add_edge("mz", "z")
pgm.add_node(
    daft.Node("sz", r"$\sigma_{z}$", 1, 2.5, plot_params=halfnormal_color))
pgm.add_edge("sz", "z")

pgm.add_node(daft.Node("dc", r"$dc_{(p,s)}$", 2, 3.8,
                       plot_params=normal_color))
pgm.add_edge("dc", "x")
pgm.add_node(
Пример #23
0
pgm.add_node(n)
pgm.add_node(daft.Node("speckle_coeff", r"$z_i$", 2, 3, plot_params=s_color))
pgm.add_node(daft.Node("speckle_img", r"$x_i$", 2, 2, plot_params=s_color))

pgm.add_node(daft.Node("spec", r"$s$", 4, 3, plot_params=p_color))
pgm.add_node(daft.Node("shape", r"$g$", 4, 2, plot_params=p_color))
pgm.add_node(daft.Node("planet_pos", r"$\mu_i$", 3, 3, plot_params=p_color))
pgm.add_node(daft.Node("planet_img", r"$p_i$", 3, 2, plot_params=p_color))

pgm.add_node(daft.Node("pixels", r"$y_i ^j$", 2.5, 1, observed=True))

# Edges.
pgm.add_edge("phi", "speckle_coeff")
pgm.add_edge("speckle_coeff", "speckle_img")
pgm.add_edge("speckle_img", "pixels")

pgm.add_edge("spec", "planet_img")
pgm.add_edge("shape", "planet_img")
pgm.add_edge("planet_pos", "planet_img")
pgm.add_edge("planet_img", "pixels")

# And a plate.
pgm.add_plate(daft.Plate([1.5, 0.2, 2, 3.2], label=r"exposure $i$",
                         shift=-0.1))
pgm.add_plate(daft.Plate([2, 0.5, 1, 1], label=r"pixel $j$", shift=-0.1))

# Render and save.
pgm.render(interactive=True)
pgm.figure.savefig("exoplanets.pdf")
pgm.figure.savefig("exoplanets.png", dpi=150)
Пример #24
0
import os

# Instantiate the PGM.
pgm = daft.PGM([2.3, 2.05], origin=[0.3, 0.3])

# Hierarchical parameters.
pgm.add_node(daft.Node("alpha", r"$\alpha$", 0.5, 2, fixed=True))
pgm.add_node(daft.Node("beta", r"$\beta$", 1.5, 2))

# Latent variable.
pgm.add_node(daft.Node("w", r"$w_n$", 1, 1))

# Data.
pgm.add_node(daft.Node("x", r"$x_n$", 2, 1, observed=True))

# Add in the edges.
pgm.add_edge("alpha", "beta")
pgm.add_edge("beta", "w")
pgm.add_edge("w", "x")
pgm.add_edge("beta", "x")

# And a plate.
pgm.add_plate(
    daft.Plate([0.5, 0.5, 2, 1], label=r"$n = 1, \cdots, N$", shift=-0.1))

# Render and save.
pgm.render()
folder = "/Users/kpmurphy/github/pyprobml/figures"
pgm.figure.savefig(os.path.join(folder, "classic.pdf"))
pgm.figure.savefig(os.path.join(folder, "classic.png"))
Пример #25
0
"""

from matplotlib import rc
rc("font", family="serif", size=12)
rc("text", usetex=True)
import daft
pgm = daft.PGM([5.4, 5.4], origin=[1.2, 1.2])
wide = 1.5
verywide = 1.5 * wide
dy = 0.75

# electrons
el_x, el_y = 2., 2.
pgm.add_plate(
    daft.Plate([el_x - 0.6, el_y - 0.6, 2.2, 2 * dy + 0.3],
               label="electrons $i$"))
pgm.add_node(
    daft.Node("xabc",
              r"xa$_i$,xabc$_i$,ya$_i$,\textit{etc}",
              el_x + 0.5,
              el_y + 0 * dy,
              aspect=2.3 * wide,
              observed=True))
pgm.add_node(
    daft.Node("xyti", r"$x_i,y_i,t_i$", el_x + 1., el_y + 1 * dy, aspect=wide))
pgm.add_edge("xyti", "xabc")

# intensity fields
ph_x, ph_y = el_x + 2.5, el_y + 3 * dy
pgm.add_node(
    daft.Node("Ixyt", r"$I_{\nu}(x,y,t)$", ph_x, ph_y, aspect=verywide))
Пример #26
0
pgm.add_node(daft.Node("v0", r"$\post{\nu}_k$", 1, 1.5, fixed=True))
pgm.add_node(daft.Node("k0", r"$\post{\kappa}_k$", 1, 2, fixed=True))
pgm.add_node(daft.Node("m0", r"$\post{\vm}_k$", 1, 2.5, fixed=True))

pgm.add_node(daft.Node("Lam_k", r"$\vLambda_k$", 3, 1))
pgm.add_node(daft.Node("mu_k", r"$\vmu_k$", 3, 2))

pgm.add_node(daft.Node("alpha0", r"$\post{\valpha}$", 1, 3.5, fixed=True))
pgm.add_node(daft.Node("pi", r"$\vpi$", 5, 3.5))

pgm.add_node(daft.Node("zz_n", r"$\post{\vz}_n$", 5, 1, \
    offset=(-15,-10), fixed=True))
pgm.add_node(daft.Node("z_n", r"$\vz_n$", 5, 2))


pgm.add_plate(daft.Plate([0.5, 0.5, 3, 2.5], label=r"$k=1:K$", shift=-0.1))
pgm.add_plate(daft.Plate([4.5, 0.5, 1, 2], label=r"$n=1:N$", shift=-0.1))



pgm.add_edge("S0", "Lam_k")
pgm.add_edge("v0", "Lam_k")
pgm.add_edge("k0", "mu_k")
pgm.add_edge("m0", "mu_k")
pgm.add_edge("alpha0", "pi")

pgm.add_edge("Lam_k", "mu_k")

pgm.add_edge("zz_n", "z_n")

Пример #27
0
from matplotlib import rc
rc("font", family="serif", size=12)
rc("text", usetex=True)
import daft

if __name__ == "__main__":
    pgm = daft.PGM([3.7, 3.15], origin=[-0.35, 2.2])
    pgm.add_node(daft.Node("omega", r"$\omega$", 2, 5))
    pgm.add_node(daft.Node("true", r"$\tilde{X}_n$", 2, 4))
    pgm.add_node(daft.Node("obs", r"$X_n$", 2, 3, observed=True))
    pgm.add_node(daft.Node("alpha", r"$\alpha$", 3, 4))
    pgm.add_node(daft.Node("Sigma", r"$\Sigma$", 0, 3))
    pgm.add_node(daft.Node("sigma", r"$\sigma_n$", 1, 3))
    pgm.add_plate(daft.Plate([0.5, 2.25, 2, 2.25], label=r"stars $n$"))
    pgm.add_edge("omega", "true")
    pgm.add_edge("true", "obs")
    pgm.add_edge("alpha", "true")
    pgm.add_edge("Sigma", "sigma")
    pgm.add_edge("sigma", "obs")
    pgm.render(interactive=True)
    pgm.figure.savefig("gaia.pdf")
    pgm.figure.savefig("gaia.png", dpi=150)
Пример #28
0
pgm.add_edge('z2', 'w2')
pgm.add_edge('z3', 'w3')

pgm.add_node(daft.Node('phi1', r'$\phi_{t-1, k}$', x=x1, y=TOP - 5))
pgm.add_node(daft.Node('phi2', r'$\phi_{t, k}$', x=x2, y=TOP - 5))
pgm.add_node(daft.Node('phi3', r'$\phi_{t+1, k}$', x=x3, y=TOP - 5))
pgm.add_edge('phi1', 'phi2')
pgm.add_edge('phi2', 'phi3')
pgm.add_edge('phi1', 'w1')
pgm.add_edge('phi2', 'w2')
pgm.add_edge('phi3', 'w3')

w = .1
pgm.add_plate(
    daft.Plate([x_unit / 2 + w, TOP - 5.5, 3 * x_unit - 2 * w, 1 - w],
               label='K',
               label_offset=(5, 10)))
# timeslices
pgm.add_plate(
    daft.Plate([x_unit / 2 + w, TOP - 4.5, x_unit - 2 * w, 4],
               label=r'$D_{t-1}$',
               label_offset=(5, 10)))
pgm.add_plate(
    daft.Plate([x1 + x_unit / 2 + w, TOP - 4.5, x_unit - 2 * w, 4],
               label=r'$D_t$',
               label_offset=(5, 10)))
pgm.add_plate(
    daft.Plate([x2 + x_unit / 2 + w, TOP - 4.5, x_unit - 2 * w, 4],
               label=r'$D_{t+1}$',
               label_offset=(5, 10)))
# documents
Пример #29
0
pgm.add_edge("alphag", "Fg")
pgm.add_edge("betag", "Fg")
pgm.add_edge("Fg", "mus")
pgm.add_edge("Fb", "mus")
pgm.add_edge("mus", "Ns")
pgm.render()
pgm.figure.savefig("hier_poissoneg_pgm2.png", dpi=200)

pgm = daft.PGM([3.2, 2.25], observed_style="inner")
pgm.add_node(daft.Node("alphab", r"$\alpha_b$", 0.25, 0.15, fixed=True))
pgm.add_node(daft.Node("betab", r"$\beta_b$", 0.25, 0.55, fixed=True))
pgm.add_node(daft.Node("Fb", r"$F_b$", 1.65, 0.4))
pgm.add_node(daft.Node("Nb", r"$N_b$", 2.8, 0.4, observed=True))
pgm.add_edge("alphab", "Fb")
pgm.add_edge("betab", "Fb")
pgm.add_edge("Fb", "Nb")
pgm.add_node(daft.Node("Lstar", r"$L^\ast$", 0.9, 1.725))  # 1.4
pgm.add_node(daft.Node("alpha", r"$\alpha$", 0.9, 1.025))  # 0.85
pgm.add_node(daft.Node("Lg", r"$L_{g,i}$", 1.9, 1.4))
pgm.add_node(daft.Node("Ns", r"$N_{s,i}$", 2.8, 1.4, observed=True))
pgm.add_edge("Lstar", "Lg")
pgm.add_edge("alpha", "Lg")
pgm.add_edge("Fb", "Ns")
pgm.add_edge("Lg", "Ns")
pgm.add_plate(
    daft.Plate([1.45, 0.85, 1.65, 1.1],
               label=r"${}_{i=1,\ldots,m}$",
               position='bottom left'))
pgm.render()
pgm.figure.savefig("hier_poissoneg_pgm3.png", dpi=200)
Пример #30
0
# Add in the edges.
pgm.add_edge("alpha", "DP")
pgm.add_edge("DP", "pi_k")
pgm.add_edge("pi_k", "z_n")
pgm.add_edge("z_n", "v_mn")
pgm.add_edge("d_mn", "v_mn")

pgm.add_edge("H", "phi_mk")
pgm.add_edge("phi_mk", "v_mn")

# Add plates.

pgm.add_plate(
    daft.Plate([1.55, 2.60, 2, 1],
               label=r"$k = 1, \cdots, K, ..., (\infty)$",
               shift=-0.1,
               label_offset=[115, 0]))

pgm.add_plate(
    daft.Plate([0.4, 0.45, 3, 1.83],
               label=r"$n = 1, \cdots, N$",
               shift=-0.1,
               label_offset=[175, 0]))

pgm.add_plate(
    daft.Plate([2.55, 2.78, 0.85, 0.62],
               label=r"$m = 1, \cdots, M$",
               shift=-0.1,
               label_offset=[49, 20]))

pgm.add_plate(