# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

# # Exploratory Model Analysis
#
# *Purpose*: **TODO**.
#

# ## Setup
#

import grama as gr
DF = gr.Intention()
# %matplotlib inline

#

from grama.models import make_prlc_rand
md_circuit = make_prlc_rand()

# # Basic Facts
#

# ### __qX__ Model summary
#

# solution-begin
md_circuit 
# solution-end

# *Observations*
#
# <!-- task-begin -->
# - ...
#   - (Your response here)
# <!-- task-end -->
#

# ## Model context
#
# The deterministic variables `L, R, C` are the *designed* component values.
#
# The random variables `dL, dR, dC` are *perturbations* to the designed component values.
#

# # Inputs
#
# ## Assess input variability
#

# ### __qX__ Overview of inputs
#

(
    md_circuit
# solution-begin
    >> gr.ev_sample(n=1e3, df_det="nom", skip=True)
    >> gr.pt_auto()
# solution-end
)

# *Observations*
#
# <!-- task-begin -->
# - ...
#   - (Your response here)
# <!-- task-end -->
#

# ### __qX__ Compare designed and realized values
#
# The following plot shows the designed and realized capacitance values. Inspect the plot, then change to log scales for both the x and y axes. Answer the questions under *observations* below.
#

# TASK: Inspect the following plot, change to a log scale
# for both x and y
(
    # NOTE: No need to edit this part of the code
    md_circuit
    >> gr.ev_sample(
        n=20,
        df_det=gr.df_make(
            R=1e-1,
            L=1e-5,
            C=gr.logspace(-3, 2, 20)
        )
    )
    # Visualize
    >> gr.ggplot(gr.aes("C", "Cr"))
    + gr.geom_point()
# task-begin
    # TASK: Add a log scale for x and y
# task-end
# solution-begin
    + gr.scale_x_log10()
    + gr.scale_y_log10()
# solution-end
)

# *Observations*
#
# <!-- task-begin -->
# - ...
#   - (Your response here)
# <!-- task-end -->
#

# # Outputs
#

# ### __qX__ Overview of outputs
#

(
    md_circuit
# solution-begin
    >> gr.ev_sample(n=1e4, df_det="nom")
    >> gr.pt_auto()
# solution-end
)

# *Observations*
#
# *Note*: If you can't reliably make out the shapes of the distributions, try increasing `n`.
#
# <!-- task-begin -->
# - What distribution shapes do the realized component values `Lr, Rr, Cr` have?
#   - (Your response here)
# - What distribution shape does the output `Q` have?
#   - (Your response here)
# - What distribution shape does the output `omega0` have?
#   - (Your response here)
# <!-- task-end -->
# <!-- solution-begin -->
# - What distribution shapes do the realized component values `Lr, Rr, Cr` have?
#   - These are all uniform distributions
# - What distribution shape does the output `Q` have?
#   - This distribution is a bit bell-shaped, but it has a much flatter top and narrower tails than a gaussian.
# - What distribution shape does the output `omega0` have?
#   - This distribution is strongly asymmetric, with a longer right tail (right skew).
# <!-- solution-end -->
#

# ### __qX__ Scatter of outputs
#

(
    md_circuit
    >> gr.ev_sample(n=1e3, df_det="nom")
# solution-begin
    >> gr.ggplot(gr.aes("Q", "omega0"))
    + gr.geom_point()
# solution-end
)


# *Observations*
#
# <!-- task-begin -->
# - ...
#   - (Your response here)
# <!-- task-end -->
#

# ### __qX__ Density of outputs
#
# Visualize `Q` and `omega0` with a 2d bin plot. Increase the sample size `n` to get a "full" plot.
#

(
    md_circuit
# solution-begin
    >> gr.ev_sample(n=1e4, df_det="nom")
    >> gr.ggplot(gr.aes("Q", "omega0"))
    + gr.geom_bin2d()
# solution-end
)

# *Observations*
#
# <!-- task-begin -->
# - Briefly describe the distribution of realized performance (values of `Q` and `omega0`).
#   - (Your response here)
# <!-- task-end -->
# <!-- solution-begin -->
# - Briefly describe the distribution of realized performance (values of `Q` and `omega0`).
#   - The distribution is oddly rectangular, with fairly "sharp" square sides. The distribution seems to end abruptly; there are exactly zero counts outside the "curved rectangle". The values of `Q` and `omega0` are negatively correlated; when `Q` is larger `omega0` tends to be smaller. The distribution is also asymmetric; note that highest `count` (brightest fill color) is *not* in the center, but rather towards the bottom-right.
# <!-- solution-end -->
#

# ### __qX__ Compare nominal with distribution
#

(
    md_circuit
    >> gr.ev_sample(n=1e4, df_det="nom")
    
    >> gr.ggplot(gr.aes("Q", "omega0"))
    + gr.geom_bin2d()
    + gr.geom_point(
# task-begin
        # Use the `data` argument to add a geometry for
        # the output value associated with nominal input values
# task-end
# solution-begin
        data=md_circuit
        >> gr.ev_nominal(df_det="nom"),
# solution-end
        color="salmon",
        size=4,
    )
)

# *Observations*
#
# The nominal design (red point) represents the predicted performance if we assume the nominal circuit component values. 
#
# <!-- task-begin -->
# - How does the distribution of real circuit performance (values of `Q`, `omega0`) compare with the nominal performance?
#   - (Your response here)
# - Is the most likely performance (values of `Q`, `omega0`) the same as the nominal performance? ("Most likely" is where `count` is the largest.)
#   - (Your response here)
# - Assume that another system depends on the particular values of `Q` and `omega0` provided by this system. Would it be safe to assume the performance is within 1% of the nominal performance?
#   - (Your response here)
# <!-- task-end -->
# <!-- solution-begin -->
# - How does the distribution of real circuit performance (values of `Q`, `omega0`) compare with the nominal performance?
#   - The distribution of real performance is quite large, and not at all symmetric about the nominal performance. The values `Q` and `omega0` exhibit negative correlation: when `Q` is larger `omega0` tends to be smaller.
# - Is the most likely performance (values of `Q`, `omega0`) the same as the nominal performance? ("Most likely" is where `count` is the largest.)
#   - No; the most likely performance is at a larger value of `Q` and smaller value of `omega0`.
# - Assume that another system depends on the particular values of `Q` and `omega0` provided by this system. Would it be safe to assume the performance is within 1% of the nominal performance?
#   - No; we see variability in `Q` and `omega0` far greater than 1%!
# <!-- solution-end -->
#

# # Input-to-output Relationships
#

# + [markdown] tags=[]
# ### __qX__ Sinew plot
#
# -

(
    md_circuit
# solution-begin
    >> gr.ev_sinews(df_det="swp")
    >> gr.pt_auto()
# solution-end
)

# *Observations*
#
# <!-- task-begin -->
# - ...
#   - (Your response here)
# <!-- task-end -->
#

StarcoderdataJupyterScriptsDedupFiltered