forked from econ-ark/HARK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HARKestimation.py
148 lines (125 loc) · 5.8 KB
/
HARKestimation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
'''
Functions for estimating structural models, including optimization methods
and bootstrapping tools.
'''
# The following libraries are part of the standard python distribution
from __future__ import division # Use new division function
import numpy as np # Numerical Python
from time import time # Used to time execution
from copy import deepcopy # For replicating complex objects
from scipy.optimize import fmin, fmin_powell # Minimizers
from HARKutilities import warnings # Import modified "warnings" library
def minimizeNelderMead(objectiveFunction, parameter_guess, verbose=False, **kwargs):
'''
Minimizes the objective function using the Nelder-Mead simplex algorithm,
starting from an initial parameter guess.
Parameters
----------
objectiveFunction : function
The function to be minimized. It should take only a single argument, which
should be a list representing the parameters to be estimated.
parameter_guess : [float]
A starting point for the Nelder-Mead algorithm, which must be a valid
input for objectiveFunction.
verbose : boolean
A flag for the amount of output to print.
Returns
-------
xopt : [float]
The values that minimize objectiveFunction.
'''
# Execute the minimization, starting from the given parameter guess
t0 = time() # Time the process
OUTPUT = fmin(objectiveFunction, parameter_guess, full_output=1, maxiter=1000, disp=verbose, **kwargs)
t1 = time()
# Extract values from optimization output:
xopt = OUTPUT[0] # Parameters that minimize function.
fopt = OUTPUT[1] # Value of function at minimum: ``fopt = func(xopt)``.
optiter = OUTPUT[2] # Number of iterations performed.
funcalls = OUTPUT[3] # Number of function calls made.
warnflag = OUTPUT[4] # warnflag : int
# 1 : Maximum number of function evaluations made.
# 2 : Maximum number of iterations reached.
# Check that optimization succeeded:
if warnflag != 0:
warnings.warn("Minimization failed! xopt=" + str(xopt) + ', fopt=' + str(fopt) +
', optiter=' + str(optiter) +', funcalls=' + str(funcalls) +
', warnflag=' + str(warnflag))
# Display and return the results:
if verbose:
print("Time to estimate is " + str(t1-t0) + " seconds.")
return xopt
def minimizePowell(objectiveFunction, parameter_guess, verbose=False):
'''
Minimizes the objective function using a derivative-free Powell algorithm,
starting from an initial parameter guess.
Parameters
----------
objectiveFunction : function
The function to be minimized. It should take only a single argument, which
should be a list representing the parameters to be estimated.
parameter_guess : [float]
A starting point for the Powell algorithm, which must be a valid
input for objectiveFunction.
verbose : boolean
A flag for the amount of output to print.
Returns
-------
xopt : [float]
The values that minimize objectiveFunction.
'''
# Execute the minimization, starting from the given parameter guess
t0 = time() # Time the process
OUTPUT = fmin_powell(objectiveFunction, parameter_guess, full_output=1, maxiter=1000, disp=verbose)
t1 = time()
# Extract values from optimization output:
xopt = OUTPUT[0] # Parameters that minimize function.
fopt = OUTPUT[1] # Value of function at minimum: ``fopt = func(xopt)``.
direc = OUTPUT[2]
optiter = OUTPUT[3] # Number of iterations performed.
funcalls = OUTPUT[4] # Number of function calls made.
warnflag = OUTPUT[5] # warnflag : int
# 1 : Maximum number of function evaluations made.
# 2 : Maximum number of iterations reached.
# Check that optimization succeeded:
if warnflag != 0:
warnings.warn("Minimization failed! xopt=" + str(xopt) + ', fopt=' + str(fopt) + ', direc=' + str(direc) + ', optiter=' + str(optiter) +', funcalls=' + str(funcalls) +', warnflag=' + str(warnflag))
# Display and return the results:
if verbose:
print("Time to estimate is " + str(t1-t0) + " seconds.")
return xopt
def bootstrapSampleFromData(data,weights=None,seed=0):
'''
Samples rows from the input array of data, generating a new data array with
an equal number of rows (records). Rows are drawn with equal probability
by default, but probabilities can be specified with weights (must sum to 1).
Parameters
----------
data : np.array
An array of data, with each row representing a record.
weights : np.array
A weighting array with length equal to data.shape[0].
seed : int
A seed for the random number generator.
Returns
-------
new_data : np.array
A resampled version of input data.
'''
# Set up the random number generator
RNG = np.random.RandomState(seed)
N = data.shape[0]
# Set up weights
if weights is not None:
cutoffs = np.cumsum(weights)
else:
cutoffs = np.linspace(0,1,N)
# Draw random indices
indices = np.searchsorted(cutoffs,RNG.uniform(size=N))
# Create a bootstrapped sample
new_data = deepcopy(data[indices,])
return new_data
if __name__ == '__main__':
print("Sorry, HARKestimation doesn't actually do anything on its own.")
print("To see some examples of its functions in actions, check out an application")
print("like /SolvingMicroDSOPs/StructEstimation or /cstwMPC/cstwMPC.")