Search code examples
pythonpandasstringpymc3

Converting a Pandas Dataframe Cell into a Function Input


I have a Dataframe, df, which contains the probability distributions of different Variables (V1, V2, etc.):

    distribution    parameter_1  parameter_2    parameter_3
V1  Beta             132.0         6775.1       -0.2
V2  Beta             297.4        22734.6       -0.5
V3  Gamma            458.8           -0.4        0.0
V4  Beta           40391.7         7619.2      -13.3
V5  Beta             182.9      5148014.0       -0.3
V6  Beta             295.2        12452.0       -0.4
V7  Beta              32.2     11934770.0       -0.2

To select the distribution of "V1", Beta, from the dataframe, and to store it in a named string, I do the following:

variable = df.loc["V1"][0]

This stores the distribution name as a string named "variable".

I would like to use the distribution name which is stored in "variable" in this formula:

V1_obs = pm.Beta('V1_obs', mu=mu, sigma=sigma, observed=V1)

If I tried to substitute "variable" for the distribution name as follows:

V1_obs = pm.variable('V1_obs', mu=mu, sigma=sigma, observed=V1)

It says that the module does not have a function named "variable". Does anyone have any clues on how I could get "variable" to work in the function?


Solution

  • Right now, when you do pm.variable, python looks for object named 'variable' in the module 'pm'. What you want is for the module to look up the corresponding object/function that has the same name as the value of the 'variable'

    You can save the variable name to function mapping in a dict like

    fn_dict = {'Beta':pm.Beta, 'Gamma':pm.Gamma}
    

    and then try,

    V1_obs = fn_dict[variable]('V1_obs', mu=mu, sigma=sigma, observed=V1)
    

    This should work. when looking for 'variable' within the dict, it will return the corresponding object/fn from the module