Skip to content

[Design]: Automatic specialization of distribution family. #1

@galenseilis

Description

@galenseilis

I am leaning towards automatically specializing the classes of distribution used where possible. I have spotted two opportunities to do this.

The first is that sometimes in change of variables we know the resulting distribution. For example, the sum of two independent exponential random variables has an Erlang distribution. This can be dealt with by using operator overloading where __add__ will return the appropriate Erlang distribution given the left and right operands are exponential.

The second is upon initialization, at least from a users perspective. Sometime a distribution under one family of distribution is exactly a member of another distribution. For example, every gamma distribution Gamma(n, beta) where n is a positive integer is also an Erlang distribution. Thus many things we can say about gamma distributions will apply in the restricted case of Erlang distributions. The implementation approach here would be to use __new__ to return a different class depending on the input parameters given.

Here is a toy Python example of using __new__ to change what class appears to be initialized.

class Bar:
    def __init__(self, a):
        self.a = a

    def greet(self):
        print("Hello from Bar!")

class Baz:
    def __init__(self, a):
        self.a = a

    def greet(self):
        print("Hello from Baz!")

class Foo:
    def __new__(cls, a):
        if a == 0:
            return Bar(a)
        elif a == 1:
            return Baz(a)
        else:
            return super(Foo, cls).__new__(cls)

# Example usage
obj1 = Foo(0)
obj2 = Foo(1)

print(type(obj1))  # <class '__main__.Bar'>
print(type(obj2))  # <class '__main__.Baz'>

obj1.greet()  # Hello from Bar!
obj2.greet()  # Hello from Baz!

It could be tempting to try to create a class hierarchy around types of distributions, but I don't want to go there. Partly b/c I am curious about trying this alternative. Partly because I am not a fan of the strong coupling introduced by deep class hierarchies. Partly b/c there is not a unique way to generalize a given distribution.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions