Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev mixed source #226

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
2584c18
Include source of var. in Marginals
eys123 Jul 11, 2023
7e1c4d7
Split copulas into copula_real and copula_err
eys123 Jul 20, 2023
8d62730
Double check source is only 'real' or 'error'
eys123 Jul 20, 2023
c95a1e9
Add thorough checks for sources in copula
eys123 Jul 21, 2023
afd9fe0
Add checks for single-source cases in sample
eys123 Jul 25, 2023
e0efe0b
Add mixed source capabilities to eval_sample()
MayaSimone Jul 25, 2023
d09017d
Add sources to marginals
MayaSimone Jul 26, 2023
164e536
Merge branch 'dev_mixed_source' of github.com:eys123/py_grama into mi…
MayaSimone Jul 27, 2023
1b9263d
Add checks for copula, fix indices
eys123 Jul 28, 2023
985ad80
Merge branch 'master' into dev_mixed_source
zdelrosario Jul 28, 2023
a2bee4d
Merge branch 'master' of github.com:zdelrosario/py_grama into dev_mix…
eys123 Jul 28, 2023
cb223dd
Adding standard n for mixed data
MayaSimone Jul 28, 2023
eabccad
Fix gibrat typo
eys123 Jul 28, 2023
81b3263
Add functionality for gaussian copulas
eys123 Aug 3, 2023
61588d4
Pull merge
eys123 Aug 3, 2023
9910a21
Resolve merge errors
eys123 Aug 3, 2023
be63ec6
Add one n with mized sources capability
MayaSimone Aug 3, 2023
e16868f
Merge branch 'dev_mixedSource' of github.com:MayaSimone/py_grama into…
eys123 Aug 3, 2023
c6100d6
Merged mixed source development
MayaSimone Aug 3, 2023
4fcc4a3
Resolve merge error
eys123 Aug 3, 2023
b4b1c91
Merge branch 'dev_mixedSource' of github.com:MayaSimone/py_grama into…
eys123 Aug 3, 2023
7aad06f
Handle mixed source cases with just n
eys123 Aug 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 86 additions & 30 deletions grama/comp_building.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,19 @@
"getvars",
]

from grama import add_pipe, CopulaGaussian, CopulaIndependence, Density, \
Function, FunctionModel, FunctionVectorized, Marginal, MarginalNamed, \
pipe, tran_copula_corr
from grama import (
add_pipe,
CopulaGaussian,
CopulaIndependence,
Density,
Function,
FunctionModel,
FunctionVectorized,
Marginal,
MarginalNamed,
pipe,
tran_copula_corr,
)
from .eval_defaults import eval_sample
from collections import ChainMap
from pandas import concat, DataFrame
Expand All @@ -32,8 +42,7 @@
## Model Building Interface (MBI) tools
##################################################
def _comp_function_data(model, fun, var, out, name, runtime):
r"""Internal function builder
"""
r"""Internal function builder"""
model_new = model.copy()

# Check invariants
Expand Down Expand Up @@ -114,6 +123,7 @@ def fun(x, y, z):
"""
return f.__code__.co_varnames


# Freeze inputs
# -------------------------
@curry
Expand All @@ -139,17 +149,15 @@ def comp_freeze(model, df=None, **var):
# Process DataFrame if provided
if not df is None:
if df.shape[0] > 1:
raise ValueError(
"Provided DataFrame must have only one row."
)
raise ValueError("Provided DataFrame must have only one row.")
var = dict(zip(df.columns, df.values.flatten()))

# All variables are provided
var_miss = set(set(var.keys())).difference(model.var)
if len(var_miss) != 0:
raise ValueError(
"All inputs listed in `var` argument must be present in model.var.\n" +
"Missing inputs {}".format(var_miss)
"All inputs listed in `var` argument must be present in model.var.\n"
+ "Missing inputs {}".format(var_miss)
)

if any(map(lambda x: hasattr(x, "__iter__"), var.values())):
Expand All @@ -164,7 +172,7 @@ def comp_freeze(model, df=None, **var):
var_diff,
list(var.keys()),
"(Freeze inputs: {})".format(list(var.keys())),
0
0,
)

## Add to model
Expand Down Expand Up @@ -566,8 +574,13 @@ def comp_marginals(model, **kwargs):
except KeyError:
sign = 0

try:
source = value_copy.pop("source")
except KeyError:
source = "real"

new_model.density.marginals[key] = MarginalNamed(
sign=sign, d_name=dist, d_param=value_copy
sign=sign, d_name=dist, d_param=value_copy, source=source
)

## Handle Marginal input
Expand All @@ -583,7 +596,7 @@ def comp_marginals(model, **kwargs):
# Add copula
##################################################
@curry
def comp_copula_independence(model):
def comp_copula_independence(model, source="real"):
r"""Add an independence copula to model

Composition. Add an independence copula to an existing model.
Expand All @@ -610,10 +623,28 @@ def comp_copula_independence(model):

"""
new_model = model.copy()
new_model.density = Density(
marginals=model.density.marginals,
copula=CopulaIndependence(new_model.var_rand),
)
var_rand_real = []
var_rand_err = []
for var in new_model.var_rand:
if new_model.density.marginals[var].source == "real":
var_rand_real.append(var)
else:
var_rand_err.append(var)

if source == "real":
copula_err = new_model.density.copula_err
new_model.density = Density(
marginals=model.density.marginals,
copula_real=CopulaIndependence(var_rand_real, source),
copula_err=copula_err,
)
else:
copula_real = new_model.density.copula_real
new_model.density = Density(
marginals=model.density.marginals,
copula_err=CopulaIndependence(var_rand_err, source),
copula_real=copula_real,
)
new_model.update()

return new_model
Expand All @@ -623,7 +654,7 @@ def comp_copula_independence(model):

# -------------------------
@curry
def comp_copula_gaussian(model, df_corr=None, df_data=None):
def comp_copula_gaussian(model, df_corr=None, df_data=None, source="real"):
r"""Add a Gaussian copula to model

Composition. Add a gaussian copula to an existing model.
Expand Down Expand Up @@ -663,30 +694,55 @@ def comp_copula_gaussian(model, df_corr=None, df_data=None):
)

"""
new_model = model.copy()
var_rand_real = []
var_rand_err = []
for var in new_model.var_rand:
if new_model.density.marginals[var].source == "real":
var_rand_real.append(var)
else:
var_rand_err.append(var)

if not (df_data is None):
df_corr = tran_copula_corr(df_data, model=new_model)

if not (df_corr is None):
new_model = model.copy()
new_model.density = Density(
marginals=model.density.marginals,
copula=CopulaGaussian(list(model.density.marginals.keys()), df_corr,),
)
if source == "real":
new_model.density = Density(
marginals=model.density.marginals,
copula_real=CopulaGaussian(var_rand_real, df_corr, source),
copula_err=new_model.density.copula_err,
)
else:
new_model.density = Density(
marginals=model.density.marginals,
copula_err=CopulaGaussian(var_rand_err, df_corr, source),
copula_real=new_model.density.copula_real,
)
new_model.update()

return new_model

if not (df_data is None):
new_model = model.copy()
df_corr = tran_copula_corr(df_data, model=new_model)

new_model.density = Density(
marginals=model.density.marginals,
copula=CopulaGaussian(list(model.density.marginals.keys()), df_corr,),
)
if source == "real":
new_model.density = Density(
marginals=model.density.marginals,
copula_real=CopulaGaussian(var_rand_real, df_corr, source),
copula_err=new_model.density.copula_err,
)
else:
new_model.density = Density(
marginals=model.density.marginals,
copula_err=CopulaGaussian(var_rand_err, df_corr, source),
copula_real=new_model.density.copula_real,
)
new_model.update()

return new_model

else:
raise ValueError("Must provide df_corr or df_data")
raise ValueError("Must provide df_corr or df_data")


cp_copula_gaussian = add_pipe(comp_copula_gaussian)
Loading
Loading