Skip to content

Commit c9c7baf

Browse files
feat(SimulationHelpers): add STatistic monitoring
1 parent 93cbc02 commit c9c7baf

9 files changed

Lines changed: 305 additions & 218 deletions

File tree

include/geode/stochastic/sampling/mcmc/helpers/simulation_monitor.hpp

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,8 @@
2222
*/
2323

2424
#pragma once
25+
#include <geode/basic/range.hpp>
2526
#include <geode/stochastic/common.hpp>
26-
// #include <geode/stochastic/sampling/mcmc/metropolis_hasting_sampler.hpp>
27-
// #include <geode/stochastic/sampling/mcmc/models/energy_term_collection.hpp>
28-
29-
#include <absl/strings/str_join.h>
30-
#include <filesystem>
31-
#include <fstream>
3227

3328
namespace geode
3429
{
@@ -44,40 +39,50 @@ namespace geode
4439

4540
StatisticsMonitor( const index_t nb_energy_terms )
4641
{
47-
sum.resize( nb_energy_terms, 0.0 );
48-
sum_squares.resize( nb_energy_terms, 0.0 );
49-
means.resize( nb_energy_terms, 0.0 );
50-
variances.resize( nb_energy_terms, 0.0 );
42+
sum_.resize( nb_energy_terms, 0.0 );
43+
sum_squares_.resize( nb_energy_terms, 0.0 );
44+
means_.resize( nb_energy_terms, 0.0 );
45+
variances_.resize( nb_energy_terms, 0.0 );
5146
}
5247

5348
void add_realization( const std::vector< double >& values )
5449
{
55-
for( const auto stat_id : Range{ values.size() } )
50+
OPENGEODE_EXCEPTION( values.size() == sum_.size(),
51+
"[StatisticsMonitor] - Mismatch between realization size and "
52+
"expected number of statistics." );
53+
++count_;
54+
for( size_t i = 0; i < values.size(); ++i )
5655
{
57-
sum[stat_id] += values[stat_id];
58-
sum_squares[stat_id] += values[stat_id] * values[stat_id];
56+
double delta = values[i] - means_[i];
57+
means_[i] += delta / count_;
58+
if( count_ > 1 )
59+
variances_[i] = ( ( count_ - 2 ) * variances_[i]
60+
+ delta * ( values[i] - means_[i] ) )
61+
/ ( count_ - 1 );
5962
}
6063
}
6164

62-
void finalize( const index_t nb_realizations )
65+
const index_t statiscal_count() const
6366
{
64-
for( const auto stat_id : Range{ sum.size() } )
65-
{
66-
means[stat_id] = sum[stat_id] / nb_realizations;
67-
double variance =
68-
( sum_squares[stat_id]
69-
- ( sum[stat_id] * sum[stat_id] ) / nb_realizations )
70-
/ ( nb_realizations - 1 );
71-
variances[stat_id] = variance;
72-
// stddevs[stat_id] =std::sqrt( std::max( variance, 0.0 ) );
73-
}
67+
return count_;
7468
}
7569

76-
public:
77-
std::vector< double > sum;
78-
std::vector< double > sum_squares;
79-
std::vector< double > means;
80-
std::vector< double > variances;
70+
const std::vector< double >& means() const
71+
{
72+
return means_;
73+
}
74+
75+
const std::vector< double >& variances() const
76+
{
77+
return variances_;
78+
}
79+
80+
private:
81+
std::vector< double > sum_;
82+
std::vector< double > sum_squares_;
83+
std::vector< double > means_;
84+
std::vector< double > variances_;
85+
index_t count_{ 0 };
8186
};
8287

8388
} // namespace geode

include/geode/stochastic/sampling/mcmc/helpers/simulation_printer.hpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
*/
2323

2424
#pragma once
25+
2526
#include <geode/stochastic/common.hpp>
26-
// #include <geode/stochastic/sampling/mcmc/metropolis_hasting_sampler.hpp>
27-
// #include <geode/stochastic/sampling/mcmc/models/energy_term_collection.hpp>
27+
2828
#include <geode/stochastic/sampling/mcmc/helpers/simulation_monitor.hpp>
2929
#include <geode/stochastic/spatial/object_sets.hpp>
3030

@@ -46,7 +46,7 @@ namespace geode
4646
std::string realisations_prefix{ "pattern_" };
4747
index_t realisations_print_frequency{ 100 };
4848

49-
std::string output_folder{ "." };
49+
std::string output_folder{ std::filesystem::current_path().string() };
5050
};
5151

5252
class SimulationPrinter
@@ -59,7 +59,7 @@ namespace geode
5959

6060
// Print statistics to the configured statistics file
6161
void print_statistics(
62-
const std::vector< double >& stats, absl::string_view header )
62+
const std::vector< double >& stats, absl::string_view header ) const
6363
{
6464
if( !config_.print_statistics )
6565
return;
@@ -72,9 +72,10 @@ namespace geode
7272

7373
template < typename ObjectType >
7474
void print_object_sets( const ObjectSets< ObjectType >& object_sets,
75-
index_t realization_id )
75+
index_t realization_id ) const
7676
{
77-
if( !config_.print_realisations )
77+
if( !config_.print_realisations
78+
|| realization_id % config_.realisations_print_frequency != 0 )
7879
return;
7980

8081
const auto filename =
@@ -96,7 +97,7 @@ namespace geode
9697
}
9798
}
9899
void print_statistics_summary( const StatisticsMonitor& monitor,
99-
absl::string_view energy_term_names )
100+
absl::string_view energy_term_names ) const
100101
{
101102
if( !config_.print_statistics_summary )
102103
return;
@@ -109,16 +110,17 @@ namespace geode
109110
std::ofstream file = open_file_with_dirs( summary_path );
110111
file << "# Summary statistics\n";
111112
file << energy_term_names.data() << "\n";
112-
file << absl::StrCat( "# Count:\n", monitor.count, "\n" );
113113
file << absl::StrCat(
114-
"# Means:\n", absl::StrJoin( monitor.means, " ; " ), "\n" );
114+
"# Count:\n", monitor.statiscal_count(), "\n" );
115+
file << absl::StrCat(
116+
"# Means:\n", absl::StrJoin( monitor.means(), " ; " ), "\n" );
115117
file << absl::StrCat( "# Variances:\n",
116-
absl::StrJoin( monitor.variances, " ; " ), "\n" );
118+
absl::StrJoin( monitor.variances(), " ; " ), "\n" );
117119
}
118120

119121
private:
120122
void write_header_if_new(
121-
absl::string_view filename, absl::string_view header )
123+
absl::string_view filename, absl::string_view header ) const
122124
{
123125
namespace fs = std::filesystem;
124126
fs::path file_path{ std::string( filename ) };
@@ -148,7 +150,8 @@ namespace geode
148150

149151
return file;
150152
}
151-
const std::string& create_statistics_file( absl::string_view header )
153+
const std::string& create_statistics_file(
154+
absl::string_view header ) const
152155
{
153156
stats_file_path_ = ( std::filesystem::path( config_.output_folder )
154157
/ config_.statistics_filename )
@@ -165,7 +168,7 @@ namespace geode
165168

166169
private:
167170
SimulationPrinterConfigurator config_;
168-
std::optional< std::string > stats_file_path_;
171+
mutable std::optional< std::string > stats_file_path_;
169172
};
170173

171174
} // namespace geode

0 commit comments

Comments
 (0)