12 const int MIN_SCALE = -6;
13 const int MAX_SCALE = 12;
16 const size_t MY_MAX_NODES = 35;
21 const size_t N_CONSTANTS = 4;
27 double compoundNormalLogUniform_lpdf(
const double _x) {
28 auto x = std::abs(_x);
29 return log(std::erfc(x*exp(-MAX_SCALE)/
ROOT2)-std::erfc(x*exp(-MIN_SCALE)/
ROOT2)) -
30 LOG2 - log(x) - log(MAX_SCALE-MIN_SCALE);
45 assert(constants.size() == N_CONSTANTS);
47 reset_constant_index();
50 assert(constant_idx == count_constants());
58 virtual double constant_prior()
const {
61 if(count_constants() > N_CONSTANTS) {
66 for(
auto& c : constants) {
67 lp += compoundNormalLogUniform_lpdf(std::abs(c));
73 virtual void randomize_constants()
override {
75 constants.resize(N_CONSTANTS);
76 for(
size_t i=0;i<N_CONSTANTS;i++) {
82 virtual size_t count_constants()
const override {
84 for(
const auto& x :
value) {
93 std::pair<double,double> constant_proposal(
Constant c)
const override {
98 auto sc = exp(
uniform(MIN_SCALE, MAX_SCALE));
106 return std::make_pair(v,fb);
112 return std::make_pair(v,fb);
117 double fx = this->
call(datum.input,
NaN);
119 if(std::isnan(fx) or std::isinf(fx))
124 return normal_lpdf(fx, datum.output, datum.reliability );
129 if(this->value.count() > MY_MAX_NODES) {
134 #ifdef REQUIRE_USE_ALL_VARIABLES 135 std::vector<bool> uses_variable(
NUM_VARS,
false);
136 for(
const auto& n : this->value) {
137 if(n.rule->format[0] ==
'x') {
138 std::string s = n.rule->format; s.erase(0,1);
139 uses_variable[string_to<int>(s)] =
true;
142 for(
const auto& v : uses_variable) {
151 virtual std::string __my_string_recurse(
const Node* n,
size_t& idx)
const {
156 else if(n->
rule->
N == 0) {
163 std::vector<std::string> childStrings(n->
nchildren());
167 for(
size_t i=0;i<n->
rule->
N;i++) {
168 childStrings[i] = __my_string_recurse(&n->
child(i),idx);
172 for(
size_t i=0;i<n->
rule->
N;i++) {
174 assert(pos != std::string::npos);
182 virtual std::string
string(std::string prefix=
"")
const override {
185 return structure_string();
191 virtual std::string structure_string(
bool usedot=
true)
const {
204 virtual size_t hash()
const override {
220 assert(constants.size() == N_CONSTANTS);
234 ret.constants = constants;
236 double fb = x.second;
239 return std::make_pair(ret, fb);
247 assert(constants.size() == N_CONSTANTS);
258 for(
size_t i=0;i<N_CONSTANTS;i++) {
259 if(should_propose[i]) {
260 auto [v, __fb] = this->constant_proposal(constants[i]);
261 ret.constants[i] = v;
266 return std::make_pair(ret, fb);
283 double fb = x.second;
286 ret.constants = constants;
288 assert( constants.size() == N_CONSTANTS);
290 ret.randomize_constants();
291 fb += ret.constant_prior()-this->constant_prior();
294 ret.constants = constants;
298 return std::make_pair(ret, fb);
307 ret.randomize_constants();
313 randomize_constants();
318 ret.randomize_constants();
323 randomize_constants();
328 ret.randomize_constants();
std::vector< bool > random_nonempty_subset(const size_t n, const double p)
Returns a random nonempty subset of n elements, as a vector<bool> of length n with trues for elements...
Definition: Random.h:220
ProposalType
Definition: Main.cpp:124
virtual size_t hash() const override
Definition: Lexicon.h:117
virtual bool operator==(const ConstantContainer &h) const
Definition: ConstantContainer.h:77
virtual size_t hash() const
Definition: ConstantContainer.h:91
virtual void complete() override
Definition: Lexicon.h:246
std::optional< std::pair< Node, double > > sample_function_leaving_args(GrammarType *grammar, const Node &from)
This samples functions f(a,b) -> g(a,b) (e.g. without destroying what's below). This uses a little tr...
Definition: Proposers.h:331
This is a struct to basically hold a double (or float) for use in SymbolicRegression etc This allows ...
static MyHypothesis sample()
Static function for making a hypothesis. Be careful using this with references because they may not f...
Definition: MCMCable.h:29
virtual double compute_prior() override
Definition: MyHypothesis.h:40
bool is_evaluable() const override
Definition: Lexicon.h:349
Definition: ConstantContainer.h:48
double compute_single_likelihood(const datum_t &d) override
Compute the likelihood of a single data point.
Definition: Main.cpp:229
size_t NUM_VARS
Definition: Main.cpp:43
double uniform()
Definition: Random.h:11
Node value
Definition: LOTHypothesis.h:72
const double ROOT2
Definition: Numerics.h:19
std::string to_string_with_precision(const T a_value, const int n=14)
Definition: Strings.h:23
double prior
Definition: Bayesable.h:42
double data_X_mean
Definition: Main.cpp:32
virtual DiscreteDistribution< output_t > call(const key_t k, const input_t x, const output_t &err=output_t{})
Definition: Lexicon.h:361
static const std::string ChildStr
Definition: Rule.h:24
DiscreteDistribution< word > call(const utterance &input)
Definition: Main.cpp:225
std::array< D, MAX_VARS > X_t
Definition: Main.cpp:47
bool flip(float p=0.5)
Definition: Random.h:25
Definition: ConstantContainer.h:12
size_t N
Definition: Rule.h:29
we don't need inputs/outputs for out MyHypothesis
Definition: MyHypothesis.h:6
std::optional< std::pair< Node, double > > delete_tree(GrammarType *grammar, const Node &from)
Definition: Proposers.h:275
double random_normal(double mu=0, double sd=1.0)
Definition: Random.h:39
constexpr double infinity
Definition: Numerics.h:20
static constexpr nonterminal_t nt()
Definition: Grammar.h:84
Definition: DeterministicLOTHypothesis.h:14
Bayesable< defaultdatum_t< S, S >, std::vector< defaultdatum_t< S, S > > >::datum_t datum_t
Definition: LOTHypothesis.h:47
const double LOG2
Definition: Numerics.h:18
std::string format
Definition: Rule.h:28
this_t & child(const size_t i)
Definition: BaseNode.h:175
const Rule * rule
Definition: Node.h:32
std::optional< std::pair< Node, double > > regenerate(GrammarType *grammar, const Node &from)
A little helper function that resamples everything below when we can. If we can't, then we'll recurse.
Definition: Proposers.h:107
virtual std::string string(std::string prefix="") const override
Definition: MyHypothesis.h:159
const std::string LAMBDAXDOT_STRING
Definition: Strings.h:20
virtual std::string string(std::string prefix="") const override
Definition: Lexicon.h:102
void expand_to_neighbor(int k) override
Definition: Lexicon.h:336
std::optional< std::pair< Node, double > > insert_tree(GrammarType *grammar, const Node &from)
Definition: Proposers.h:192
constexpr double NaN
Definition: Numerics.h:21
This is a thread_local rng whose first object is used to see others (in other threads). This way, we can have thread_local rngs that all are seeded deterministcally in Fleet via –seed=X.
double data_Y_sd
Definition: Main.cpp:35
virtual MyHypothesis make_neighbor(int k) const
Return a new hypothesis which is the k'th neighbor (just calls expand_to_neighbor) NOTE This does not...
Definition: Searchable.h:35
nonterminal_t nt
Definition: Rule.h:27
Definition: ConstantContainer.h:24
std::optional< std::pair< Node, double > > swap_args(GrammarType *grammar, const Node &from)
This propose swaps around arguments of the same type.
Definition: Proposers.h:389
virtual MyHypothesis restart() const override
Definition: Lexicon.h:301
virtual bool operator==(const MyHypothesis &l) const override
Equality checks equality on each part.
Definition: Lexicon.h:138
double data_Y_mean
Definition: Main.cpp:33
double data_X_sd
Definition: Main.cpp:34
size_t nchildren() const
Definition: BaseNode.h:208
double D
Definition: Main.cpp:15
T normal_lpdf(T x, T mu=0.0, T sd=1.0)
Definition: Random.h:45
virtual double compute_prior() override
Definition: Lexicon.h:252
virtual std::optional< std::pair< MyHypothesis, double > > propose() const override
This proposal guarantees that there will be at least one factor that is proposed to. Each individual factor is proposed to with p_factor_propose.
Definition: Main.cpp:163