API Listing
WiNDC.NationalTable
— TypeNationalTable
Subtype of WiNDCtable
that holds the national data.
Fields
table::DataFrame
: The main table of the WiNDCtable.sets::DataFrame
: The sets of the WiNDCtable.
Domain
:commodities
:sectors
:year
WiNDC.StateTable
— TypeStateTable
A table that contains data for state-level data.
Fields
table::DataFrame
- The data table.sets::DataFrame
- The sets table.
WiNDC.WiNDCtable
— TypeWiNDCtable
Abstract supertype for all WiNDCtables.
Required Fields
table::DataFrame
: The main table of the WiNDCtable.sets::DataFrame
: The sets of the WiNDCtable.
Required Functions
domain(data::T) where T<:WiNDCtable
:domain
To Do:
Add general idea of how this works.
DataFrames.subset
— Methodsubset(
data::T,
@nospecialize(args...)
) where T <: WiNDCtable
Return a subset of data
based on the conditions given in args
. This will subset both the main table and the set table.
Required Arguments
data::T
: TheWiNDCtable
to subset.args::Tuple
: Pairs of the formset_name => boolean function
.
Return
A table of type T
.
Example
If data
is a detailed NationalTable
and we want to only view the soybean commodity:
```julia
subset(data, :commodities => (y -> y=="1111A0") )
WiNDC.absorption_tax_rate
— Methodabsorption_tax_rate(data::AbstractNationalTable; column::Symbol = :value, output::Symbol = :value)
Calculate the absorption tax rate of the sectors.
Required Arguments
data::AbstractNationalTable
: The national data.
Keyword Arguments
column::Symbol = :value
: The column to be used for the calculation.output::Symbol = :value
: The name of the output column.
Output
Returns a DataFrame with the columns :sectors
and :value
.
WiNDC.armington_supply
— Methodarmington_supply(data::AbstractNationalTable; column::Symbol = :value, output::Symbol = :value)
Calculate the armington supply of the sectors.
Required Arguments
data::AbstractNationalTable
: The national data.
Keyword Arguments
column::Symbol = :value
: The column to be used for the calculation.output::Symbol = :value
: The name of the output column.
Output
Returns a DataFrame with the columns :sectors
and :value
.
Process
\[\sum_{s\in\text{Sectors}} \text{Intermediate_Demand} + \text{Exogenous_Final_Demand} + \text{Personal_Consumption}\]
WiNDC.build_national_table
— Methodbuild_national_table(
file_paths::Vector{String};
aggregation = :detailed
)
Builds the national data from the supply and use tables.
Required Arguments
file_paths::Vector{String}
: A vector of file paths to the supply and use tables. Uses regex to search for the correct files. This should be the output offetch_supply_use
Optional Arguments
aggregation::Symbol = :detailed
: The type of table. Either:detailed
or:summary
.
Return
Process
The data undergoes a series of transformations to create the final table. The data is loaded from the XLSXFiles, transformed into a DataFrame, and then joined with the sets created from the table.
The following data tranformations take place:
- Negative flows from
intermediate_demand
andintermediate_supply
are reversed. subsidies
andsector_subsidy
are negated.margin_demand
retains only positive values.margin_supply
retains only negative values, these are made positivepersonal_consumption
retains only positive values.household_supply
retains only negative values, these are made positive.
WiNDC.calibrate
— Methodcalibrate(data::AbstractNationalTable)
This is currently geared toward calibrating the national dataset. I'll be working to make this be a general calibration function.
Returns a new AbstractNationalTable with the calibrated values and the model.
There are three primary balancing operations:
- Zero Profit - Column sums are equal
- Market Clearance - Row sums are equal
- Margin Balance - The margins balance
The three tax rates are fixed. The tax rates are:
- Output Tax Rate
- Absorption Tax Rate
- Import Tariff Rate
The following are fixed:
- Labor Compensation
- Imports
- Exports
- Household Supply
Any zero values will remain zero.
WiNDC.calibrate
— Methodcalibrate(data::T; silent = false, lower_bound = .01, upper_bound = 10)
General calibration functions. Handles variable creation, objective function, optimization and post-processing.
Required Arguments
data::T
: A table of data to calibrate.
Optional Arguments
silent::Bool=false
: If true, the optimization solver will not print output.lower_bound::Number=.01
: The lower bound multiplier for the variables.upper_bound::Number=10
: The upper bound multiplier for the variables.
Returns
A tuple of the calibrated data and the optimization model. The calibrated data will have the same type as the input data.
Functions
calibrate_fix_variables
- Fix variables that should not be calibrated. calibrate_constraints
- Add constraints to the model.
WiNDC.calibrate_constraints
— Methodcalibrate_constraints(
M::Model,
data::AbstractNationalTable;
lower_bound = .01,
upper_bound = 10
)
There are three primary balancing operations:
zero_profit
- Column sums are equalmarket_clearance
- Row sums are equalmargin_balance
- The margins balance
Two table sums are bounded by lower_bound
and upper_bound
multipliers. These are:
The three tax rates are fixed. The tax rates are:
WiNDC.calibrate_fix_variables
— Methodcalibrate_fix_variables(M::Model, data::AbstractNationalTable)
Four subtables are exogenous to the model and are fixed. These are:
imports
exports
labor_demand
household_supply
WiNDC.create_national_sets
— Methodcreate_national_sets(
use::XLSX.Worksheet,
supply::XLSX.Worksheet,
set_regions)
This function creates the sets for the detailed national data.
set regions for detailed table
Dict(
"commodities" => ("use", ["A7:B408"], :commodities),
"labor_demand" => ("use", ["A410:B410"], :commodities),
"other_tax" => ("use", ["A411:B411"], :commodities),
"capital_demand" => ("use", ["A412:B412"], :commodities),
"sectors" => ("use", ["C5:ON6"], :sectors),
"personal_consumption" => ("use", ["OP5:OP6"], :sectors),
"household_supply" => ("use", ["OP5:OP6"], :sectors),
"exports" => ("use", ["OV5:OV6"], :sectors),
"exogenous_final_demand" => ("use", ["OQ5:OU6","OW5:PH6"], :sectors),
"imports" => ("supply", ["OP5:OP6"], :sectors),
"margin_demand" => ("supply", ["OS5:OT6"], :sectors),
"margin_supply" => ("supply", ["OS5:OT6"], :sectors),
"duty" => ("supply", ["OV5:OV6"], :sectors),
"tax" => ("supply", ["OW5:OW6"], :sectors),
"subsidies" => ("supply", ["OX5:OX6"], :sectors)
)
WiNDC.create_national_subtables
— Methodcreate_national_subtables(sets)
This function creates the subtables for the detailed national data.
WiNDC.detailed_summary_map
— Methoddetailed_summary_map(detailed_path)
This function reads the detailed table and returns a DataFrame that maps the detailed sectors to the summary sectors. The first sheet of the detailed table is a map between the detailed sectors and the summary sectors. In addition this maps value added, final demand and supply extras to the summary sectors.
WiNDC.disaggregate_national_to_state
— Methoddisaggregate_national_to_state(
data::NationalTable,
data_path::String,
files::Vector{Tuple{String,String}},
summary_map;
industry_codes = "industry_codes.csv",
state_fips = "state_fips.csv",
aggregation = :detailed
)
This function disaggregates the national data to the state level. It takes in the national data, the path to the state data, the files that contain the state data, a summary map that maps the national data to the state data, and the industry codes. It returns a StateTable.
WiNDC.domain
— Methoddomain(data::T) where T<:WiNDCtable
Return the domain of the WiNDCtable object. Must be implemented for any subtype of WiNDCtable. Will throw an error if not implemented.
Required Arguments
data
- A WiNDCtable-like object.
Output
Returns a vector of symbols representing the domain of the WiNDCtable object.
WiNDC.down_fill
— Methoddown_fill(X)
This function fills in the missing values in a column with the last non-missing value.
WiNDC.fetch_supply_use
— Methodfetch_supply_use(
;
url::String = "https://apps.bea.gov/industry/iTables%20Static%20Files/AllTablesSUP.zip",
output_path::String = joinpath(pwd(), "data/national"),
)
Fetch the supply and use tables from the BEA website. The data is stored in a zip file, which is downloaded and extracted to the output_path
. The extracted files are then returned as a vector of strings.
Optional Arguments
url::String
: The url of the zip file containing the supply and use tables. Default is
"https://apps.bea.gov/industry/iTables%20Static%20Files/AllTablesSUP.zip".
output_path::String
: The path to save the extracted files. Default is the directorydata/national
Output
Returns a vector of the absolute paths to the extracted files.
WiNDC.filter_to_sut
— Methodfilter_to_sut(x::AbstractString)
Return true
if the string x
corresponds to either a detailed or summary supply or use table. Otherwise, return false
.
WiNDC.get_set
— Methodget_set(data::T) where T<:WiNDCtable
get_set(data::T, set_name::String) where T<:WiNDCtable
get_set(data::T, set_name::Vector{String}) where T<:WiNDCtable
Return the elements of the given sets. If no set is given, return all sets.
Required Arguments
data
- A WiNDCtable-like object.set_name
- A string or vector of strings representing the set names to be extracted.
Returns
Returns a DataFrame with three columns, :element
, :set
and :description
WiNDC.get_subtable
— Methodget_subtable(data::T, subtable::String, column::Vector{Symbol}; negative::Bool = false, keep_all_columns = false) where T<:WiNDCtable
get_subtable(data::T, subtable::String; column::Symbol = :value, output::Symbol = :value, negative = false) where T<:WiNDCtable
get_subtable(data::T, subtable::Vector{String}) where T<:WiNDCtable
Return the subtables requested as a DataFrame
Required Arguments
data
- A WiNDCtable-like object.subtable
- A string or vector of strings representing the subtable names to be extracted.
Optional Arguments
column
- A symbol representing the column to be extracted. Default is:value
.output
- A symbol representing the output column name. Default is:value
.negative
- A boolean representing whether the values should be negated. Default isfalse
.
Returns
Returns a DataFrame with the requested subtables and columns.
WiNDC.get_table
— Methodget_table(data::T) where T<:WiNDCtable
Return the main table of the WiNDCtable object as a DataFrame
Required Arguments
data
- A WiNDCtable-like object.
Output
Returns a DataFrame with columns domain(data)
, subtable
, and value
.
WiNDC.gross_output
— Methodgross_output(data::AbstractNationalTable; column::Symbol = :value, output::Symbol = :value)
Calculate the gross output of the sectors.
Required Arguments
data::AbstractNationalTable
: The national data.
Keyword Arguments
column::Symbol = :value
: The column to be used for the calculation.output::Symbol = :value
: The name of the output column.
Output
Returns a DataFrame with the columns :sectors
and :value
.
Process
\[\sum_{s\in\text{Sectors}} \text{Intermediate_Demand} + \text{Household_Supply} - \text{Margin_Supply}\]
WiNDC.import_tariff_rate
— Methodimport_tariff_rate(data::AbstractNationalTable; column::Symbol = :value, output::Symbol = :value)
Calculate the import tariff rate of the sectors.
Required Arguments
data::AbstractNationalTable
: The national data.
Keyword Arguments
column::Symbol = :value
: The column to be used for the calculation.output::Symbol = :value
: The name of the output column.
Output
Returns a DataFrame with the columns :sectors
and :value
.
WiNDC.load_national_data
— Methodload_national_data(
use::XLSX.XLSXFile,
supply::XLSX.XLSXFile;
table_type = :detailed,
use_range = "A6:PI417",
supply_range = "A6:OZ409"
)
Load the national supply and use tables. This function loads the data from the XLSXFiles and returns a DataFrame with the following columns:
:commodities
: The commodities in the table.:sectors
: The sectors in the table.:value
: The value of the commodity in the sector.:year
: The year of the data.
Required Arguments
use::XLSX.XLSXFile
: The XLSXFile containing the use table.supply::XLSX.XLSXFile
: The XLSXFile containing the supply table.
Optional Arguments
table_type::Symbol = :detailed
: The type of table. Either:detailed
or:summary
.use_range::String = "A6:PI417"
: The range of the use table in the XLSXFile.supply_range::String = "A6:OZ409"
: The range of the supply table in the XLSXFile.
Return
A DataFrame.
WiNDC.load_national_data_single_year
— Methodload_national_data_single_year(
X::XLSX.XLSXFile,
year,
range,
table_name::String;
scale = 1_000,
data_start_row = 2
)
Load a single year of national data. This function is used to load data from the supply and use tables. The data is transformed into a DataFrame with the following columns:
:commodities
: The commodities in the table.:sectors
: The sectors in the table.:value
: The value of the commodity in the sector.:year
: The year of the data.:table
: The name of the table.
Required Arguments
X::XLSX.XLSXFile
: The XLSXFile containing the data.year
: The year of the data.range
: The range of the data in the XLSXFile.table_name::String
: The name of the table. Usually "use" or "supply".
Optional Arguments
scale::Int = 1_000
: The scale of the data.data_start_row::Int = 2
: The row in the XLSXFile where the data starts. Summary tables start at 3, detailed at 2.
Return
A DataFrame.
WiNDC.load_table
— Methodload_table(
file_path::String
years::Int...;
)
Load a WiNDCtable
from a file.
Required Arguments
file_path::String
: The path to the file.years::Int...
: The years to load. If no years are provided, all years in the file will be loaded.
Returns
A subtype of a WiNDCtable, with the data and sets loaded from the file.
WiNDC.make_subtable
— Methodmake_subtable(sets, rows, columns, table, subtable)
A helper function for extracting subtables.
WiNDC.margin_balance
— Methodmargin_balance(data::AbstractNationalTable; column = :value, output = :margin_balance)
Calculate the margin balance condition for the given data. In a calibrated dataset all values will be zero.
Required Arguments
data
- A WiNDCtable-like object.
Optional Arguments
column::Symbol
: The column to use for the calculation. Default is:value
.output::Symbol
: The name of the output column. Default is:margin_balance
.
Output
Returns a DataFrame with the margin balance condition.
WiNDC.market_clearance
— Methodmarket_clearance(data::AbstractNationalTable; column = :value, output = :market_clearance)
Calculate the market clearance condition for the given data. In a calibrated dataset all values will be zero.
Required Arguments
data
- A WiNDCtable-like object.
Optional Arguments
column::Symbol
: The column to use for the calculation. Default is:value
.output::Symbol
: The name of the output column. Default is:market_clearance
.
Output
Returns a DataFrame with the market clearance condition.
WiNDC.national_mpsge
— Methodnational_mpsge(data::NationalTable)
Create a MPSGE model from the given NationalTable object.
Required Arguments
data
- A NationalTable object.
Output
Returns a MPSGEModel object.
To Do:
Describe Model
WiNDC.other_tax_rate
— Methodother_tax_rate(data::AbstractNationalTable; column = :value, output = :value)
Calculate the other tax rate of the sectors.
Required Arguments
data::AbstractNationalTable
: The national data.
Keyword Arguments
column::Symbol = :value
: The column to be used for the calculation.output::Symbol = :value
: The name of the output column.
Output
Returns a DataFrame with the columns :sectors
and :value
.
WiNDC.save_table
— Methodsave_table(
output_path::String
MU::T;
overwrite::Bool = false
) where T<:WiNDCtable
Save a WiNDCtable
to a file. The file format is HDF5, which can be opened in any other language. The file will have the following structure:
year - DataFrame - The data for each year in the WiNDCtable
sets - DataFrame - The sets of the WiNDCtable
columns - Array - The column names of each yearly DataFrame
Required Arguments
output_path::String
: The path to save the file. Must end in .jld2.MU::WiNDCtable
: TheWiNDCtable
to save.
Optional Arguments
overwrite::Bool
: If true, overwrite the file if it already exists. Default is false.
WiNDC.weight_function
— Methodweight_function(year_detail, year_summary, minimum_detail, maximum_detail)
Create the weight function for the interpolation of the detailed table to the summary table based solely on the year.
WiNDC.zero_profit
— Methodzero_profit(data::AbstractNationalTable; column = :value, output = :zero_profit)
Calculate the zero profit condition for the given data. In a calibrated dataset all values will be zero.
Required Arguments
data
- A WiNDCtable-like object.
Optional Arguments
column::Symbol
: The column to use for the calculation. Default is:value
.output::Symbol
: The name of the output column. Default is:zero_profit
.
Output
Returns a DataFrame with the zero profit condition.