test-econ-model/include/econ.hpp

93 lines
2.5 KiB
C++

#include <iostream>
#include <vector>
#include <random>
#include <cmath>
static std::random_device econ_dev;
static std::mt19937 econ_rng(econ_dev());
// ----------------------------------------------------------
// -- Recipient Finders
// ----------------------------------------------------------
/**
* @brief Find & return random participant
*
* @param balances Balance vec
* @return int
*/
static int find_participant_random(std::vector<int> &balances) {
// random spend ratio is too costly
std::uniform_int_distribution<std::mt19937::result_type> dist_len(0,balances.size()-1);
return dist_len(econ_rng);
}
/**
* @brief Find & return random participant
*
* @param balances Balance vec
* @param percent_cutoff Upper N% to decide among
* @return int
*/
static int find_participant_random_wealthy(std::vector<int> &balances, float percent_cutoff = 10) {
// random spend ratio is too costly
int cutoff = percent_cutoff / 100 * balances.size();
std::uniform_int_distribution<std::mt19937::result_type> dist_len(0,balances.size()-1);
return dist_len(econ_rng);
}
// ----------------------------------------------------------
// -- Spend Decisions
// ----------------------------------------------------------
/**
* @brief Simple random economic spend decision
*
* @param balances Balances vec
* @param id ID of participant making the decision
* @param spend_ratio Proportion of savings they are willing
* to spend
*/
static void economic_decision_simple(std::vector<int> &balances, int id, float spend_ratio) {
// assert(spend_ratio > 0 && spend_ratio <= 1);
int total_bal = balances.at(id);
int spendable = spend_ratio * total_bal;
int recipient_id = find_participant_random(balances);
balances.at(id) -= spendable;
balances.at(recipient_id) += spendable;
}
/**
* @brief Sin-based varying spend output based on age. Peaks in midlife
*
* @param ages Ages vec
* @param balances Balances vec
* @param id ID of participant making decision
* @param max_spend_ratio max/peak spend ratio
* @param bump how much to bump min spend amount from 0
*/
static void economic_random_age_sin(
std::vector<int> &ages,
std::vector<int> &balances,
int max_age,
int id,
float max_spend_ratio,
float bump = 0.1
) {
int recipient_id = find_participant_random(balances);
int spendable = round(
max_spend_ratio *
sin(ages[id] / max_age) +
bump)
;
balances.at(id) -= spendable;
balances.at(recipient_id) += spendable;
}
static void economic_random_age_sin() {
}