Esempio n. 1
0
def test_formula_transform():
    """
    Check if variables can be added/multiplied/transformed.
    The resulting expression can be plugged into a model.
    """
    from mle import var

    x = var('x', vector=True, observed=True)
    a = var('a')
    b = var('b')

    formula = a * x**2 + b
Esempio n. 2
0
def test_pdf_product():
    """
    Check if PDF models can be joined
    """
    from mle import var, Normal, Join
    x = var('x', vector=True, observed=True)
    y = var('y', observed=True)
    mu = var('mu')
    sigma = var('sigma')

    model = Join(Join(Normal(x, mu, sigma)), Normal(y, mu, sigma))
    assert(model.observed == [x, y])
Esempio n. 3
0
def test_formula_transform():
    """
    Check if variables can be added/multiplied/transformed.
    The resulting expression can be plugged into a model.
    """
    from mle import var

    x = var('x', vector=True, observed=True)
    a = var('a')
    b = var('b')

    formula = a * x**2 + b
Esempio n. 4
0
def test_pdf_product():
    """
    Check if PDF models can be joined
    """
    from mle import var, Normal, Join
    x = var('x', vector=True, observed=True)
    y = var('y', observed=True)
    mu = var('mu')
    sigma = var('sigma')

    model = Join(Join(Normal(x, mu, sigma)), Normal(y, mu, sigma))
    assert (model.observed == [x, y])
Esempio n. 5
0
def test_error_on_illegal_bound():
    """
    Check if exception is raised when user specifies illegal bound.
    Some distributions automatically apply certain bounds.
    Example: sigma > 0 for the Normal distribution.
    If a user-specified bound conflicts with that, an exception should be thrown.
    """
    from mle import var, Normal

    x = var('x', vector=True, observed=True)
    mu = var('mu')
    sigma = var('sigma', lower=-1)

    Normal(x, mu, sigma)
Esempio n. 6
0
def test_error_on_illegal_bound():
    """
    Check if exception is raised when user specifies illegal bound.
    Some distributions automatically apply certain bounds.
    Example: sigma > 0 for the Normal distribution.
    If a user-specified bound conflicts with that, an exception should be thrown.
    """
    from mle import var, Normal

    x = var('x', vector=True, observed=True)
    mu = var('mu')
    sigma = var('sigma', lower=-1)

    Normal(x, mu, sigma)
Esempio n. 7
0
def test_simple_fit():
    """
    Check if generating/fitting Gaussian data works
    """
    from mle import Normal, vec, var
    import numpy as np

    x = vec('x', observed=True)
    mu = var('mu')
    sigma = var('sigma')

    dist = Normal(x, mu, sigma)
    np.random.seed(42)
    data = dist.sample(1e6, {'mu': 0, 'sigma': 1})
    results = dist.fit(data, {'mu': 1, 'sigma': 2}, method='L-BFGS-B')
Esempio n. 8
0
def test_const():
    """
    Check if parameters can be set to be constant.
    """
    from mle import var, Normal
    import numpy as np
    x = var('x', vector=True, observed=True)
    mu = var('mu', const=True)
    sigma = var('sigma')

    model = Normal(x, mu, sigma)
    np.random.seed(42)
    data = np.random.normal(0, 1, 200)

    results = model.fit({'x': data}, {'mu': 1, 'sigma': 1})
    assert (results.x['mu'] == 1)
Esempio n. 9
0
def test_const():
    """
    Check if parameters can be set to be constant.
    """
    from mle import var, Normal
    import numpy as np
    x = var('x', vector=True, observed=True)
    mu = var('mu', const=True)
    sigma = var('sigma')

    model = Normal(x, mu, sigma)
    np.random.seed(42)
    data = np.random.normal(0, 1, 200)

    results = model.fit({'x': data}, {'mu': 1, 'sigma': 1})
    assert(results.x['mu'] == 1)
Esempio n. 10
0
def test_const():
    """
    Check if parameters can be set to be constant.
    """
    from mle import var, vec, Normal
    import numpy as np
    x = vec('x', observed=True)
    mu = var('mu', const=True)
    sigma = var('sigma')

    model = Normal(x, mu, sigma)
    np.random.seed(42)
    data = model.sample(200, {'mu': 0, 'sigma': 1})

    results = model.fit(data, {'mu': 1, 'sigma': 1})
    assert (results.x['mu'] == 1)
Esempio n. 11
0
def test_simple_fit():
    """
    Check if fitting Gaussian data works
    """
    from mle import Normal, var
    import numpy as np

    x = var('x', vector=True, observed=True)
    mu = var('mu')
    sigma = var('sigma')

    dist = Normal(x, mu, sigma)
    np.random.seed(42)

    data = np.random.normal(0, 1, 100000)

    try:
        dist.fit({'x': data}, {'mu': 1, 'sigma': 2}, method='BFGS')
    except:
        assert False, 'Fitting generated data failed'
Esempio n. 12
0
def test_linear_regression():
    """
    Check if fitting a linear model works.
    """
    from mle import Normal, var
    import numpy as np

    x = var('x', vector=True, observed=True)
    y = var('y', vector=True, observed=True)

    a = var('a')
    b = var('b')
    sigma = var('sigma')

    model = Normal(y, a * x + b, sigma)
    np.random.seed(42)

    xs = np.linspace(0, 1, 20)
    ys = 0.5 * xs - 0.3 + np.random.normal(0, 0.2, 20)

    results = model.fit({'x': xs, 'y':ys}, {'a': 2, 'b': 1, 'sigma': 1})
    print(results)
Esempio n. 13
0
def test_simple_fit():
    """
    Check if generating/fitting Gaussian data works
    """
    from mle import Normal, var
    import numpy as np

    x = var('x', vector=True, observed=True)
    mu = var('mu')
    sigma = var('sigma')

    dist = Normal(x, mu, sigma)
    np.random.seed(42)
    try:
        data = dist.sample(1e6, {'mu': 0, 'sigma': 1})
    except:
        assert False, 'Generating data failed'
        
    try:
        results = dist.fit(data, {'mu': 1, 'sigma': 2}, method='BFGS')
    except:
        assert False, 'Fitting generated data failed'
Esempio n. 14
0
def test_linear_regression():
    """
    Check if fitting a linear model works.
    """
    from mle import Normal, var
    import numpy as np

    x = var('x', vector=True, observed=True)
    y = var('y', vector=True, observed=True)

    a = var('a')
    b = var('b')
    sigma = var('sigma')

    model = Normal(y, a * x + b, sigma)
    np.random.seed(42)

    xs = np.linspace(0, 1, 20)
    ys = 0.5 * xs - 0.3 + np.random.normal(0, 0.2, 20)

    results = model.fit({'x': xs, 'y': ys}, {'a': 2, 'b': 1, 'sigma': 1})
    print(results)
Esempio n. 15
0
def test_simple_fit():
    """
    Check if generating/fitting Gaussian data works
    """
    from mle import Normal, var
    import numpy as np

    x = var('x', vector=True, observed=True)
    mu = var('mu')
    sigma = var('sigma')

    dist = Normal(x, mu, sigma)
    np.random.seed(42)
    try:
        data = dist.sample(1e6, {'mu': 0, 'sigma': 1})
    except:
        assert False, 'Generating data failed'

    try:
        results = dist.fit(data, {'mu': 1, 'sigma': 2}, method='BFGS')
    except:
        assert False, 'Fitting generated data failed'
Esempio n. 16
0
def sfa_inner(lnrptA, lnmptA, SBInvestRatioA, fundScaleA, ALRatioA, institutionRatioA):
	# Define model
	lnrpt = var('lnrpt', observed=True, vector=True)
	lnmpt = var('lnmpt', observed=True, vector=True)
	SBInvestRatio = var('SBInvestRatio', observed=True, vector=True)
	fundScale = var('fundScale', observed=True, vector=True)
	ALRatio = var('ALRatio', observed=True, vector=True)
	institutionRatio = var('institutionRatio', observed=True, vector=True)

	a1 = var('a1')
	a2 = var('a2')
	a3 = var('a3')
	a4 = var('a4')
	b0 = var('b0')
	b1 = var('b1')
	sigma = var('sigma')

	model = Normal(lnrpt, b1 * lnmpt + (a1 * SBInvestRatio) + (a2 * fundScale) + (a3 * ALRatio) + (a4 * institutionRatio) + b0, sigma)

	# Fit model to data
	result = model.fit({'lnrpt': lnrptA, 'lnmpt': lnmptA, 
		'SBInvestRatio': SBInvestRatioA, 'fundScale': fundScaleA,
		'ALRatio': ALRatioA, 'institutionRatio': institutionRatioA}, 
		{'a1': 1, 'a2': 1, 'a3': 1, 'a4': 1, 'b0': 1, 'b1': 1, 'sigma': 1})
	
	return result.x['b1'];
