Skip to content

Commit

Permalink
Merge pull request #750 from Mv77/fixRandomDraws
Browse files Browse the repository at this point in the history
Change in RNG in prior PR wasn't implemented in a few places; now fixed.
  • Loading branch information
mnwhite authored Jul 3, 2020
2 parents 0bd0595 + dddb1a3 commit d302243
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 13 deletions.
7 changes: 5 additions & 2 deletions HARK/ConsumptionSaving/ConsIndShockModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2496,7 +2496,8 @@ def constructLognormalIncomeProcessUnemployment(self):
ShkPrbsRet = np.array([1.0])
IncomeDstnRet = DiscreteDistribution(ShkPrbsRet,
[PermShkValsRet,
TranShkValsRet])
TranShkValsRet],
seed=self.RNG.randint(0, 2**31-1))

# Loop to fill in the list of IncomeDstn random variables.
for t in range(T_cycle): # Iterate over all periods, counting forward
Expand All @@ -2518,7 +2519,9 @@ def constructLognormalIncomeProcessUnemployment(self):
).approx(PermShkCount, tail_N=0)
### REPLACE
###REPLACE
IncomeDstn.append(combineIndepDstns(PermShkDstn_t,TranShkDstn_t)) # mix the independent distributions
IncomeDstn.append(combineIndepDstns(PermShkDstn_t,
TranShkDstn_t,
seed = self.RNG.randint(0, 2**31-1))) # mix the independent distributions
PermShkDstn.append(PermShkDstn_t)
TranShkDstn.append(TranShkDstn_t)
return IncomeDstn, PermShkDstn, TranShkDstn
Expand Down
4 changes: 2 additions & 2 deletions HARK/ConsumptionSaving/ConsPortfolioModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def getRisky(self):

mu = np.log(RiskyAvg / (np.sqrt(1. + RiskyVar / RiskyAvgSqrd)))
sigma = np.sqrt(np.log(1. + RiskyVar / RiskyAvgSqrd))
self.RiskyNow = Lognormal(mu, sigma).draw(1, seed=self.RNG.randint(0, 2**31-1))
self.RiskyNow = Lognormal(mu, sigma, seed=self.RNG.randint(0, 2**31-1)).draw(1)


def getAdjust(self):
Expand All @@ -370,7 +370,7 @@ def getAdjust(self):
-------
None
'''
self.AdjustNow = Bernoulli(self.AdjustPrb).draw(self.AgentCount, seed=self.RNG.randint(0, 2**31-1))
self.AdjustNow = Bernoulli(self.AdjustPrb, seed=self.RNG.randint(0, 2**31-1)).draw(self.AgentCount)


def getRfree(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,4 @@ def test_simulation(self):

self.assertAlmostEqual(
np.mean(self.agent.history['mLvlNow']),
1.2063544810887799)
1.2043946738813716)
10 changes: 4 additions & 6 deletions HARK/ConsumptionSaving/tests/test_IndShockConsumerType.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ def test_getShocks(self):
self.agent.getShocks()

self.assertEqual(self.agent.PermShkNow[0],
0.9686755529883603)
1.0427376294215103)
self.assertEqual(self.agent.PermShkNow[1],
1.0050166461586711)
0.9278094171517413)
self.assertEqual(self.agent.TranShkNow[0],
1.2093790234554653)
0.881761797501595)

def test_ConsIndShockSolverBasic(self):
LifecycleExample = IndShockConsumerType(
Expand Down Expand Up @@ -102,13 +102,11 @@ def test_simulated_values(self):
self.agent.initializeSim()
self.agent.simulate()

print(self.agent.aLvlNow)

self.assertAlmostEqual(self.agent.MPCnow[1],
0.5711503906043797)

self.assertAlmostEqual(self.agent.aLvlNow[1],
0.21251164208206255)
0.18438326264597635)


class testBufferStock(unittest.TestCase):
Expand Down
4 changes: 2 additions & 2 deletions HARK/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ def addDiscreteOutcome(distribution, x, p, sort = False):

return DiscreteDistribution(pmf,X)

def combineIndepDstns(*distributions):
def 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
Expand Down Expand Up @@ -897,4 +897,4 @@ def combineIndepDstns(*distributions):
P_out = np.prod(np.array(P_temp),axis=0)

assert np.isclose(np.sum(P_out),1),'Probabilities do not sum to 1!'
return DiscreteDistribution(P_out, X_out)
return DiscreteDistribution(P_out, X_out, seed = seed)

0 comments on commit d302243

Please sign in to comment.