Snippets
Series
Create
Create a series
# Copyright © Ferra Solutions (Pty) Ltd. All rights reserved.
# Shows how to create a series,
# load it with observations and
# do simple calculations with it
# First observation is in 2010
# Actual indexing depends on
# frequency
# Calculate this with different
# frequencies to see how it works
mySeries = observe(
dateIndex( 2010, 1, 1 ),
1, 2, 3, 4, 5, 6, 7, 8 );
# Difference in natural log
# These three calculations are
# all exactly the same
# Note how lags are calculated
# in the third series
msdl1 = dLn( mySeries );
msdl2 = d( ln( mySeries ) );
msdl3 = ln( mySeries ) -
ln( mySeries(-1) );
# Print results, see log for output
print( msdl1, msdl2, msdl3 );
Calculation
Series calculation
# Copyright © Ferra Solutions (Pty) Ltd. All rights reserved.
# Simple time series calculation
# Calculate real GDP for the US
# Using annual nominal GDP and
# monthly CPI
# Calculate real GDP as nominal
# GDP divided by CPI
# Load series from FRED, this may
# take a while
NomGDP = dataFred( "GDPA" );
CPI = dataFred( "CPIAUCSL" );
# Calculate real GDP
RealGDP = NomGDP / CPI;
# Set formatting to fixed and
# with a '%' postfix
formatFix();
format( 2, ., "", "%" );
# Digits 2,
# length (missing, so use
# default),
# prefix (empty string) and
# postfix (%)
# Print annual percentage change
# See log for details
print( 100 * py( RealGDP ) );
# Reset format
formatReset();
US CPI
CPI Report
# Copyright © Ferra Solutions (Pty) Ltd. All rights reserved.
# Report US CPI
# in a table
# Load data
# To e.g. load SA CPI, use
# CPI = dataStatsSa( "P0141",
# "CPA00000" );
CPI = dataFred( "CPIAUCSL" );
# Set some properties
setProperty( "CPI",
"code",
"US CPI" );
# Load recession indicator
REC = dataFred( "USARECM" );
# Create report
# Note that this will create
# a new report.
# First we delete the old one,
# if any. Be careful, this will
# delete the report if it
# exists.
reportDelete( "US CPI" );
reportAdd( "US CPI",
"US CPI Report", "",
"TableCycle", CPI, REC );
SA Buildings
Buidling statistics report
# Copyright © Ferra Solutions (Pty) Ltd. All rights reserved.
# Report building statistics
# in a table
# Load data from STATSSA
# Plans passed in large munis
PLANS = dataStatsSa( "P5041.1",
"GS000003" );
# Set some properties
setProperty( "PLANS",
"code",
"Plans passed" );
# Capture recession indicator
# The values here are from the
# SARB quarterly bulletin
# and capture the historical
# downward phases of the local
# economy
REC[dateIndex( 1946, 9, 1 )] = 1;
REC[dateIndex( 1947, 5, 1 )] = 0;
REC[dateIndex( 1948, 12, 1 )] = 1;
REC[dateIndex( 1950, 3, 1 )] = 0;
REC[dateIndex( 1952, 1, 1 )] = 1;
REC[dateIndex( 1953, 4, 1 )] = 0;
REC[dateIndex( 1955, 5, 1 )] = 1;
REC[dateIndex( 1956, 10, 1 )] = 0;
REC[dateIndex( 1958, 2, 1 )] = 1;
REC[dateIndex( 1959, 4, 1 )] = 0;
REC[dateIndex( 1960, 5, 1 )] = 1;
REC[dateIndex( 1961, 9, 1 )] = 0;
REC[dateIndex( 1965, 5, 1 )] = 1;
REC[dateIndex( 1966, 1, 1 )] = 0;
REC[dateIndex( 1967, 6, 1 )] = 1;
REC[dateIndex( 1968, 1, 1 )] = 0;
REC[dateIndex( 1971, 1, 1 )] = 1;
REC[dateIndex( 1972, 9, 1 )] = 0;
REC[dateIndex( 1974, 9, 1 )] = 1;
REC[dateIndex( 1978, 1, 1 )] = 0;
REC[dateIndex( 1981, 9, 1 )] = 1;
REC[dateIndex( 1983, 4, 1 )] = 0;
REC[dateIndex( 1984, 7, 1 )] = 1;
REC[dateIndex( 1986, 4, 1 )] = 0;
REC[dateIndex( 1989, 3, 1 )] = 1;
REC[dateIndex( 1993, 6, 1 )] = 0;
REC[dateIndex( 1996, 12, 1 )] = 1;
REC[dateIndex( 1999, 9, 1 )] = 0;
REC[dateIndex( 2007, 12, 1 )] = 1;
REC[dateIndex( 2009, 9, 1 )] = 0;
REC[dateIndex( 2013, 12, 1 )] = 1;
# Create report
# Note that this will create
# a new report.
# First we delete the old one,
# if any. Be careful, this will
# delete the report if it
# exists.
reportDelete( "SA Buildings" );
reportAdd( "SA Buildings",
"SA Buildings Report", "",
"TableCycle", PLANS, REC );
HP filter
Hodrick-Prescott filter
# Copyright © Ferra Solutions (Pty) Ltd. All rights reserved.
# This calculates the HP
# filter and uses that to
# estimate the 'gap' between
# actual and potential GDP
# PS this will take forever
# and also run out of memory
# whichever comes sooner
# PSS Give it 5 minutes at
# least, some crazy big
# matrices used in this
# calculation behind the
# scenes ...
# PSSS You can always touch
# the spinning wheel to
# interrupt an ongoing
# calculation ... this is
# true in most places in fdm
# Download US GDP from FRED
GDP = dataFred( "GDPC1" );
# We will apply the filter to
# log of GDP
lGDP = ln( GDP );
# Calculate potential
lPOT = hpFilter( lGDP );
POT = exp( lPOT );
# Calculate gap
GAP = GDP / POT - 1;
print( GAP * 100 );
# Given the time this takes to
# calculate, one would typically
# want to save it for reuse later
# on, as below
# Note that this will replace any
# series named GAP in the current
# frequency with this one
dataReplace( "GAP", GAP );
Contract adjustment
Annual inflation adjustment
# Copyright © Ferra Solutions (Pty) Ltd. All rights reserved.
# This calculates the annual
# price escalation that should
# be applied to a typical
# contract linked to consumer
# inflation for example.
# Note that this is an example
# and the terms of a specific
# contract may require a
# different calculation.
# This calculates the 12 month
# percentage change in CPI and
# then uses the average of the
# last 12 months of that to
# calculate the proposed
# escalation.
# Load data
# To e.g. load US CPI, use
# CPI = dataFred( "CPIAUCSL" );
# in stead.
# PPI can be used in a similar
# fashion for a contract linked
# to producer inflation.
# Again, contract specifics may
# differ and this is just an
# example and not legal advice.
# Load SA CPI from STATSSA
CPI = dataStatsSa( "P0141",
"CPA00000" );
# Calculate annual (yoy)
# percentage change
yoy = py( CPI );
# Calculate 12 month simple
# moving average
inflation = sma( 12, yoy );
# Format and print result
# Use most recent value or
# use value from contract
# birthday month for example
format( 1 );
print( "Per cent increase",
inflation * 100 );