Esempio n. 17
0
import numpy as np
from mle import var, Normal

# Define model
x = var("x", observed=True, vector=True)
y = var("y", observed=True, vector=True)

a = var("a")
b = var("b")
sigma = var("sigma")

model = Normal(y, a * x + b, sigma)

# Generate data
xs = np.linspace(0, 2, 20)
ys = 0.5 * xs + 0.3 + np.random.normal(0, 0.1, 20)

# Fit model to data
result = model.fit({"x": xs, "y": ys}, {"a": 1, "b": 1, "sigma": 1})
print(result)
Esempio n. 18
0
import numpy as np
from mle import var, Normal

# Define model
x = var('x', observed=True, vector=True)
y = var('y', observed=True, vector=True)

a = var('a')
b = var('b')
sigma = var('sigma')

model = Normal(y, a * x + b, sigma)

# Generate data
xs = np.linspace(0, 2, 20)
ys = 0.5 * xs + 0.3 + np.random.normal(0, 0.1, 20)

# Fit model to data
result = model.fit({'x': xs, 'y': ys}, {'a': 1, 'b': 1, 'sigma': 1})
print(result)
Esempio n. 19
0
from numpy.random import normal
from numpy import linspace, array, append
import matplotlib.pyplot as plt
from scipy.stats import norm
from time import clock
from pandas import DataFrame

from mle import Normal, var, par, Mix2

x = var("x")
mu1 = par("mu1")
sigma1 = par("sigma1")
mu2 = par("mu2")
sigma2 = par("sigma2")
frac = par("frac")

model = Mix2(frac, Normal(x, mu1, sigma1), Normal(x, mu2, sigma2))

N = int(1e5)

data = array(append(normal(3, 1, N), normal(-3, 1, 2 * N)),
             dtype=[("x", float)])

initial = {"frac": 0.5, "mu1": 2, "sigma1": 4, "mu2": 2, "sigma2": 4}

start = clock()
result = model.fit(data, initial, method="Powell")
print("Fit took {:5.1f} s".format(clock() - start))
print(result)

from mle.tests import kolsmi, chisquare
Esempio n. 20
0
def sfa_inner(lnrptA, lnmptA, SBInvestRatioA, fundScaleA, ALRatioA,
              institutionRatioA):
    # Define model
    lnrpt = var('lnrpt', observed=True, vector=True)
    lnmpt = var('lnmpt', observed=True, vector=True)
    SBInvestRatio = var('SBInvestRatio', observed=True, vector=True)
    fundScale = var('fundScale', observed=True, vector=True)
    ALRatio = var('ALRatio', observed=True, vector=True)
    institutionRatio = var('institutionRatio', observed=True, vector=True)

    a1 = var('a1')
    a2 = var('a2')
    a3 = var('a3')
    a4 = var('a4')
    b0 = var('b0')
    b1 = var('b1')
    sigma = var('sigma')

    model = Normal(
        lnrpt, b1 * lnmpt + (a1 * SBInvestRatio) + (a2 * fundScale) +
        (a3 * ALRatio) + (a4 * institutionRatio) + b0, sigma)

    # Fit model to data
    result = model.fit(
        {
            'lnrpt': lnrptA,
            'lnmpt': lnmptA,
            'SBInvestRatio': SBInvestRatioA,
            'fundScale': fundScaleA,
            'ALRatio': ALRatioA,
            'institutionRatio': institutionRatioA
        }, {
            'a1': 1,
            'a2': 1,
            'a3': 1,
            'a4': 1,
            'b0': 1,
            'b1': 1,
            'sigma': 1
        })

    return result.x['b1']