mirror of
https://github.com/saymrwulf/prophet.git
synced 2026-11-16 08:38:30 +00:00
Merge branch 'loulo1-patch-1'
This commit is contained in:
commit
9f0ce77be5
1 changed files with 31 additions and 19 deletions
|
|
@ -87,25 +87,28 @@ class CmdStanPyBackend(IStanBackend):
|
||||||
|
|
||||||
def fit(self, stan_init, stan_data, **kwargs):
|
def fit(self, stan_init, stan_data, **kwargs):
|
||||||
(stan_init, stan_data) = self.prepare_data(stan_init, stan_data)
|
(stan_init, stan_data) = self.prepare_data(stan_init, stan_data)
|
||||||
if 'algorithm' not in kwargs:
|
|
||||||
kwargs['algorithm'] = 'Newton' if stan_data['T'] < 100 else 'LBFGS'
|
if 'inits' not in kwargs and 'init' in kwargs:
|
||||||
iterations = int(1e4)
|
kwargs['inits'] = self.prepare_data(kwargs['init'], stan_data)[0]
|
||||||
|
|
||||||
|
args = dict(
|
||||||
|
data=stan_data,
|
||||||
|
inits=stan_init,
|
||||||
|
algorithm='Newton' if stan_data['T'] < 100 else 'LBFGS',
|
||||||
|
iter=int(1e4),
|
||||||
|
)
|
||||||
|
args.update(kwargs)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.stan_fit = self.model.optimize(data=stan_data,
|
self.stan_fit = self.model.optimize(**args)
|
||||||
inits=stan_init,
|
|
||||||
iter=iterations,
|
|
||||||
**kwargs)
|
|
||||||
except RuntimeError as e:
|
except RuntimeError as e:
|
||||||
# Fall back on Newton
|
# Fall back on Newton
|
||||||
if self.newton_fallback and kwargs['algorithm'] != 'Newton':
|
if self.newton_fallback and args['algorithm'] != 'Newton':
|
||||||
logger.warning(
|
logger.warning(
|
||||||
'Optimization terminated abnormally. Falling back to Newton.'
|
'Optimization terminated abnormally. Falling back to Newton.'
|
||||||
)
|
)
|
||||||
kwargs['algorithm'] = 'Newton'
|
args['algorithm'] = 'Newton'
|
||||||
self.stan_fit = self.model.optimize(data=stan_data,
|
self.stan_fit = self.model.optimize(**args)
|
||||||
inits=stan_init,
|
|
||||||
iter=iterations,
|
|
||||||
**kwargs)
|
|
||||||
else:
|
else:
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
|
|
@ -118,16 +121,25 @@ class CmdStanPyBackend(IStanBackend):
|
||||||
def sampling(self, stan_init, stan_data, samples, **kwargs) -> dict:
|
def sampling(self, stan_init, stan_data, samples, **kwargs) -> dict:
|
||||||
(stan_init, stan_data) = self.prepare_data(stan_init, stan_data)
|
(stan_init, stan_data) = self.prepare_data(stan_init, stan_data)
|
||||||
|
|
||||||
|
if 'inits' not in kwargs and 'init' in kwargs:
|
||||||
|
kwargs['inits'] = self.prepare_data(kwargs['init'], stan_data)[0]
|
||||||
|
|
||||||
|
args = dict(
|
||||||
|
data=stan_data,
|
||||||
|
inits=stan_init,
|
||||||
|
algorithm='Newton' if stan_data['T'] < 100 else 'LBFGS',
|
||||||
|
)
|
||||||
|
|
||||||
if 'chains' not in kwargs:
|
if 'chains' not in kwargs:
|
||||||
kwargs['chains'] = 4
|
kwargs['chains'] = 4
|
||||||
iter_half = samples // 2
|
iter_half = samples // 2
|
||||||
|
kwargs['iter_sampling'] = iter_half
|
||||||
if 'iter_warmup' not in kwargs:
|
if 'iter_warmup' not in kwargs:
|
||||||
kwargs['iter_warmup'] = iter_half
|
kwargs['iter_warmup'] = iter_half
|
||||||
|
|
||||||
self.stan_fit = self.model.sample(data=stan_data,
|
args.update(kwargs)
|
||||||
inits=stan_init,
|
|
||||||
iter_sampling=iter_half,
|
self.stan_fit = self.model.sample(**args)
|
||||||
**kwargs)
|
|
||||||
res = self.stan_fit.draws()
|
res = self.stan_fit.draws()
|
||||||
(samples, c, columns) = res.shape
|
(samples, c, columns) = res.shape
|
||||||
res = res.reshape((samples * c, columns))
|
res = res.reshape((samples * c, columns))
|
||||||
|
|
@ -166,7 +178,7 @@ class CmdStanPyBackend(IStanBackend):
|
||||||
'm': init['m'],
|
'm': init['m'],
|
||||||
'delta': init['delta'].tolist(),
|
'delta': init['delta'].tolist(),
|
||||||
'beta': init['beta'].tolist(),
|
'beta': init['beta'].tolist(),
|
||||||
'sigma_obs': 1
|
'sigma_obs': init['sigma_obs']
|
||||||
}
|
}
|
||||||
return (cmdstanpy_init, cmdstanpy_data)
|
return (cmdstanpy_init, cmdstanpy_data)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue