Exemplo n.º 1
0
def test_adapt_single_model(population_strategy: PopulationStrategy):
    n = 10
    df = pd.DataFrame([{"s": np.random.rand()} for _ in range(n)])
    w = np.ones(n) / n
    kernel = MultivariateNormalTransition()
    kernel.fit(df, w)

    population_strategy.adapt_population_size([kernel], np.array([1.]))
    assert population_strategy.nr_particles > 0
Exemplo n.º 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))
Exemplo n.º 3
0
def test_adapt_two_models(population_strategy: PopulationStrategy):
    n = 10
    kernels = []
    for _ in range(2):
        df = pd.DataFrame([{"s": np.random.rand()} for _ in range(n)])
        w = np.ones(n) / n
        kernel = MultivariateNormalTransition()
        kernel.fit(df, w)
        kernels.append(kernel)

    population_strategy.adapt_population_size(kernels, np.array([.7, .2]))
    assert population_strategy.nr_particles > 0
Exemplo n.º 4
0
def test_no_parameters(population_strategy: PopulationStrategy):
    n = 10
    df = pd.DataFrame(index=list(range(n)))
    w = np.ones(n) / n

    kernels = []
    for _ in range(2):
        kernel = MultivariateNormalTransition()
        kernel.fit(df, w)
        kernels.append(kernel)

    population_strategy.adapt_population_size(kernels, np.array([.7, .3]))
    assert population_strategy.nr_particles > 0
Exemplo n.º 5
0
def test_no_parameters(population_strategy: PopulationStrategy):
    n = 10
    df = pd.DataFrame(index=list(range(n)))
    w = np.ones(n) / n

    kernels = []
    for _ in range(2):
        kernel = MultivariateNormalTransition()
        kernel.fit(df, w)
        kernels.append(kernel)

    population_strategy.update(kernels, np.array([0.7, 0.3]), t=0)
    assert population_strategy(t=0) > 0
Exemplo n.º 6
0
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
Exemplo n.º 7
0
def test_one_with_one_without_parameters(
        population_strategy: PopulationStrategy):
    n = 10
    kernels = []

    df_without = pd.DataFrame(index=list(range(n)))
    w_without = np.ones(n) / n
    kernel_without = MultivariateNormalTransition()
    kernel_without.fit(df_without, w_without)
    kernels.append(kernel_without)

    df_with = pd.DataFrame([{"s": np.random.rand()} for _ in range(n)])
    w_with = np.ones(n) / n
    kernel_with = MultivariateNormalTransition()
    kernel_with.fit(df_with, w_with)
    kernels.append(kernel_with)

    population_strategy.adapt_population_size(kernels, np.array([.7, .3]))
    assert population_strategy.nr_particles > 0
Exemplo n.º 8
0
def test_transitions_not_modified(population_strategy: PopulationStrategy):
    n = 10
    kernels = []
    test_points = pd.DataFrame([{"s": np.random.rand()} for _ in range(n)])

    for _ in range(2):
        df = pd.DataFrame([{"s": np.random.rand()} for _ in range(n)])
        w = np.ones(n) / n
        kernel = MultivariateNormalTransition()
        kernel.fit(df, w)
        kernels.append(kernel)

    test_weights = [k.pdf(test_points) for k in kernels]

    population_strategy.adapt_population_size(kernels, np.array([.7, .2]))

    after_adaptation_weights = [k.pdf(test_points) for k in kernels]

    same = all([(k1 == k2).all()
                for k1, k2 in zip(test_weights, after_adaptation_weights)])
    err_msg = ("Population strategy {}"
               " modified the transitions".format(population_strategy))

    assert same, err_msg
Exemplo n.º 9
0
df, w = history.get_distribution(m=1)
params = test_priors[1]
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)