Ejemplo n.º 1
0
    def merge(models):
        for model in models:
            if not model.scaled:
                raise ValueError("Only scaled distributions can be merged.")

        (models, xx) = UnivariateModel.intersect_x_all(models)

        model = SplineModel()
        
        for ii in range(len(xx)):
            conditional = SplineModelConditional()
            
            y0 = SplineModel.neginf
            # Loop through each segment
            while y0 != SplineModel.posinf:
                y1 = SplineModel.posinf
                coeffs = np.zeros(3)
                
                for jj in range(len(models)):
                    modcond = models[jj].get_conditional(xx[ii])
                    for kk in range(len(modcond.y0s)):
                        if modcond.y0s[kk] <= y0 and modcond.y1s[kk] > y0:
                            if modcond.y1s[kk] < y1:
                                y1 = modcond.y1s[kk]
                            coeffs[0:len(modcond.coeffs[kk])] = np.array(coeffs[0:len(modcond.coeffs[kk])]) + np.array(modcond.coeffs[kk])

                while len(coeffs) > 0 and coeffs[-1] == 0:
                    coeffs = coeffs[0:-1]
                    
                conditional.add_segment(y0, y1, coeffs)
                y0 = y1

            model.add_conditional(xx[ii], conditional.rescale())

        return model
Ejemplo n.º 2
0
    def combine(one, two):
        if one.xx_is_categorical != two.xx_is_categorical:
            raise ValueError("Cannot combine models that do not agree on categoricity")

        (one, two, xx) = UnivariateModel.intersect_x(one, two)

        means = np.array(one.means) + np.array(two.means)
        sizes = 1 / (1 / np.sqrt(np.array(one.sizes)) + 1 / np.sqrt(np.array(two.means)))**2

        return MeanSizeModel(one.xx_is_categorical, xx, means, sizes)
Ejemplo n.º 3
0
    def combine(one, two):
        if one.xx_is_categorical != two.xx_is_categorical:
            raise ValueError(
                "Cannot combine models that do not agree on categoricity")

        (one, two, xx) = UnivariateModel.intersect_x(one, two)

        means = np.array(one.means) + np.array(two.means)
        sizes = 1 / (1 / np.sqrt(np.array(one.sizes)) +
                     1 / np.sqrt(np.array(two.means)))**2

        return MeanSizeModel(one.xx_is_categorical, xx, means, sizes)
Ejemplo n.º 4
0
    def combine(one, two):
        if one.xx_is_categorical != two.xx_is_categorical:
            raise ValueError("Cannot combine models that do not agree on categoricity")
        if not one.scaled or not two.scaled:
            raise ValueError("Cannot combine unscaled models")

        (one, two, xx) = UnivariateModel.intersect_x(one, two)

        conditionals = []
        for ii in range(len(xx)):
            conditionals.append(one.get_conditional(xx[ii]).convolve(two.get_conditional(xx[ii])).rescale())

        return SplineModel(one.xx_is_categorical, xx, conditionals, True)
Ejemplo n.º 5
0
    def combine(one, two):
        if one.xx_is_categorical != two.xx_is_categorical:
            raise ValueError("Cannot combine models that do not agree on categoricity")
        if not one.scaled or not two.scaled:
            raise ValueError("Cannot combine unscaled models")

        if np.all(np.array(one.locations) == 0):
            return two

        if not isinstance(two, DeltaModel):
            raise ValueError("Combining non-zero delta with non-delta is not yet implemented.")
        
        (one, two, xx) = UnivariateModel.intersect_x(one, two)

        return DeltaModel(one.xx_is_categorical, xx, map(lambda ii: one.locations[ii] + two.locations[ii], range(len(xx))), 1)
Ejemplo n.º 6
0
    def combine(one, two):
        if not one.scaled or not two.scaled:
            raise ValueError("Cannot combine unscaled models")

        (one, two, xx) = UnivariateModel.intersect_x(one, two)

        allxx = set(one.xx) | set(two.xx)
        allxx = np.array(allxx)
        midpts = (allxx[1:] + allxx[:-1]) / 2
        onemodel = one.model.recategorize_x(map(model.get_bin_at, midpts), range(1, len(allxx)))
        twomodel = two.model.recategorize_x(map(model.get_bin_at, midpts), range(1, len(allxx)))

        model = Model.combine([onemodel, twomodel], [1, 1])

        return BinModel(allxx, model, True)
Ejemplo n.º 7
0
    def combine(one, two):
        if not one.scaled or not two.scaled:
            raise ValueError("Cannot combine unscaled models")

        (one, two, xx) = UnivariateModel.intersect_x(one, two)

        allxx = set(one.xx) | set(two.xx)
        allxx = np.array(allxx)
        midpts = (allxx[1:] + allxx[:-1]) / 2
        onemodel = one.model.recategorize_x(map(model.get_bin_at, midpts),
                                            range(1, len(allxx)))
        twomodel = two.model.recategorize_x(map(model.get_bin_at, midpts),
                                            range(1, len(allxx)))

        model = Model.combine([onemodel, twomodel], [1, 1])

        return BinModel(allxx, model, True)
Ejemplo n.º 8
0
    def combine(one, two):
        if one.xx_is_categorical != two.xx_is_categorical:
            raise ValueError("Cannot combine models that do not agree on categoricity")
        if one.yy_is_categorical or two.yy_is_categorical:
            raise ValueError("Cannot combine categorical y models")
        if not one.scaled or not two.scaled:
            raise ValueError("Cannot combine unscaled models")

        (one, two, xx) = UnivariateModel.intersect_x(one, two)

        yy_one_min = min(one.yy)
        yy_one_max = max(one.yy)
        yy_two_min = min(two.yy)
        yy_two_max = max(two.yy)
        yy_step = min(median(diff(sort(one.yy))), median(diff(sort(two.yy))))
        yy_one = arange(yy_one_min, yy_one_max, yy_step)
        if yy_one[-1] + yy_step == yy_one_max:
            yy_one = append(yy_one, [yy_one_max])
        yy_two = arange(yy_two_min, yy_two_max, yy_step)
        if yy_two[-1] + yy_step == yy_two_max:
            yy_two = append(yy_two, [yy_two_max])

        if not array_equal(yy_one, one.yy):
            one = one.interpolate_y(yy_one)
        if not array_equal(yy_two, two.yy):
            two = two.interpolate_y(yy_two)
        pp_one = one.lin_p()
        pp_two = two.lin_p()

        newpp = ones((len(xx), len(yy_one) + len(yy_two) - 1))
        for ii in range(len(xx)):
            newpp[ii,] = convolve(pp_one[ii,], pp_two[ii,])
            newpp[ii,] = newpp[ii,] / sum(newpp[ii,]) # Scale

        yy = append(arange(min(yy_one) + min(yy_two), max(yy_one) + max(yy_two), yy_step), [max(yy_one) + max(yy_two)])

        return DDPModel('ddp1', 'combine', one.xx_is_categorical, xx, False, yy, newpp, scaled=True)
Ejemplo n.º 9
0
    def combine(one, two):
        if one.xx_is_categorical != two.xx_is_categorical:
            raise ValueError("Cannot combine models that do not agree on categoricity")
        if one.yy_is_categorical or two.yy_is_categorical:
            raise ValueError("Cannot combine categorical y models")
        if not one.scaled or not two.scaled:
            raise ValueError("Cannot combine unscaled models")

        (one, two, xx) = UnivariateModel.intersect_x(one, two)

        yy_one_min = min(one.yy)
        yy_one_max = max(one.yy)
        yy_two_min = min(two.yy)
        yy_two_max = max(two.yy)
        yy_step = min(median(diff(sort(one.yy))), median(diff(sort(two.yy))))
        yy_one = arange(yy_one_min, yy_one_max, yy_step)
        if yy_one[-1] + yy_step == yy_one_max:
            yy_one = append(yy_one, [yy_one_max])
        yy_two = arange(yy_two_min, yy_two_max, yy_step)
        if yy_two[-1] + yy_step == yy_two_max:
            yy_two = append(yy_two, [yy_two_max])

        if not array_equal(yy_one, one.yy):
            one = one.interpolate_y(yy_one)
        if not array_equal(yy_two, two.yy):
            two = two.interpolate_y(yy_two)
        pp_one = one.lin_p()
        pp_two = two.lin_p()
        
        newpp = ones((len(xx), len(yy_one) + len(yy_two) - 1))
        for ii in range(len(xx)):
            newpp[ii,] = convolve(pp_one[ii,], pp_two[ii,])
            newpp[ii,] = newpp[ii,] / sum(newpp[ii,]) # Scale

        yy = append(arange(min(yy_one) + min(yy_two), max(yy_one) + max(yy_two), yy_step), [max(yy_one) + max(yy_two)])

        return DDPModel('ddp1', 'combine', one.xx_is_categorical, xx, False, yy, newpp, scaled=True)