Distribution

class HARK.distribution.Bernoulli(p=0.5, seed=0)

Bases: HARK.distribution.Distribution

A Bernoulli distribution.

draw(N)

Generates arrays of booleans drawn from a simple Bernoulli distribution. The input p can be a float or a list-like of floats; its length T determines the number of entries in the output. The t-th entry of the output is an array of N booleans which are True with probability p[t] and False otherwise.

Parameters:N (int) – Number of draws in each row.
Returns:
  • draws (np.array or [np.array]) – T-length list of arrays of Bernoulli draws each of size N, or a single
  • array of size N (if sigma is a scalar).
p = None
class HARK.distribution.DiscreteDistribution(pmf, X, seed=0)

Bases: HARK.distribution.Distribution

A representation of a discrete probability distribution.

X = None
dim()
drawDiscrete(N, X=None, exact_match=False)

Simulates N draws from a discrete distribution with probabilities P and outcomes X.

Parameters:
  • N (int) – Number of draws to simulate.
  • X (None, int, or np.array) – If None, then use this distribution’s X for point values. If an int, then the index of X for the point values. If an np.array, use the array for the point values.
  • exact_match (boolean) – Whether the draws should “exactly” match the discrete distribution (as closely as possible given finite draws). When True, returned draws are a random permutation of the N-length list that best fits the discrete distribution. When False (default), each draw is independent from the others and the result could deviate from the input.
Returns:

draws – An array of draws from the discrete distribution; each element is a value in X.

Return type:

np.array

draw_events(n)

Draws N ‘events’ from the distribution PMF. These events are indices into X.

pmf = None
class HARK.distribution.Distribution(seed=0)

Bases: object

reset()

Reset the random number generator of this distribution.

class HARK.distribution.Lognormal(mu=0.0, sigma=1.0, seed=0)

Bases: HARK.distribution.Distribution

A Lognormal distribution

approx(N, tail_N=0, tail_bound=None, tail_order=2.718281828459045)

Construct a discrete approximation to a lognormal distribution with underlying normal distribution N(mu,sigma). Makes an equiprobable distribution by default, but user can optionally request augmented tails with exponentially sized point masses. This can improve solution accuracy in some models.

Parameters:
  • N (int) – Number of discrete points in the “main part” of the approximation.
  • tail_N (int) – Number of points in each “tail part” of the approximation; 0 = no tail.
  • tail_bound ([float]) – CDF boundaries of the tails vs main portion; tail_bound[0] is the lower tail bound, tail_bound[1] is the upper tail bound. Inoperative when tail_N = 0. Can make “one tailed” approximations with 0.0 or 1.0.
  • tail_order (float) – Factor by which consecutive point masses in a “tail part” differ in probability. Should be >= 1 for sensible spacing.
Returns:

d – Probability associated with each point in array of discrete points for discrete probability mass function.

Return type:

DiscreteDistribution

draw(N)

Generate arrays of lognormal draws. The sigma input can be a number or list-like. If a number, output is a length N array of draws from the lognormal distribution with standard deviation sigma. If a list, output is a length T list whose t-th entry is a length N array of draws from the lognormal with standard deviation sigma[t].

Parameters:
  • N (int) – Number of draws in each row.
  • Returns
  • ------------
  • draws (np.array or [np.array]) – T-length list of arrays of mean one lognormal draws each of size N, or a single array of size N (if sigma is a scalar).
mu = None
sigma = None
class HARK.distribution.MeanOneLogNormal(sigma=1.0, seed=0)

Bases: HARK.distribution.Lognormal

class HARK.distribution.Normal(mu=0.0, sigma=1.0, seed=0)

Bases: HARK.distribution.Distribution

A Normal distribution.

approx(N)

Returns a discrete approximation of this distribution.

draw(N, seed=0)

Generate arrays of normal draws. The mu and sigma inputs can be numbers or list-likes. If a number, output is a length N array of draws from the normal distribution with mean mu and standard deviation sigma. If a list, output is a length T list whose t-th entry is a length N array with draws from the normal distribution with mean mu[t] and standard deviation sigma[t].

Parameters:N (int) – Number of draws in each row.
Returns:draws – T-length list of arrays of normal draws each of size N, or a single array of size N (if sigma is a scalar).
Return type:np.array or [np.array]
mu = None
sigma = None
class HARK.distribution.Uniform(bot=0.0, top=1.0, seed=0)

Bases: HARK.distribution.Distribution

A Uniform distribution.

approx(N)

Makes a discrete approximation to this uniform distribution.

Parameters:N (int) – The number of points in the discrete approximation
Returns:d – Probability associated with each point in array of discrete points for discrete probability mass function.
Return type:DiscreteDistribution
bot = None
draw(N)

Generate arrays of uniform draws. The bot and top inputs can be numbers or list-likes. If a number, output is a length N array of draws from the uniform distribution on [bot,top]. If a list, output is a length T list whose t-th entry is a length N array with draws from the uniform distribution on [bot[t],top[t]].

Parameters:N (int) – Number of draws in each row.
Returns:draws – T-length list of arrays of uniform draws each of size N, or a single array of size N (if sigma is a scalar).
Return type:np.array or [np.array]
top = None
class HARK.distribution.Weibull(scale=1.0, shape=1.0, seed=0)

Bases: HARK.distribution.Distribution

A Weibull distribution

draw(N)

Generate arrays of Weibull draws. The scale and shape inputs can be numbers or list-likes. If a number, output is a length N array of draws from the Weibull distribution with the given scale and shape. If a list, output is a length T list whose t-th entry is a length N array with draws from the Weibull distribution with scale scale[t] and shape shape[t].

Note: When shape=1, the Weibull distribution is simply the exponential dist.

Mean: scale*Gamma(1 + 1/shape)

Parameters:
  • N (int) – Number of draws in each row.
  • Returns
  • ------------
  • draws (np.array or [np.array]) – T-length list of arrays of Weibull draws each of size N, or a single array of size N (if sigma is a scalar).
scale = None
shape = None
HARK.distribution.addDiscreteOutcome(distribution, x, p, sort=False)

Adds a discrete outcome of x with probability p to an existing distribution, holding constant the relative probabilities of other outcomes.

Parameters:
  • distribution ([np.array]) – Two element list containing a list of probabilities and a list of outcomes.
  • x (float) – The new value to be added to the distribution.
  • p (float) – The probability of the discrete outcome x occuring.
Returns:

d – Probability associated with each point in array of discrete points for discrete probability mass function.

Return type:

DiscreteDistribution

HARK.distribution.addDiscreteOutcomeConstantMean(distribution, x, p, sort=False)

Adds a discrete outcome of x with probability p to an existing distribution, holding constant the relative probabilities of other outcomes and overall mean.

Parameters:
  • distribution ([np.array]) – Two element list containing a list of probabilities and a list of outcomes.
  • x (float) – The new value to be added to the distribution.
  • p (float) – The probability of the discrete outcome x occuring.
  • sort (bool) – Whether or not to sort X before returning it
Returns:

d – Probability associated with each point in array of discrete points for discrete probability mass function.

Return type:

DiscreteDistribution

HARK.distribution.approxBeta(N, a=1.0, b=1.0)

Calculate a discrete approximation to the beta distribution. May be quite slow, as it uses a rudimentary numeric integration method to generate the discrete approximation.

Parameters:
  • N (int) – Size of discrete space vector to be returned.
  • a (float) – First shape parameter (sometimes called alpha).
  • b (float) – Second shape parameter (sometimes called beta).
Returns:

d – Probability associated with each point in array of discrete points for discrete probability mass function.

Return type:

DiscreteDistribution

HARK.distribution.approxLognormalGaussHermite(N, mu=0.0, sigma=1.0, seed=0)
HARK.distribution.calcLognormalStyleParsFromNormalPars(muNormal, stdNormal)
HARK.distribution.calcNormalStyleParsFromLognormalPars(avgLognormal, stdLognormal)
HARK.distribution.combineIndepDstns(*distributions, seed=0)

Given n lists (or tuples) whose elements represent n independent, discrete probability spaces (probabilities and values), construct a joint pmf over all combinations of these independent points. Can take multivariate discrete distributions as inputs.

Parameters:distributions ([np.array]) – Arbitrary number of distributions (pmfs). Each pmf is a list or tuple. For each pmf, the first vector is probabilities and all subsequent vectors are values. For each pmf, this should be true: len(X_pmf[0]) == len(X_pmf[j]) for j in range(1,len(distributions))
Returns:
  • A DiscreteDistribution, consisting of
  • P_out (np.array) – Probability associated with each point in X_out.
  • X_out (np.array (as many as in *distributions)) – Discrete points for the joint discrete probability mass function.
HARK.distribution.makeMarkovApproxToNormal(x_grid, mu, sigma, K=351, bound=3.5)

Creates an approximation to a normal distribution with mean mu and standard deviation sigma, returning a stochastic vector called p_vec, corresponding to values in x_grid. If a RV is distributed x~N(mu,sigma), then the expectation of a continuous function f() is E[f(x)] = numpy.dot(p_vec,f(x_grid)).

Parameters:
  • x_grid (numpy.array) – A sorted 1D array of floats representing discrete values that a normally distributed RV could take on.
  • mu (float) – Mean of the normal distribution to be approximated.
  • sigma (float) – Standard deviation of the normal distribution to be approximated.
  • K (int) – Number of points in the normal distribution to sample.
  • bound (float) – Truncation bound of the normal distribution, as +/- bound*sigma.
Returns:

p_vec – A stochastic vector with probability weights for each x in x_grid.

Return type:

numpy.array

HARK.distribution.makeMarkovApproxToNormalByMonteCarlo(x_grid, mu, sigma, N_draws=10000)

Creates an approximation to a normal distribution with mean mu and standard deviation sigma, by Monte Carlo. Returns a stochastic vector called p_vec, corresponding to values in x_grid. If a RV is distributed x~N(mu,sigma), then the expectation of a continuous function f() is E[f(x)] = numpy.dot(p_vec,f(x_grid)).

Parameters:
  • x_grid (numpy.array) – A sorted 1D array of floats representing discrete values that a normally distributed RV could take on.
  • mu (float) – Mean of the normal distribution to be approximated.
  • sigma (float) – Standard deviation of the normal distribution to be approximated.
  • N_draws (int) – Number of draws to use in Monte Carlo.
Returns:

p_vec – A stochastic vector with probability weights for each x in x_grid.

Return type:

numpy.array

HARK.distribution.makeTauchenAR1(N, sigma=1.0, rho=0.9, bound=3.0)

Function to return a discretized version of an AR1 process. See http://www.fperri.net/TEACHING/macrotheory08/numerical.pdf for details

Parameters:
  • N (int) – Size of discretized grid
  • sigma (float) – Standard deviation of the error term
  • rho (float) – AR1 coefficient
  • bound (float) – The highest (lowest) grid point will be bound (-bound) multiplied by the unconditional standard deviation of the process
Returns:

  • y (np.array) – Grid points on which the discretized process takes values
  • trans_matrix (np.array) – Markov transition array for the discretized process