Skip to content

Commit

Permalink
Merge pull request #570 from stan-dev/bugfix/issue-530-no-model-param…
Browse files Browse the repository at this point in the history
…eters

Fixes #530. Bugfix/issue 530 no model parameters
  • Loading branch information
syclik committed Mar 8, 2014
2 parents 47e9541 + f1373d2 commit a8fd956
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/docs/stan-reference/commands.tex
Original file line number Diff line number Diff line change
Expand Up @@ -1319,7 +1319,7 @@ \subsubsection{Fixed Parameter Sampler}
The fixed parameter sampler generates a new sample without changing
the current state of the Markov chain; only generated quantities may
change. This can be useful when, for example, trying to generate pseudo-data
using the generated quantities block.
using the generated quantities block. If the parameters block is empty (no parameters) then using {\tt algorithm=fixed\_param} is mandatory.
%
\begin{description}
\hierlongcmd{\indentarrow\indentarrow\indentarrow}{\farg{\bfseries fixed\_param}}
Expand Down
9 changes: 8 additions & 1 deletion src/stan/common/command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ namespace stan {
//////////////////////////////////////////////////

Model model(data_var_context, &std::cout);

Eigen::VectorXd cont_params = Eigen::VectorXd::Zero(model.num_params_r());

if (output_stream) {
Expand Down Expand Up @@ -603,6 +603,13 @@ namespace stan {
parser.arg("method")->arg("sample")->arg("adapt"));
bool adapt_engaged = dynamic_cast<stan::gm::bool_argument*>(adapt->arg("engaged"))->value();

if (model.num_params_r() == 0 && algo->value() != "fixed_param") {
std::cout
<< "Must use algorithm=fixed_param for model that has no parameters."
<< std::endl;
return -1;
}

if (algo->value() == "fixed_param") {

sampler_ptr = new stan::mcmc::fixed_param_sampler();
Expand Down
22 changes: 15 additions & 7 deletions src/stan/mcmc/hmc/hamiltonians/ps_point.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace stan {

// Point in a generic phase space
class ps_point {
friend class ps_point_test;

public:

Expand Down Expand Up @@ -76,16 +77,23 @@ namespace stan {
protected:

template <typename T>
inline void _fast_vector_copy(Eigen::Matrix<T, Eigen::Dynamic, 1>& v_to, const Eigen::Matrix<T, Eigen::Dynamic, 1>& v_from) {
v_to.resize(v_from.size());
std::memcpy(&v_to(0), &v_from(0), v_from.size() * sizeof(double));
static inline void _fast_vector_copy(Eigen::Matrix<T, Eigen::Dynamic, 1>& v_to, const Eigen::Matrix<T, Eigen::Dynamic, 1>& v_from) {
int sz = v_from.size();
v_to.resize(sz);
if (sz > 0) {
std::memcpy(&v_to(0), &v_from(0), v_from.size() * sizeof(double));
}
}

template <typename T>
inline void _fast_matrix_copy(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& v_to,
static inline void _fast_matrix_copy(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& v_to,
const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& v_from) {
v_to.resize(v_from.rows(), v_from.cols());
std::memcpy(&v_to(0), &v_from(0), v_from.size() * sizeof(double));
int nr = v_from.rows();
int nc = v_from.cols();
v_to.resize(nr, nc);
if (nr > 0 && nc > 0) {
std::memcpy(&v_to(0), &v_from(0), v_from.size() * sizeof(double));
}
}

};
Expand All @@ -95,4 +103,4 @@ namespace stan {
} // stan


#endif
#endif
64 changes: 64 additions & 0 deletions src/test/unit/mcmc/hmc/hamiltonians/ps_point_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <gtest/gtest.h>
#include <stan/mcmc/hmc/hamiltonians/ps_point.hpp>

namespace stan {

namespace mcmc {

class ps_point_test : public ::testing::Test
{
public:
typedef Eigen::Matrix<double, Eigen::Dynamic, 1> vector_t;
typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> matrix_t;

static void fast_vector_copy()
{
vector_t from3(3); from3 << 5.25, 3.125, -6.5;
vector_t to3(12);
ps_point::_fast_vector_copy(to3, from3);

EXPECT_EQ(from3, to3);

int zero = 0;
vector_t from0(zero);
vector_t to0(7);
ps_point::_fast_vector_copy(to0, from0);

EXPECT_EQ(from0, to0);
}

static void fast_matrix_copy()
{
matrix_t from2_3(2, 3);
from2_3 << 5, 2, 7, -3, 4, -9;
matrix_t to2_3(1, 13);
ps_point::_fast_matrix_copy(to2_3, from2_3);

EXPECT_EQ(from2_3, to2_3);

int zero = 0;

matrix_t from2_0(2, zero);
matrix_t to2_0(8, 3);
ps_point::_fast_matrix_copy(to2_0, from2_0);

EXPECT_EQ(from2_0, to2_0);

matrix_t from0_5(zero, 5);
matrix_t to0_5(7, 4);
ps_point::_fast_matrix_copy(to0_5, from0_5);

EXPECT_EQ(from0_5, to0_5);
}

};

TEST(psPoint, fastVectorCopy) {
ps_point_test::fast_vector_copy();
}

TEST(psPoint, fastMatrixCopy) {
ps_point_test::fast_matrix_copy();
}
}
}

0 comments on commit a8fd956

Please sign in to comment.