class RVkde(RVBase):
    def __init__(self, df, w, key, min, max):
        self.df = df
        self.w = w
        self.key = key
        self.kde = MultivariateNormalTransition(scaling=1)
        self.kde.fit(df[[key]], w)
        self.min = min
        self.max = max

        # Calucating truncated away cdf to compensate in pdf
        min_xs = np.linspace(self.min - 10., self.min, num=200)
        min_pdfs = [
            self.kde.pdf(pd.DataFrame({self.key: [x]})) for x in min_xs
        ]
        min_cdf = np.sum(min_pdfs) * 10 / 200

        max_xs = np.linspace(self.max, self.max + 10, num=200)
        max_pdfs = [
            self.kde.pdf(pd.DataFrame({self.key: [x]})) for x in max_xs
        ]
        max_cdf = np.sum(max_pdfs) * 10 / 200
        self.trunc_cdf = min_cdf + max_cdf

    def rvs(self):
        x = self.kde.rvs()
        while x[0] < self.min or x[0] > self.max:
            x = self.kde.rvs()
        return (x[0])

    def pdf(self, x):
        p = 0.
        if x > self.min and x < self.max:
            x = pd.DataFrame({self.key: [x]})
            p = self.kde.pdf(x)
        return p / (1 - self.trunc_cdf)

    def copy(self):
        return copy.deepcopy(self)

    def pmf(self):
        return 0.

    def cdf(self, x):
        cdf = 0
        if x > self.min:
            xs = np.linspace(self.min, x, num=100)
            pdfs = [self.pdf(x) for x in xs]
            cdf = np.sum(pdfs) * (x - self.min) / 100
        return cdf
Beispiel #2
0
def priors_from_kde(df,w):
    prior_dict = {}
    for key in df.columns:
        kde = MultivariateNormalTransition(scaling=1)
        kde.fit(df[[key]], w)
        x = kde.rvs(1000)
        α,β,loc,scale = scst.beta.fit(x[key])
        prior_dict.update({key: RV("beta", α,β,loc,scale)})
    return(Distribution(**prior_dict))
Beispiel #3
0
priors[1].decorator_repr()

pyabc.parameters.ParameterStructure

pyabc.parameters.from_dictionary_of_dictionaries()


#%%

from pyabc.visualization import kde_1d
from pyabc.transition import MultivariateNormalTransition
import pyabc.random_variables as rv
kde = MultivariateNormalTransition(scaling=1)
kde.fit(df[["p"]], w)

scst.beta.fit(kde.rvs(100), loc=0)

x, pdf = kde_1d(df, w, "p")

class PriorRV(rv.RVBase):
    def __init__

priors[1]["p"]


sckde = scst.gaussian_kde(df["p"])
sckde.pdf(0.9)


beta_priors = [Distribution(**{key: RV("beta", 0.4, 0.4, loc=a, scale=(b - a)) for key, (a,b) in sample_param_spaces[mod].items()}) for mod in model_names]