Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 34 additions & 37 deletions include/geode/stochastic/inference/statistics_tools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,50 +27,47 @@
#include <geode/stochastic/inference/statistics_tracker.hpp>
#include <geode/stochastic/inference/target_statistics.hpp>

namespace geode
namespace geode::statistics
{
namespace statistics
template < typename ObjectType >
void validate( const StatisticsTracker< ObjectType >& tracker,
const TargetStatistics< ObjectType >& targets )
{
template < typename ObjectType >
void validate( const StatisticsTracker< ObjectType >& tracker,
const TargetStatistics< ObjectType >& targets )
{
const auto& model = targets.model();
const auto& model = targets.model();

for( const auto& term_uuid : targets.active_terms() )
{
const auto mean = tracker.mean( term_uuid );
const auto target = targets.target( term_uuid );
for( const auto& term_uuid : targets.active_terms() )
{
const auto mean = tracker.mean( term_uuid );
const auto target = targets.target( term_uuid );

const auto rel_error =
std::fabs( mean - target )
/ ( std::fabs( target ) + geode::GLOBAL_EPSILON );
const auto rel_error =
std::fabs( mean - target )
/ ( std::fabs( target ) + geode::GLOBAL_EPSILON );

OpenGeodeStochasticStochasticException::check_exception(
rel_error < targets.tolerance( term_uuid ), nullptr,
OpenGeodeException::TYPE::result,
"[StatisticsValidator] Failure for term ",
model.term_name( term_uuid ), "\n mean = ", mean,
"\n target = ", target, "\n error = ", rel_error,
"\n tol = ", targets.tolerance( term_uuid ) );
}
OpenGeodeStochasticStochasticException::check_exception(
rel_error < targets.tolerance( term_uuid ), nullptr,
OpenGeodeException::TYPE::result,
"[StatisticsValidator] Failure \n --> term ",
model.term_name( term_uuid ), "\n mean = ", mean,
"\n target = ", target, "\n error = ", rel_error,
"\n tol = ", targets.tolerance( term_uuid ) );
}
}

template < typename ObjectType >
double quadratic_loss( const StatisticsTracker< ObjectType >& tracker,
const TargetStatistics< ObjectType >& targets )
{
double loss = 0.0;

for( const auto& term_uuid : targets.active_terms() )
{
const auto diff =
tracker.mean( term_uuid ) - targets.value( term_uuid );
template < typename ObjectType >
double quadratic_loss( const StatisticsTracker< ObjectType >& tracker,
const TargetStatistics< ObjectType >& targets )
{
double loss = 0.0;

loss += diff * diff;
}
for( const auto& term_uuid : targets.active_terms() )
{
const auto diff =
tracker.mean( term_uuid ) - targets.value( term_uuid );

return loss;
loss += diff * diff;
}
} // namespace statistics
} // namespace geode

return loss;
}
} // namespace geode::statistics
20 changes: 16 additions & 4 deletions include/geode/stochastic/models/energy_terms/pairwise_term.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,16 @@ namespace geode
std::optional< ObjectId > exclude_id,
const ObjectSets< ObjectType >& state ) const
{
double sum = 0.0;
const auto impacted_set_it =
objectset_adjacency_map_.find( object_ref.set_id );
if( impacted_set_it == objectset_adjacency_map_.end() )
{
return 0.;
}
const auto neighbors = state.neighbors( object_ref.object,
objectset_adjacency_map_.at( object_ref.set_id ),
impacted_set_it->second,
interaction_->neighborhood_searching_distance(), exclude_id );
double sum = 0.0;
for( const auto& neigh_id : neighbors )
{
ObjectRef< ObjectType > neigh_object{
Expand All @@ -147,11 +153,17 @@ namespace geode
{
return 0.;
}
double sum = 0.0;
const auto impacted_set_it =
objectset_adjacency_map_.find( object_id.set_id );
if( impacted_set_it == objectset_adjacency_map_.end() )
{
return 0.;
}
const auto neighbors = state.neighbors( cur_obj,
objectset_adjacency_map_.at( object_id.set_id ),
impacted_set_it->second,
interaction_->neighborhood_searching_distance(), object_id );
ObjectRef< ObjectType > object_ref{ cur_obj, object_id.set_id };
double sum = 0.0;
for( const auto& neigh_id : neighbors )
{
const auto& neigh_obj = state.get_object( neigh_id );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
* SOFTWARE.
*
*/
#pragma once

#include <geode/stochastic/models/energy_terms/energy_term_builder.hpp>

Expand Down Expand Up @@ -63,12 +62,12 @@ namespace

namespace geode
{
std::pair< std::vector< geode::uuid >,
std::pair< std::vector< uuid >,
absl::flat_hash_map< uuid, std::vector< uuid > > >
pairwise_builder_initialize_interactions_helper(
const std::vector< std::pair< uuid, uuid > >& interacting_sets )
{
std::vector< geode::uuid > interacting_set_ids;
std::vector< uuid > interacting_set_ids;
absl::flat_hash_map< uuid, std::vector< uuid > >
objectset_adjacency_map;

Expand Down
14 changes: 7 additions & 7 deletions tests/stochastic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,13 @@ add_geode_test(
${PROJECT_NAME}::stochastic
)

#add_geode_test(
# SOURCE "sampling/mcmc/test-mh-strauss.cpp"
# DEPENDENCIES
# OpenGeode::basic
# OpenGeode::geometry
# ${PROJECT_NAME}::stochastic
#)
add_geode_test(
SOURCE "sampling/mcmc/test-mh-strauss.cpp"
DEPENDENCIES
OpenGeode::basic
OpenGeode::geometry
${PROJECT_NAME}::stochastic
)


add_geode_test(
Expand Down
80 changes: 19 additions & 61 deletions tests/stochastic/sampling/mcmc/test-mh-poisson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <geode/stochastic/spatial/single_object_features/single_object_feature_config.hpp>
namespace
{
// NOLINT(*-magic-numbers)
struct SetDescription
{
std::string name;
Expand All @@ -48,7 +49,8 @@
: public geode::SimulationRunner< geode::Point2D >
{
public:
PoissonSimulationRunner( const geode::SpatialDomain< 2 >& domain )
explicit PoissonSimulationRunner(
const geode::SpatialDomain< 2 >& domain )
: geode::SimulationRunner< geode::Point2D >( domain ) {};

void add_set_descriptor( const SetDescription& descriptor )
Expand Down Expand Up @@ -94,7 +96,7 @@
geode::ModelConfig config;
for( const auto& energy_desc : density_descriptors_ )
{
config.terms.push_back( energy_desc );
config.terms.emplace_back( energy_desc );
}

model_ = std::move( geode::build_model< geode::Point2D >(
Expand Down Expand Up @@ -122,52 +124,7 @@
create_target_statistics();
}

void check_statistics( const geode::StatisticsTracker< geode::Point2D >&
statistic_monitoring ) const
{
// const auto& computed_means =
// statistic_monitoring.means(); const auto&
// computed_variances = statistic_monitoring.variances();
//
// for( const auto stat_id :
// geode::Range{
// this->energy_terms_collection_.size() } )
// {
// const auto& term = energy_terms_collection_.get(
// ordered_energy_terms_[stat_id] );
//
// const auto expected_means =
// this->ordered_target_statistics_[stat_id];
//
// const auto target_vs_mean_error =
// std::fabs( computed_means[stat_id] -
// expected_means ) / expected_means;
//
// geode::OpenGeodeStochasticStochasticException::test(
// target_vs_mean_error < 0.05, "[MH test]
// statistic value ", computed_means[stat_id], "
// for energy term: ", term.name().value_or(
// term.id().string() ), " not close enough to
// expected value ", expected_means, " --> error
// : ", target_vs_mean_error );
//
// const auto target_vs_variance_error =
// std::fabs( computed_variances[stat_id] -
// expected_means ) / expected_means;
//
// geode::OpenGeodeStochasticStochasticException::test(
// target_vs_variance_error < 0.15,
// "[MH test] variance of statistic ",
// computed_variances[stat_id], " for energy
// term: ", term.name().value_or(
// term.id().string() ), " not close enough to
// expected value ", expected_means, " --> error
// : ", target_vs_variance_error );
// }
}

private:
geode::BoundingBox2D box_;
std::vector< SetDescription > set_descriptors_;
std::vector< PoissonDensityDescription > density_descriptors_;
std::vector< geode::TargetStatisticConfig >
Expand All @@ -183,34 +140,34 @@

geode::BoundingBox2D box;
box.add_point( geode::Point2D{ { 0.0, 0.0 } } );
box.add_point( geode::Point2D{ { 10.0, 10.0 } } );

Check warning on line 143 in tests/stochastic/sampling/mcmc/test-mh-poisson.cpp

View workflow job for this annotation

GitHub Actions / test / tidy

tests/stochastic/sampling/mcmc/test-mh-poisson.cpp:143:48 [cppcoreguidelines-avoid-magic-numbers]

10.0 is a magic number; consider replacing it with a named constant

Check warning on line 143 in tests/stochastic/sampling/mcmc/test-mh-poisson.cpp

View workflow job for this annotation

GitHub Actions / test / tidy

tests/stochastic/sampling/mcmc/test-mh-poisson.cpp:143:42 [cppcoreguidelines-avoid-magic-numbers]

10.0 is a magic number; consider replacing it with a named constant
geode::SpatialDomain domain( box, 0. );

std::array< double, 4 > birth_ratio{ 0.1, 0.5, 2., 4. };

Check warning on line 146 in tests/stochastic/sampling/mcmc/test-mh-poisson.cpp

View workflow job for this annotation

GitHub Actions / test / tidy

tests/stochastic/sampling/mcmc/test-mh-poisson.cpp:146:60 [cppcoreguidelines-avoid-magic-numbers]

4. is a magic number; consider replacing it with a named constant

Check warning on line 146 in tests/stochastic/sampling/mcmc/test-mh-poisson.cpp

View workflow job for this annotation

GitHub Actions / test / tidy

tests/stochastic/sampling/mcmc/test-mh-poisson.cpp:146:56 [cppcoreguidelines-avoid-magic-numbers]

2. is a magic number; consider replacing it with a named constant

Check warning on line 146 in tests/stochastic/sampling/mcmc/test-mh-poisson.cpp

View workflow job for this annotation

GitHub Actions / test / tidy

tests/stochastic/sampling/mcmc/test-mh-poisson.cpp:146:51 [cppcoreguidelines-avoid-magic-numbers]

0.5 is a magic number; consider replacing it with a named constant

Check warning on line 146 in tests/stochastic/sampling/mcmc/test-mh-poisson.cpp

View workflow job for this annotation

GitHub Actions / test / tidy

tests/stochastic/sampling/mcmc/test-mh-poisson.cpp:146:46 [cppcoreguidelines-avoid-magic-numbers]

0.1 is a magic number; consider replacing it with a named constant
std::array< double, 4 > change_ratio{ 0., 1., 1., 0. };

for( const auto config : geode::Range{ birth_ratio.size() } )
{
// --- Set description
SetDescription setA;
setA.name = "A";
setA.birth_ratio = birth_ratio[config];
setA.death_ratio = 1.0;
setA.change_ratio = change_ratio[config];
SetDescription set_a;
set_a.name = "A";
set_a.birth_ratio = birth_ratio[config];
set_a.death_ratio = 1.0;
set_a.change_ratio = change_ratio[config];

// --- Energy term description
PoissonDensityDescription densityA;
densityA.term_name = "density";
densityA.object_set_names = { "A" };
densityA.lambda = 0.3;
densityA.object_feature = geode::ObjectInDomainFeatureConfig{};
PoissonDensityDescription density_a;
density_a.term_name = "density";
density_a.object_set_names = { "A" };
density_a.lambda = 0.3;

Check warning on line 162 in tests/stochastic/sampling/mcmc/test-mh-poisson.cpp

View workflow job for this annotation

GitHub Actions / test / tidy

tests/stochastic/sampling/mcmc/test-mh-poisson.cpp:162:32 [cppcoreguidelines-avoid-magic-numbers]

0.3 is a magic number; consider replacing it with a named constant
density_a.object_feature = geode::ObjectInDomainFeatureConfig{};

geode::TargetStatisticConfig statA{ "density", 30.0, 0.15 };
geode::TargetStatisticConfig stat_a{ "density", 30.0, 0.15 };

Check warning on line 165 in tests/stochastic/sampling/mcmc/test-mh-poisson.cpp

View workflow job for this annotation

GitHub Actions / test / tidy

tests/stochastic/sampling/mcmc/test-mh-poisson.cpp:165:67 [cppcoreguidelines-avoid-magic-numbers]

0.15 is a magic number; consider replacing it with a named constant

Check warning on line 165 in tests/stochastic/sampling/mcmc/test-mh-poisson.cpp

View workflow job for this annotation

GitHub Actions / test / tidy

tests/stochastic/sampling/mcmc/test-mh-poisson.cpp:165:61 [cppcoreguidelines-avoid-magic-numbers]

30.0 is a magic number; consider replacing it with a named constant

PoissonSimulationRunner runner( domain );
runner.add_set_descriptor( setA );
runner.add_density_descriptor( densityA );
runner.add_target_statistics( statA );
runner.add_set_descriptor( set_a );
runner.add_density_descriptor( density_a );
runner.add_target_statistics( stat_a );
runner.initialize();

// run simulation
Expand All @@ -220,7 +177,7 @@
"/sim_point_poisson_test_", config );

geode::SimulationConfigurator sim_config;
sim_config.realizations = 1000;

Check warning on line 180 in tests/stochastic/sampling/mcmc/test-mh-poisson.cpp

View workflow job for this annotation

GitHub Actions / test / tidy

tests/stochastic/sampling/mcmc/test-mh-poisson.cpp:180:39 [cppcoreguidelines-avoid-magic-numbers]

1000 is a magic number; consider replacing it with a named constant
sim_config.metropolis_hasting_steps = 1000;
sim_config.burn_in_steps = 1000;
sim_config.printer = printer_config;
Expand Down Expand Up @@ -323,4 +280,5 @@
{
return geode::geode_lippincott();
}
// NOLINTEND(*-magic-numbers)

Check failure on line 283 in tests/stochastic/sampling/mcmc/test-mh-poisson.cpp

View workflow job for this annotation

GitHub Actions / test / tidy

tests/stochastic/sampling/mcmc/test-mh-poisson.cpp:283:8 [clang-tidy-nolint]

unmatched 'NOLINTEND' comment without a previous 'NOLINTBEGIN' comment
}
Loading
Loading