creativity  v1.3.0
Agent-based model of creativity and piracy
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
creativity::data::Treatment Class Reference

This class converts raw simulation data into multiple data rows, with piracy/policy, SR/LR as treatment effects on the base (pre-piracy, pre-policy) row. More...

#include <creativity/data/Treatment.hpp>

Public Member Functions

 Treatment ()=default
 Default constructor.
 
 Treatment (CSVParser &&csv)
 Takes over the given CSVParser and reads the data from it.
 
 Treatment (const std::string &filename)
 Creates a new CSVParser to read the given filename.
 
void readCSV (CSVParser &&csv)
 Reads the data from the given CSVParser.
 
void requirePiracy (bool require=true)
 Requires that the parsed data contain piracy observations. More...
 
void requirePolicy (bool require=true)
 Like requirePiracy(), but for policy data.
 
void requirePre (bool require=true)
 Like requirePiracy(), but for pre-piracy data.
 
void requireSR (bool require=true)
 Requires short-run data for long-run piracy/policy data that exists in the source data. More...
 
const bool & hasPre () const
 True if this treatment data pre-piracy observations.
 
const bool & hasPiracy () const
 True if the source data has piracy data.
 
const bool & hasPolicy () const
 True if the source data has policy data.
 
const bool & hasPiracySR () const
 True if the source data has short-run piracy data.
 
const bool & hasPolicySR () const
 True if the source data has short-run policy data.
 
bool hasShortrun () const
 True if the source data has short-run data for each type of associated long-run data. More...
 
const unsigned int & rowsPerSimulation () const
 The number of data rows per simulation input. This equals 1 plus the number of treatments.
 
unsigned int simulations () const
 The number of simulation files contributing to this data: this is simply an alias for obj.data().rows() / obj.rowsPerSimulation()
 
const Eigen::MatrixXd & data () const
 The full set of data. More...
 
const std::vector< std::string > & sourceFiles () const
 The simulation source values. More...
 
const std::string & sourceFile (unsigned r) const
 Return the source file associated with row r. More...
 
const std::unordered_map< std::string, unsigned > & columns () const
 Accesses the map of field name to column indices. More...
 
std::shared_ptr< const SimpleVariablevariable (const std::string &name) const
 Returns a shared_ptr to a SimpleVariable containing a copy of the requested column. More...
 
std::shared_ptr< const SimpleVariableoperator[] (const std::string &name) const
 Alias for variable()
 

Protected Member Functions

void generateRow (const CSVParser &csv, Eigen::Ref< Eigen::RowVectorXd, 0, Eigen::InnerStride<>> newrow, const std::string &prefix) const
 Generates a row of data from the CSVParser at its current position. More...
 

Protected Attributes

Eigen::MatrixXd data_
 The data matrix.
 
std::vector< std::string > source_
 In-order source values for data.
 
bool have_data_ {false}
 True once we've parsed a data file.
 
bool has_pre_ {false}
 True if the data contains pre-piracy, non-treatment rows.
 
bool has_piracy_ {false}
 True if the data contains LR piracy treatment rows.
 
bool has_piracy_sr_ {false}
 True if the data contains SR piracy treatment rows.
 
bool has_policy_ {false}
 True if the data contains LR policy treatment rows.
 
bool has_policy_sr_ {false}
 True if the data contains SR policy treatment rows.
 
bool require_pre_ {false}
 True if pre data is required.
 
bool require_piracy_ {false}
 True if piracy data is required.
 
bool require_policy_ {false}
 True if policy data is required.
 
bool require_sr_ {false}
 True if short-run data is required for each type of long-run data.
 
unsigned int rows_per_sim_ {0}
 The number of treatment row observations per source data rows (i.e. per simulation)
 
std::unordered_map< std::string, unsigned > data_column_
 Field name to column map.
 
std::unordered_map< std::string, std::shared_ptr< SimpleVariable > > var_cache_
 Cache of weak pointers to returned SimpleVariables.
 

Detailed Description

This class converts raw simulation data into multiple data rows, with piracy/policy, SR/LR as treatment effects on the base (pre-piracy, pre-policy) row.

It additionally supports filtering the resulting data–for instance, only using observations that still exhibit some writing during piracy.

Member Function Documentation

§ columns()

const std::unordered_map<std::string, unsigned>& creativity::data::Treatment::columns ( ) const

Accesses the map of field name to column indices.

Data fields (e.g. "books_written") are not prefixed with "pre.", "piracy.", etc.; parameters (e.g. "param.readers") are prefixed; there are also dummies "piracy", "policy", "SR", and "LR".

§ data()

const Eigen::MatrixXd& creativity::data::Treatment::data ( ) const
inline

The full set of data.

See also
TreatmentFilter for extracting conditional data.

§ generateRow()

void creativity::data::Treatment::generateRow ( const CSVParser csv,
Eigen::Ref< Eigen::RowVectorXd, 0, Eigen::InnerStride<>>  newrow,
const std::string &  prefix 
) const
protected

Generates a row of data from the CSVParser at its current position.

Parameters
csvthe CSVParser positioned at the desired row.
newrowthe matrix row in which values will be set
prefixthe prefix (such as "pre." or "policy.SR.") to add to non-param. data columns.

§ hasShortrun()

bool creativity::data::Treatment::hasShortrun ( ) const
inline

True if the source data has short-run data for each type of associated long-run data.

That is, if the data has piracy data, it must also have short-run piracy data; likewise for policy data.

§ requirePiracy()

void creativity::data::Treatment::requirePiracy ( bool  require = true)

Requires that the parsed data contain piracy observations.

If the data has already been parsed, this throws an exception immediately if the data does not contain piracy; if not yet parsed, this causes the next readCSV() to throw an exception if piracy data is not present.

Parameters
requiredefaults to true of omitted; can be explicitly specified as false to cancel a previous requirePiracy() call.

§ requireSR()

void creativity::data::Treatment::requireSR ( bool  require = true)

Requires short-run data for long-run piracy/policy data that exists in the source data.

In particular, if this option is enabled, the data must contain short-run observations for each category with equivalent long-run observations: that is, if there is long-run piracy data, there must also be short-run piracy data, and likewise for policy data. If the source data does not contain long-run piracy data, this option will not require short-run piracy data.

Like requirePiracy(), this throws immediately if data has already been parsed; if not, the exception will be raised if attempting to read data that doesn't contain the required short-run data.

§ sourceFile()

const std::string& creativity::data::Treatment::sourceFile ( unsigned  r) const
inline

Return the source file associated with row r.

This is simply an alias for sourceFiles().at(r / rowsPerSimulation()).

§ sourceFiles()

const std::vector<std::string>& creativity::data::Treatment::sourceFiles ( ) const
inline

The simulation source values.

Data in row x is from source file at index x / rowsPerSimulation().

§ variable()

std::shared_ptr<const SimpleVariable> creativity::data::Treatment::variable ( const std::string &  name) const

Returns a shared_ptr to a SimpleVariable containing a copy of the requested column.

The column naming values are the same as those accepted by column().

Values are internally cached–multiple calls to variable with the same value return the same SimpleVariable.

Exceptions
std::out_of_rangeif the requested variable does not exist.
Inheritance diagram for creativity::data::Treatment:
[legend]

The documentation for this class was generated from the following file: