@@ -96,12 +96,13 @@ def _fft_next_fast_len(target):
96
96
target += 1
97
97
98
98
99
- def autocorrelation (x , axis = 0 ):
99
+ def autocorrelation (x , axis = 0 , bias = True ):
100
100
"""
101
101
Computes the autocorrelation of samples at dimension ``axis``.
102
102
103
103
:param numpy.ndarray x: the input array.
104
104
:param int axis: the dimension to calculate autocorrelation.
105
+ :param bias: whether to use a biased estimator.
105
106
:return: autocorrelation of ``x``.
106
107
:rtype: numpy.ndarray
107
108
"""
@@ -127,25 +128,32 @@ def autocorrelation(x, axis=0):
127
128
128
129
# truncate and normalize the result, then transpose back to original shape
129
130
autocorr = autocorr [..., :N ]
130
- autocorr = autocorr / np .arange (N , 0.0 , - 1 )
131
+
132
+ # the unbiased estimator is known to have "wild" tails, due to few samples at longer lags.
133
+ # see Geyer (1992) and Priestley (1981) for a discussion. also note that it is only strictly
134
+ # unbiased when the mean is known, whereas we it estimate from samples here.
135
+ if not bias :
136
+ autocorr = autocorr / np .arange (N , 0.0 , - 1 )
137
+
131
138
with np .errstate (invalid = "ignore" , divide = "ignore" ):
132
139
autocorr = autocorr / autocorr [..., :1 ]
133
140
return np .swapaxes (autocorr , axis , - 1 )
134
141
135
142
136
- def autocovariance (x , axis = 0 ):
143
+ def autocovariance (x , axis = 0 , bias = True ):
137
144
"""
138
145
Computes the autocovariance of samples at dimension ``axis``.
139
146
140
147
:param numpy.ndarray x: the input array.
141
148
:param int axis: the dimension to calculate autocovariance.
149
+ :param bias: whether to use a biased estimator.
142
150
:return: autocovariance of ``x``.
143
151
:rtype: numpy.ndarray
144
152
"""
145
- return autocorrelation (x , axis ) * x .var (axis = axis , keepdims = True )
153
+ return autocorrelation (x , axis , bias ) * x .var (axis = axis , keepdims = True )
146
154
147
155
148
- def effective_sample_size (x ):
156
+ def effective_sample_size (x , bias = True ):
149
157
"""
150
158
Computes effective sample size of input ``x``, where the first dimension of
151
159
``x`` is chain dimension and the second dimension of ``x`` is draw dimension.
@@ -158,6 +166,7 @@ def effective_sample_size(x):
158
166
Stan Development Team
159
167
160
168
:param numpy.ndarray x: the input array.
169
+ :param bias: whether to use a biased estimator of the autocovariance.
161
170
:return: effective sample size of ``x``.
162
171
:rtype: numpy.ndarray
163
172
"""
@@ -166,7 +175,7 @@ def effective_sample_size(x):
166
175
assert x .shape [1 ] >= 2
167
176
168
177
# find autocovariance for each chain at lag k
169
- gamma_k_c = autocovariance (x , axis = 1 )
178
+ gamma_k_c = autocovariance (x , axis = 1 , bias = bias )
170
179
171
180
# find autocorrelation at lag k (from Stan reference)
172
181
var_within , var_estimator = _compute_chain_variance_stats (x )
0 commit comments