gdrsclib
Functions
gdrs.c File Reference
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include "gdrs.h"

Go to the source code of this file.

Functions

void handle_sql3error (sqlite3 *db, sqlite3_stmt *stmt)
 Function that wraps repetitive code for SQLite3 errors. More...
 
int loadgdrs (struct gdrs_struct *gdrs, const char *fname)
 Function to translate from SQLite3 database to internal data structures. More...
 
int readtable (sqlite3 *db, const char *column, struct datablock *table, struct appdata ad)
 Helper function to read a column from the "core" table into a datablock. More...
 
void cleargdrs (struct gdrs_struct gdrs)
 Clear out the GDRs data structure. More...
 
double globaldist (double y, int year, struct gdrs_struct *gdrs, int ppp_or_mer, double *dist_deriv)
 Calculate the fraction of global population below a specified income level in the given year. More...
 
double globalquintile (double q, int year, struct gdrs_struct *gdrs, int ppp_or_mer)
 Calculate the income at a given quintile for the world as a whole. More...
 
double nationalquintile (double q, double ybar, double sigma)
 Calculate the income at a given quintile for a country. More...
 
void combine_gap (struct fixedfactor *gap, int i, double newgap, int combine)
 Calculate and store luxury emissions. More...
 
void calc_kab (struct fixedfactor *gap, double *totgap, int year, int period, int combine, struct gdrs_struct *gdrs)
 Calculate and store "Kyoto gap" for Kyoto-adjusted baselines. More...
 
void calc_luxemiss (struct fixedfactor *gap, double *totgap, int year, int period, int combine, struct gdrs_struct *gdrs)
 Calculate and store luxury emissions. More...
 
void calcallocs (struct gdrs_struct *gdrs)
 Calculate allocations for all years. More...
 
void nextrci (int year, struct gdrs_struct *gdrs)
 Calculate the GDRs Responsibility-Capacity Indicator for a particular year. More...
 
double M (double a, double zc, double ybar, double sigma)
 Calculate a function \(M_a(y_c,\bar{y},\sigma)\) that is repeatedly used in calculations. More...
 
double a_gamma (double gamma, double y, double a, double b)
 Calculate the \(a_\gamma(y;a,b)\) function. More...
 
double b_gamma (double gamma, double y, double b)
 Calculate the \(b_\gamma(y;b)\) function. More...
 
double A_gamma (double gamma, double za, double zb, double yl, double yu, double ybar, double sigma)
 Calculate the \(A_\gamma(a,b;y_l,y_u/\pi_i,\bar{y}_i,\sigma_i)\) function. More...
 
double B_gamma (double gamma, double za, double zb, double yu, double ybar, double sigma)
 Calculate the \(B_\gamma(a,b;y_u/\pi_i,\bar{y}_i,\sigma_i)\) function. More...
 
double capbetween (double za, double zb, double zl, double zu, double yl, double yu_ppp, double ybar, double sigma, double pi, double luxmult)
 Calculate capacity per capita between two income limits. More...
 
double respbetween (double gamma, double za, double zb, double zl, double zu, double yl, double yu_ppp, double ybar, double sigma, double A)
 Calculate responsibility between two income limits. More...
 
double rhat (double E, double gamma, double sigma, double ybar)
 Calculate the factor \(\hat{r}_i\) for a country. More...
 
double c_indiv (double y, double yl, double yu, int do_interp)
 Calculate individual per capita capacity \(c_i(y)\) at a specified income level. More...
 
double r_indiv (double gamma, double y, double yl, double yu, double A, int do_interp)
 Calculate individual per capita annual responsibility \(r_i(y)\) at a specified income level. More...
 
double luxcap (int year, struct gdrs_struct *gdrs)
 If calculating luxury-capped baselines adjust the luxury threshold if needed to stay above emergency pathway. More...
 
void calcybar (struct gdrs_struct *gdrs)
 Calculate mean income from GDP and population. More...
 
struct datablock z (double y, struct gdrs_struct *gdrs, int ppp_or_mer)
 Calculate the "z" transformed version of an income level for all countries and years. More...
 
int dump2sql3 (const char *fname, struct gdrs_struct gdrs)
 After the calculation, write all results to the SQLite3 database. More...
 

Function Documentation

◆ a_gamma()

double a_gamma ( double  gamma,
double  y,
double  a,
double  b 
)

Calculate the \(a_\gamma(y;a,b)\) function.

Parameters
gammaElasticity
yIncome (PPP) at which to evaluate the function
aLower income limit
bUpper income limit
Returns

\[0 \mbox{ if } |a - b| < \epsilon\]

or

\[a_\gamma(y;a,b) = \frac{1}{b - a}\left[\frac{\gamma}{\gamma +1}\left(y^{\gamma+1}-a^{\gamma+1}\right)-a\left(y^\gamma - a^\gamma\right)\right],\]

Note
See Technical report.
All incomes are assumed to be in PPP terms.
Todo:
Check input values

Definition at line 1299 of file gdrs.c.

◆ A_gamma()

double A_gamma ( double  gamma,
double  za,
double  zb,
double  yl,
double  yu,
double  ybar,
double  sigma 
)

Calculate the \(A_\gamma(a,b;y_l,y_u/\pi_i,\bar{y}_i,\sigma_i)\) function.

Parameters
gammaElasticity
zaLower income limit (transformed variable)
zbUpper income limit (transformed variable)
ylDevelopment threshold
yuLuxury threshold
ybarMean income for the country
sigmaStandard deviation of log income for the country
Returns

\[0 \mbox{ if } |y_u - y_l| < \epsilon\]

or

\[A_\gamma(a,b) = \frac{1}{y/\pi_i-y_l}\left[ \frac{\gamma}{\gamma+1}\left(M_{\gamma+1}(a) - M_{\gamma+1}(b)\right) - y_l\left(M_\gamma(a) - M_\gamma(b)\right) + \frac{y_l^{\gamma+1}}{\gamma+1} \left(M_0(b) - M_0(a)\right)\right],\]

Note
See Technical report.
All incomes are assumed to be in PPP terms; this means that the luxury threshold has already been converted to PPP terms.
Values for z's and y's can be positive infinity, and z's can be negative infinity.
Todo:
Check input values

Definition at line 1347 of file gdrs.c.

◆ b_gamma()

double b_gamma ( double  gamma,
double  y,
double  b 
)

Calculate the \(b_\gamma(y;b)\) function.

Parameters
gammaElasticity
yIncome (PPP) at which to evaluate the function
bUpper income limit
Returns

\[b_\gamma(y;b) = y^\gamma - b^\gamma.\]

Note
See Technical report.
All incomes are assumed to be in PPP terms.
Todo:
Check input values

Definition at line 1325 of file gdrs.c.

◆ B_gamma()

double B_gamma ( double  gamma,
double  za,
double  zb,
double  yu,
double  ybar,
double  sigma 
)

Calculate the \(B_\gamma(a,b;y_u/\pi_i,\bar{y}_i,\sigma_i)\) function.

Parameters
gammaElasticity
zaLower income limit (transformed variable)
zbUpper income limit (transformed variable)
yuLuxury threshold
ybarMean income for the country
sigmaStandard deviation of log income for the country
Returns

\[B_\gamma(a,b) = \left(M_\gamma(a)-M_\gamma(b)\right) - \left(\frac{y_u}{\pi_i}\right)^\gamma\left(M_0(a) - M_0(b)\right),\]

Note
See Technical report.
All incomes are assumed to be in PPP terms; this means that the luxury threshold has already been converted to PPP terms.
Values for z's and y's can be positive infinity, and z's can be negative infinity.
Todo:
Check input values

Definition at line 1379 of file gdrs.c.

◆ c_indiv()

double c_indiv ( double  y,
double  yl,
double  yu,
int  do_interp 
)

Calculate individual per capita capacity \(c_i(y)\) at a specified income level.

Parameters
yIncome at which to calculate c_indiv
ylDevelopment threshold
yuLuxury threshold
do_interpBoolean: are we interpolating between thresholds?
Returns
Individual capacity per capita at income $y$
Note
All incomes are in MER terms.

Definition at line 1519 of file gdrs.c.

◆ calc_kab()

void calc_kab ( struct fixedfactor gap,
double *  totgap,
int  year,
int  period,
int  combine,
struct gdrs_struct gdrs 
)

Calculate and store "Kyoto gap" for Kyoto-adjusted baselines.

Parameters
gapDifference between nominal baseline and luxury-adjusted baseline (if doing luxury-capping)
totgapSum of gaps across all countries
yearCurrent year for the calculation
periodFor sequencing: PER_BEFORE_EP, PER_SEQUENCE, or PER_GDRS
combineIf more than one gap, how are they being combined? GAP_COMBINE_NONE, GAP_COMBINE_ADD, GAP_COMBINE_MAX, GAP_COMBINE_MIN
gdrsThe global data structure for the GDRs calculation

Definition at line 812 of file gdrs.c.

◆ calc_luxemiss()

void calc_luxemiss ( struct fixedfactor gap,
double *  totgap,
int  year,
int  period,
int  combine,
struct gdrs_struct gdrs 
)

Calculate and store luxury emissions.

Parameters
gapDifference between nominal baseline and luxury-adjusted baseline (if doing luxury-capping)
totgapSum of gaps across all countries
yearCurrent year for the calculation
periodFor sequencing: PER_BEFORE_EP, PER_SEQUENCE, or PER_GDRS
combineIf more than one gap, how are they being combined? GAP_COMBINE_NONE, GAP_COMBINE_ADD, GAP_COMBINE_MAX, GAP_COMBINE_MIN
gdrsThe global data structure for the GDRs calculation

Set gdrs.lux_emiss, gdrs.lux_emiss_applied

Definition at line 892 of file gdrs.c.

◆ calcallocs()

void calcallocs ( struct gdrs_struct gdrs)

Calculate allocations for all years.

Parameters
gdrsThe global data structure for the GDRs calculation Set gdrs.a1domrdxn, gdrs.alloc_gdrs

Definition at line 947 of file gdrs.c.

◆ calcybar()

void calcybar ( struct gdrs_struct gdrs)

Calculate mean income from GDP and population.

Parameters
gdrsGlobal data structure with data, parameters, etc. Set gdrs.ybar_ppp

Definition at line 1714 of file gdrs.c.

◆ capbetween()

double capbetween ( double  za,
double  zb,
double  zl,
double  zu,
double  yl,
double  yu_ppp,
double  ybar,
double  sigma,
double  pi,
double  luxmult 
)

Calculate capacity per capita between two income limits.

Parameters
zaLower income limit (transformed variable)
zbUpper income limit (transformed variable)
zlDevelopment threshold (transformed variable)
zuLuxury threshold (transformed variable)
ylDevelopment threshold
yu_pppLuxury threshold in PPP terms
ybarMean income for the country
sigmaStandard deviation of log income for the country
piInverse of ppp2mer ratio
luxmultTax multiplier on high incomes
Returns
Average per capita contribution to national capacity from those with incomes between the income limits
Note
All incomes are assumed to be in PPP terms.
The return value must be multiplied by national population.
There is a lot of redundancy in the passed parameters, but for speed these are calculated once in function next_rci.
Values for z's and y's can be positive infinity, and z's can be negative infinity.

Definition at line 1408 of file gdrs.c.

◆ cleargdrs()

void cleargdrs ( struct gdrs_struct  gdrs)

Clear out the GDRs data structure.

Parameters
gdrsThe main data structure, containing data, information, and calculated outputs

Definition at line 621 of file gdrs.c.

◆ combine_gap()

void combine_gap ( struct fixedfactor gap,
int  i,
double  newgap,
int  combine 
)

Calculate and store luxury emissions.

Parameters
gapDifference between nominal baseline and luxury-adjusted baseline (if doing luxury-capping)
iValue in gap to set
newgapNew value for entry i
combineIf more than one gap, how are they being combined? GAP_COMBINE_NONE, GAP_COMBINE_ADD, GAP_COMBINE_MAX, GAP_COMBINE_MIN

Definition at line 782 of file gdrs.c.

◆ dump2sql3()

int dump2sql3 ( const char *  fname,
struct gdrs_struct  gdrs 
)

After the calculation, write all results to the SQLite3 database.

Parameters
fnameFilename for the SQLite3 database
gdrsGlobal structure with data and parameters
Returns
Success code Add the table "gdrs" to the SQLite3 database

Definition at line 1800 of file gdrs.c.

◆ globaldist()

double globaldist ( double  y,
int  year,
struct gdrs_struct gdrs,
int  ppp_or_mer,
double *  dist_deriv 
)

Calculate the fraction of global population below a specified income level in the given year.

Parameters
yIncome level below which to calculate the share of the global population
yearYear to report population share
gdrsThe global data structure for the GDRs calculation
ppp_or_merWhether using PPP or MER conversion rates
dist_derivPointer to a variable to store the derivative of the function (for finding quintiles) or NULL
Returns
Fraction of the population below the specified income level
Note
Before this is run, ybar, sigma, and scalpop should be calculated.

Definition at line 687 of file gdrs.c.

◆ globalquintile()

double globalquintile ( double  q,
int  year,
struct gdrs_struct gdrs,
int  ppp_or_mer 
)

Calculate the income at a given quintile for the world as a whole.

Parameters
qThe quintile (e.g., 0.5 for the median)
yearThe year in which to calculate the quintile
gdrsThe global data structure for the GDRs calculation
ppp_or_merWhether using PPP or MER conversion rates
Returns
The PPP income level for the corresponding global quintile
Note
Use Newton's method.

Definition at line 739 of file gdrs.c.

◆ handle_sql3error()

void handle_sql3error ( sqlite3 db,
sqlite3_stmt stmt 
)

Function that wraps repetitive code for SQLite3 errors.

This file handles the implementation of the GDRs algorithms

Parameters
dbAn sqlite3 database handle
stmtThe return value from an SQL3 call

Definition at line 38 of file gdrs.c.

◆ loadgdrs()

int loadgdrs ( struct gdrs_struct gdrs,
const char *  fname 
)

Function to translate from SQLite3 database to internal data structures.

Parameters
gdrsThe data structure to fill
fnameFile name of the SQLite3 database
Returns
Success code Set most values in the gdrs structure
Todo:
Implement percentiles

Definition at line 54 of file gdrs.c.

◆ luxcap()

double luxcap ( int  year,
struct gdrs_struct gdrs 
)

If calculating luxury-capped baselines adjust the luxury threshold if needed to stay above emergency pathway.

Parameters
yearYear for calculating the adjusted luxury cutoff
gdrsGlobal data structure containing parameters and data for all calculations
Returns
Value of the luxury-capped baseline, or NaN Set gdrs.yu_adj and gdrs.zu_adj

Definition at line 1572 of file gdrs.c.

◆ M()

double M ( double  a,
double  zc,
double  ybar,
double  sigma 
)

Calculate a function \(M_a(y_c,\bar{y},\sigma)\) that is repeatedly used in calculations.

Parameters
aElasticity
zcIncome cutoff, transformed into "z" variable \( z = \frac{1}{\sigma} \ln\left(y/\bar y\right) + \frac{1}{2}\sigma\)
ybarMean income
sigmaStandard deviation of log income
Returns

\[M_a(y_c,\sigma,\bar y) = \bar{y}^a e^{\frac{\sigma^2}{2} a (a - 1)} \left[1 - N(z_c - a \sigma)\right],\]

where \(N(\cdot)\) is the cumulative standardized normal distribution.
Note
See Equation (7) in the technical report

\[M_a(k y_c,\sigma, k\bar y) = k^aM_a(y_c, \sigma, \bar y)\]

Definition at line 1270 of file gdrs.c.

◆ nationalquintile()

double nationalquintile ( double  q,
double  ybar,
double  sigma 
)

Calculate the income at a given quintile for a country.

Parameters
qThe quintile (e.g., 0.5 for the median, 0.2 for the bottom 20%, 0.8 for the top 20%)
ybarMean income
sigmaStandard deviation of log income
Returns
Income level for that quintile, assuming a lognormal distribution; value is

\[y = \bar{y} e^{\sigma N^{-1}(q) - \sigma^2/2}\]

Definition at line 769 of file gdrs.c.

◆ nextrci()

void nextrci ( int  year,
struct gdrs_struct gdrs 
)

Calculate the GDRs Responsibility-Capacity Indicator for a particular year.

Parameters
yearYear for which to calculate RCI
gdrsGlobal data structure containing parameters and data Set gdrs.c, gdrs.r, gdrs.pop_above_dl, gdrs.pop_above_lux, gdrs.rci, gdrs.tax_revenue_mer_dens, tax_revenue_ppp_dens, gdrs.tax_pop_dens, gdrs.tax_income_mer_dens, tax_income_ppp_dens, gdrs.tax_pop_below
Note
IMPORTANT: alloc_gdrs for prev year is expected to be set by the calling function unless it is the first year

Definition at line 1107 of file gdrs.c.

◆ r_indiv()

double r_indiv ( double  gamma,
double  y,
double  yl,
double  yu,
double  A,
int  do_interp 
)

Calculate individual per capita annual responsibility \(r_i(y)\) at a specified income level.

Parameters
gammaEmissions elasticity
yIncome at which to calculate r_indiv
ylDevelopment threshold
yuLuxury threshold
AOverall coefficient (factor \(\hat{r}_i\))
Returns
Individual annual responsibility per capita at income \(y\)
Note
All incomes are in PPP terms

Definition at line 1546 of file gdrs.c.

◆ readtable()

int readtable ( sqlite3 db,
const char *  column,
struct datablock table,
struct appdata  ad 
)

Helper function to read a column from the "core" table into a datablock.

Parameters
dbHandle for the SQLite3 database
columnName of the column in the core table of the database
tableInternal data representation
adappdata structure with dimensions
Returns
Success code

Definition at line 576 of file gdrs.c.

◆ respbetween()

double respbetween ( double  gamma,
double  za,
double  zb,
double  zl,
double  zu,
double  yl,
double  yu_ppp,
double  ybar,
double  sigma,
double  A 
)

Calculate responsibility between two income limits.

Parameters
gammaEmissions elasticity
zaLower income limit (transformed variable)
zbUpper income limit (transformed variable)
zlDevelopment threshold (transformed variable)
zuLuxury threshold (transformed variable)
ylDevelopment threshold
yu_pppLuxury threshold in PPP terms
ybarMean income for the country
sigmaStandard deviation of log income for the country
AOverall multiplicative constant to convert to emissions
Returns
Contribution to national responsibility from those with incomes between the income limits
Note
All incomes are assumed to be in PPP terms.
There is a lot of redundancy in the passed parameters, but for speed these are calculated once in function next_rci.
Values for z's and y's can be positive infinity, and z's can be negative infinity.

Definition at line 1459 of file gdrs.c.

◆ rhat()

double rhat ( double  E,
double  gamma,
double  sigma,
double  ybar 
)

Calculate the factor \(\hat{r}_i\) for a country.

Parameters
EAnnual emissions
gammaEmissions elasticity
sigmaStandard deviation of the log of income
ybarMean income
Returns
The factor

\[\hat{r}_i=\frac{E_{i}}{{\bar{y}_{i}}^{\gamma }}e^{-{\frac{\sigma _{i}^{2}}{\;2}}\gamma (\gamma -1)}\]

Note
Mean income ybar is assumed to be in PPP terms where this factor appears in formulas.

Definition at line 1503 of file gdrs.c.

◆ z()

struct datablock z ( double  y,
struct gdrs_struct gdrs,
int  ppp_or_mer 
)

Calculate the "z" transformed version of an income level for all countries and years.

Parameters
yThe income at which to calculate z
gdrsGlobal data structure with variables and parameters
ppp_or_merWhether using PPP or MER conversion rates
Returns
Value of z
Note
\( z = \frac{1}{\sigma} \ln\left(y/\bar y\right) + \frac{1}{2}\sigma\)

Definition at line 1754 of file gdrs.c.