def test_evaluate_pdf(): """Testing the evaluation of the pdf.""" # suppose that workers are uniform on [a, b] = [0, 1] a, b = sym.var('a, b') uniform_cdf = (valid_var - a) / (b - a) params = {'a': 0.0, 'b': 1.0} workers = inputs.Input(var=valid_var, cdf=uniform_cdf, bounds=[0.25, 0.75], params=params) # evaluate with scalar input and norm=False actual_pdf = workers.evaluate_pdf(0.5, norm=False) expected_pdf = 1.0 nose.tools.assert_almost_equals(actual_pdf, expected_pdf) # evaluate with array input and norm=False actual_pdf = workers.evaluate_pdf(np.array([0.25, 0.5, 0.75]), norm=False) expected_pdf = np.ones(3) np.testing.assert_almost_equal(actual_pdf, expected_pdf) # evaluate with scalar input and norm=True actual_pdf = workers.evaluate_pdf(0.5, norm=True) expected_pdf = 2.0 nose.tools.assert_almost_equals(actual_pdf, expected_pdf) # evaluate with array input and norm=False actual_pdf = workers.evaluate_pdf(np.array([0.25, 0.5, 0.75]), norm=True) expected_pdf = np.repeat(2.0, 2) np.testing.assert_almost_equal(actual_pdf, expected_pdf)
def setUp(self): """Set up code for test fixtures.""" # define some workers skill x, a, b = sym.var('x, a, b') skill_cdf = (x - a) / (b - a) skill_params = {'a': 1.0, 'b': 2.0} skill_bounds = [skill_params['a'], skill_params['b']] workers = inputs.Input(var=x, cdf=skill_cdf, params=skill_params, bounds=skill_bounds, ) # define some firms y = sym.var('y') productivity_cdf = (y - a) / (b - a) productivity_params = skill_params productivity_bounds = skill_bounds firms = inputs.Input(var=y, cdf=productivity_cdf, params=productivity_params, bounds=productivity_bounds, ) # define symbolic expression for CES between x and y x, y, omega_A, sigma_A = sym.var('x, y, omega_A, sigma_A') A = ((omega_A * x**((sigma_A - 1) / sigma_A) + (1 - omega_A) * y**((sigma_A - 1) / sigma_A))**(sigma_A / (sigma_A - 1))) # define symbolic expression for Cobb-Douglas between l and r l, r, omega_B, sigma_B = sym.var('l, r, omega_A, sigma_A') B = l**omega_B * r**(1 - omega_B) # multiplicative separability! F_params = {'omega_A': 0.5, 'omega_B': 0.5, 'sigma_A': 0.5, 'sigma_B': 1.0} F = A * B self.model = models.Model(assortativity='positive', workers=workers, firms=firms, production=F, params=F_params) self.solver = shooting.ShootingSolver(model=self.model)
def test_validate_var(): """Testing validation of var attribute.""" # valid var must be a sym.Symbol invalid_var = 'x' with nose.tools.assert_raises(AttributeError): inputs.Input(var=invalid_var, cdf=valid_cdf, bounds=valid_bounds, params=valid_params)
def test_validate_params(): """Testing validation of bounds parameter.""" # valid parameters must be a dict invalid_params = (1.0, 2.0) with nose.tools.assert_raises(AttributeError): inputs.Input(var=valid_var, cdf=valid_cdf, bounds=valid_bounds, params=invalid_params)
def test_validate_cdf(): """Testing validation of cdf attribute.""" def invalid_cdf(x, mu, sigma): """Valid cdf must return a SymPy expression.""" return stats.lognorm.cdf(x, sigma, scale=np.exp(mu)) with nose.tools.assert_raises(AttributeError): inputs.Input(var=valid_var, cdf=invalid_cdf, bounds=valid_bounds, params=valid_params)
def test_validate_upper(): """Testing validation of upper attribute.""" # upper should be a float invalid_upper = 14 with nose.tools.assert_raises(AttributeError): workers = inputs.Input(var=valid_var, cdf=valid_cdf, bounds=valid_bounds, params=valid_params) workers.upper = invalid_upper
def test_evaluate_cdf(): """Testing the evaluation of the cdf.""" # suppose that workers are uniform on [a, b] = [0, 1] a, b = sym.var('a, b') uniform_cdf = (valid_var - a) / (b - a) params = {'a': 0.0, 'b': 1.0} workers = inputs.Input(var=valid_var, cdf=uniform_cdf, bounds=[0.0, 1.0], params=params) # evaluate with scalar input actual_cdf = workers.evaluate_cdf(0.5) expected_cdf = 0.5 nose.tools.assert_almost_equals(actual_cdf, expected_cdf) # evaluate with array input actual_cdf = workers.evaluate_cdf(np.array([0.0, 0.5, 1.0])) expected_cdf = actual_cdf np.testing.assert_almost_equal(actual_cdf, expected_cdf)
import inputs import models # define endogenous variables mu, theta = sym.var('mu, theta') # define some workers skill x, mu1, sigma1 = sym.var('x, mu1, sigma1') skill_cdf = 0.5 + 0.5 * sym.erf((sym.log(x) - mu1) / sym.sqrt(2 * sigma1**2)) skill_params = {'mu1': 0.0, 'sigma1': 1.0} skill_bounds = [1e-3, 5e1] workers = inputs.Input( var=x, cdf=skill_cdf, params=skill_params, bounds=skill_bounds, ) # define some firms y, mu2, sigma2 = sym.var('y, mu2, sigma2') productivity_cdf = 0.5 + 0.5 * sym.erf( (sym.log(y) - mu2) / sym.sqrt(2 * sigma2**2)) productivity_params = {'mu2': 0.0, 'sigma2': 1.0} productivity_bounds = [1e-3, 5e1] firms = inputs.Input( var=y, cdf=productivity_cdf, params=productivity_params, bounds=productivity_bounds,