How to use the cmdstanpy.cmdstan_args.OptimizeArgs function in cmdstanpy

To help you get started, we’ve selected a few cmdstanpy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github stan-dev / cmdstanpy / test / test_cmdstan_args.py View on Github external
def test_args_algorithm(self):
        args = OptimizeArgs(algorithm='non-valid_algorithm')
        with self.assertRaises(ValueError):
            args.validate()
        args = OptimizeArgs(algorithm='Newton')
        args.validate()
        cmd = args.compose(None, cmd=['output'])
        self.assertIn('algorithm=newton', ' '.join(cmd))
github stan-dev / cmdstanpy / test / test_optimize.py View on Github external
def test_set_mle_attrs(self):
        stan = os.path.join(DATAFILES_PATH, 'optimize', 'rosenbrock.stan')
        model = CmdStanModel(stan_file=stan)
        no_data = {}
        args = OptimizeArgs(algorithm='Newton')
        cmdstan_args = CmdStanArgs(
            model_name=model.name,
            model_exe=model.exe_file,
            chain_ids=None,
            data=no_data,
            method_args=args,
        )
        runset = RunSet(args=cmdstan_args, chains=1)
        mle = CmdStanMLE(runset)
        self.assertIn('CmdStanMLE: model=rosenbrock', mle.__repr__())
        self.assertIn('method=optimize', mle.__repr__())

        self.assertEqual(mle._column_names, ())
        self.assertEqual(mle._mle, {})

        output = os.path.join(DATAFILES_PATH, 'optimize', 'rosenbrock_mle.csv')
github stan-dev / cmdstanpy / test / test_cmdstan_args.py View on Github external
def test_args_algorithm_init_alpha(self):
        args = OptimizeArgs(init_alpha=2e-4)
        args.validate()
        cmd = args.compose(None, cmd=['output'])

        self.assertIn('init_alpha=0.0002', ' '.join(cmd))
        args = OptimizeArgs(init_alpha=-1.0)
        with self.assertRaises(ValueError):
            args.validate()
        args = OptimizeArgs(init_alpha=1.0, algorithm='Newton')
        with self.assertRaises(ValueError):
            args.validate()
github stan-dev / cmdstanpy / test / test_cmdstan_args.py View on Github external
def test_args_algorithm_init_alpha(self):
        args = OptimizeArgs(init_alpha=2e-4)
        args.validate()
        cmd = args.compose(None, cmd=['output'])

        self.assertIn('init_alpha=0.0002', ' '.join(cmd))
        args = OptimizeArgs(init_alpha=-1.0)
        with self.assertRaises(ValueError):
            args.validate()
        args = OptimizeArgs(init_alpha=1.0, algorithm='Newton')
        with self.assertRaises(ValueError):
            args.validate()
github stan-dev / cmdstanpy / test / test_cmdstan_args.py View on Github external
def test_args_algorithm_iter(self):
        args = OptimizeArgs(iter=400)
        args.validate()
        cmd = args.compose(None, cmd=['output'])
        self.assertIn('iter=400', ' '.join(cmd))
        args = OptimizeArgs(iter=-1)
        with self.assertRaises(ValueError):
            args.validate()
github stan-dev / cmdstanpy / test / test_cmdstan_args.py View on Github external
def test_args_algorithm_init_alpha(self):
        args = OptimizeArgs(init_alpha=2e-4)
        args.validate()
        cmd = args.compose(None, cmd=['output'])

        self.assertIn('init_alpha=0.0002', ' '.join(cmd))
        args = OptimizeArgs(init_alpha=-1.0)
        with self.assertRaises(ValueError):
            args.validate()
        args = OptimizeArgs(init_alpha=1.0, algorithm='Newton')
        with self.assertRaises(ValueError):
            args.validate()
github stan-dev / cmdstanpy / test / test_cmdstan_args.py View on Github external
def test_args_algorithm(self):
        args = OptimizeArgs(algorithm='non-valid_algorithm')
        with self.assertRaises(ValueError):
            args.validate()
        args = OptimizeArgs(algorithm='Newton')
        args.validate()
        cmd = args.compose(None, cmd=['output'])
        self.assertIn('algorithm=newton', ' '.join(cmd))
github stan-dev / cmdstanpy / test / test_cmdstan_args.py View on Github external
def test_args_algorithm_iter(self):
        args = OptimizeArgs(iter=400)
        args.validate()
        cmd = args.compose(None, cmd=['output'])
        self.assertIn('iter=400', ' '.join(cmd))
        args = OptimizeArgs(iter=-1)
        with self.assertRaises(ValueError):
            args.validate()
github stan-dev / cmdstanpy / cmdstanpy / model.py View on Github external
files are saved.  If unspecified, files will be written to a
            temporary directory which is deleted upon session exit.

        :param save_diagnostics: Whether or not to save diagnostics. If True,
            csv output files are written to `-diagnostic-0.csv.`,
            where `` is set with `csv_basename`.

        :param algorithm: Algorithm to use. One of: "BFGS", "LBFGS", "Newton"

        :param init_alpha: Line search step size for first iteration

        :param iter: Total number of iterations

        :return: CmdStanMLE object
        """
        optimize_args = OptimizeArgs(
            algorithm=algorithm, init_alpha=init_alpha, iter=iter
        )

        with MaybeDictToFilePath(data, inits) as (_data, _inits):
            args = CmdStanArgs(
                self._name,
                self._exe_file,
                chain_ids=None,
                data=_data,
                seed=seed,
                inits=_inits,
                output_dir=output_dir,
                save_diagnostics=save_diagnostics,
                method_args=optimize_args,
            )
github stan-dev / cmdstanpy / cmdstanpy / cmdstan_args.py View on Github external
len(self.seed), len(self.chain_ids)
                        )
                    )
                for i in range(len(self.seed)):
                    if self.seed[i] < 0 or self.seed[i] > 2 ** 32 - 1:
                        raise ValueError(
                            'seed must be an integer value'
                            ' between 0 and 2**32-1,'
                            ' found {}'.format(self.seed[i])
                        )

        if isinstance(self.data, str):
            if not os.path.exists(self.data):
                raise ValueError('no such file {}'.format(self.data))
        elif self.data is None:
            if isinstance(self.method_args, OptimizeArgs):
                raise ValueError('data must be set when optimizing')
        elif not isinstance(self.data, (str, dict)):
            raise ValueError('data must be string or dict')

        if self.inits is not None:
            if isinstance(self.inits, (Integral, Real)):
                if self.inits < 0:
                    raise ValueError(
                        'inits must be > 0, found {}'.format(self.inits)
                    )
            elif isinstance(self.inits, str):
                if not os.path.exists(self.inits):
                    raise ValueError('no such file {}'.format(self.inits))
            elif isinstance(self.inits, list):
                if self.chain_ids is None:
                    raise ValueError(