Exemplo n.º 1
0
def fun_sir(df, rtol=1e-4):
    r"""Fully-vectorized SIR solver

    SIR IVP solver, vectorized over parameter values **and** time. The routine identifies groups of parameter values and runs a vectorized IVP solver over all associated time points, and gathers all results into a single output DataFrame. Intended for use in a grama model.

    Args:
        df (pd.DataFrame): All input values; must contain columns for all SIR parameters

    Preconditions:
        ["t", "S0", "I0", "R0", "beta", "gamma"] in df.columns

    Postconditions:
        Row-ordering of input data is reflected in the row-ordering of the output.

    Returns:
        pd.DataFrame: Solution results
    """
    ## Find all groups of non-t parameters
    df_grouped = (df >> gr.tf_mutate(_idx=DF.index,
                                     _code=gr.str_c(
                                         "S0",
                                         DF.S0,
                                         "I0",
                                         DF.I0,
                                         "R0",
                                         DF.R0,
                                         "beta",
                                         DF.beta,
                                         "gamma",
                                         DF.gamma,
                                     )))

    ## Run time-vectorized SIR solver over each group
    df_results = gr.df_grid()
    codes = set(df_grouped._code)
    for code in codes:
        df_param = (df_grouped >> gr.tf_filter(DF._code == code))

        df_results = (df_results >> gr.tf_bind_rows(
            sir_vtime(
                df_param.t,
                df_param.S0[0],
                df_param.I0[0],
                df_param.R0[0],
                df_param.beta[0],
                df_param.gamma[0],
                rtol=rtol,
            ) >> gr.tf_mutate(_idx=df_param._idx)))

    ## Sort to match original ordering
    # NOTE: Without this, the output rows will be scrambled, relative
    # to the input rows, leading to very confusing output!
    return (df_results >> gr.tf_arrange(DF._idx) >> gr.tf_drop("_idx"))
Exemplo n.º 2
0
def tran_kfolds(
    df,
    k=None,
    ft=None,
    out=None,
    var_fold=None,
    suffix="_mean",
    summaries=None,
    tf=tf_summarize,
    shuffle=True,
    seed=None,
):
    r"""Perform k-fold CV

    Perform k-fold cross-validation (CV) using a given fitting procedure (ft).
    Optionally provide a fold identifier column, or (randomly) assign folds.

    Args:
        df (DataFrame): Data to pass to given fitting procedure
        ft (gr.ft_): Partially-evaluated grama fit function; defines model fitting
            procedure and outputs to aggregate
        tf (gr.tf_): Partially-evaluated grama transform function; evaluation of
            fitted model will be passed to tf and provided with keyword arguments
            from summaries
        out (list or None): Outputs for which to compute `summaries`; None uses ft.out
        var_fold (str or None): Column to treat as fold identifier; overrides `k`
        suffix (str): Suffix for predicted value; used to distinguish between predicted and actual
        summaries (dict of functions): Summary functions to pass to tf; will be evaluated
            for outputs of ft. Each summary must have signature summary(f_pred, f_meas).
            Grama includes builtin options: gr.mse, gr.rmse, gr.rel_mse, gr.rsq, gr.ndme
        k (int): Number of folds; k=5 to k=10 recommended [1]
        shuffle (bool): Shuffle the data before CV? True recommended [1]

    Notes:
        - Many grama functions support *partial evaluation*; this allows one to specify things like hyperparameters in fitting functions without providing data and executing the fit. You can take advantage of this functionality to easly do hyperparameter studies.

    Returns:
        DataFrame: Aggregated results within each of k-folds using given model and
            summary transform

    References:
        [1] James, Witten, Hastie, and Tibshirani, "An introduction to statistical learning" (2017), Chapter 5. Resampling Methods

    Examples:

        >>> import grama as gr
        >>> from grama.data import df_stang
        >>> from grama.fit import ft_rf
        >>> df_kfolds = (
        >>>     df_stang
        >>>     >> gr.tf_kfolds(
        >>>         k=5,
        >>>         ft=ft_rf(out=["thick"], var=["E", "mu"]),
        >>>     )

    """
    ## Check invariants
    if ft is None:
        raise ValueError("Must provide ft keyword argument")
    if (k is None) and (var_fold is None):
        print("... tran_kfolds is using default k=5")
        k = 5
    if summaries is None:
        print("... tran_kfolds is using default summaries mse and rsq")
        summaries = dict(mse=mse, rsq=rsq)

    n = df.shape[0]
    ## Handle custom folds
    if not (var_fold is None):
        ## Check for a valid var_fold
        if not (var_fold in df.columns):
            raise ValueError("var_fold must be in df.columns or None")
        ## Build folds
        levels = unique(df[var_fold])
        k = len(levels)
        print("... tran_kfolds found {} levels via var_folds".format(k))
        Is = []
        for l in levels:
            Is.append(list(arange(n)[df[var_fold] == l]))

    else:
        ## Shuffle data indices
        if shuffle:
            if seed:
                set_seed(seed)
            I = permutation(n)
        else:
            I = arange(n)
        ## Build folds
        di = int(ceil(n / k))
        Is = [I[i * di:min((i + 1) * di, n)] for i in range(k)]

    ## Iterate over folds
    df_res = DataFrame()
    for i in range(k):
        ## Train by out-of-fold data
        md_fit = df >> tf_filter(~var_in(X.index, Is[i])) >> ft

        ## Determine predicted and actual
        if out is None:
            out = str_replace(md_fit.out, suffix, "")
        else:
            out = str_replace(out, suffix, "")

        ## Test by in-fold data
        df_pred = md_fit >> ev_df(df=df >> tf_filter(var_in(X.index, Is[i])),
                                  append=False)

        ## Specialize summaries for output names
        summaries_all = ChainMap(*[{
            key + "_" + o: fun(X[o + suffix], X[o])
            for key, fun in summaries.items()
        } for o in out])

        ## Aggregate
        df_summary_tmp = (
            df_pred >>
            tf_bind_cols(df[out] >> tf_filter(var_in(X.index, Is[i]))) >>
            tf(**summaries_all)
            # >> tf_mutate(_kfold=i)
        )

        if var_fold is None:
            df_summary_tmp = df_summary_tmp >> tf_mutate(_kfold=i)
        else:
            df_summary_tmp[var_fold] = levels[i]

        df_res = concat((df_res, df_summary_tmp),
                        axis=0).reset_index(drop=True)

    return df_res
Exemplo n.º 3
0
def get_buoyant_properties(df_hull_rot, df_mass, w_slope, w_intercept):
    r"""
    Args:
        df_hull_rot (DataFrame): Rotated hull points
        df_mass (DataFrame): Mass properties
        eq_water (lambda): takes in an x value, spits out a y
    """
    # x and y intervals
    dx = df_mass.dx[0]
    dy = df_mass.dy[0]

    # Define equation for the surface of the water using slope intercept form
    eq_water = lambda x: w_slope * x + w_intercept

    # Find points under sloped waterline
    df_hull_under = (df_hull_rot >> gr.tf_mutate(
        under=df_hull_rot.y <= eq_water(df_hull_rot.x)) >>
                     gr.tf_filter(DF.under == True))

    # x and y position of COB
    x_cob = average(df_hull_under.x)
    y_cob = average(df_hull_under.y)

    # Pull x and y of COM as well
    x_com = df_mass.x[0]
    y_com = df_mass.y[0]

    # Total mass of water by finding area under curve
    m_water = RHO_WATER * len(df_hull_under) * dx * dy

    # Net force results from the difference in masses between the boat and the water
    F_net = (m_water - df_mass.mass[0]) * G

    # Distance to determine torque is ORTHOGONAL TO WATERLINE
    # Equation from https://www.geeksforgeeks.org/perpendicular-distance-between-a-point-and-a-line-in-2-d/

    # Calculate righting moment for different cases of w_slope
    # Account for zero slope
    if w_slope == 0:
        M_net = G * m_water * x_cob
    else:
        #         norm_dist = ((1 * y_com) + (1/w_slope * x_com) + ((1/w_slope) * x_cob) + y_cob) / np.sqrt(1 + (1/w_slope)**2)
        a = 1 / w_slope
        b = 1
        c = (1 / w_slope) * x_cob + y_cob
        x1 = x_com
        y1 = y_com

        norm_dist = (a * x1 + b * y1 + c) / np.sqrt(a**2 + b**2)

        if w_slope > 0:  # positive water slope creates a positive moment
            M_net = G * m_water * norm_dist
        elif w_slope < 0:  # negative water slope creates a negative moment
            M_net = -G * m_water * norm_dist

    return DataFrame(dict(
        x=[x_cob],
        y=[y_cob],
        F_net=[F_net],
        M_net=[M_net],
    ))