Stock to flow · stock_to_flow

🟢 3 of 3 forms all ok · VTL 2.2 Reference Manual ↗ · vtlengine 1.10.0rc3 (9e1c81a6) · pandas 2.3.3 · DuckDB 1.5.5
🟢 all ok🟡 partially implemented🛡️ refused with an error🔴 not implemented or crash🔵 engines differ ✅ match · ❌ wrong result · 🛡 guarded error (code) · 💥 crash · 📌 team decision
        ---
config:
  flowchart:
    useMaxWidth: false

---
flowchart LR
    classDef ok fill:#d4edda,stroke:#28a745,color:#000
    classDef partial fill:#fff3cd,stroke:#ffc107,color:#000
    classDef refused fill:#d4edda,stroke:#28a745,stroke-width:3px,stroke-dasharray: 2 2,color:#000
    classDef missing fill:#f8d7da,stroke:#dc3545,color:#000
    classDef diverge fill:#cce5ff,stroke:#007bff,color:#000
    classDef block fill:#2b3a4a,stroke:#2b3a4a,color:#fff,font-weight:bold
    classDef ext fill:#fff,stroke:#666,color:#000,stroke-dasharray: 5 5
    O["Stock to flow · stock_to_flow<br/>🟢3"]:::ok
    v_time_period["stock_to_flow(DS) on time_period"]:::ok
    v_date["stock_to_flow(DS) on date"]:::ok
    v_time["stock_to_flow(DS) on time"]:::ok
    v_time_period --- O
    v_date --- O
    O --- v_time
    click v_time_period href "#form-time-period"
    click v_date href "#form-date"
    click v_time href "#form-time"
    

Types

Takes number, time, date, time_period · Gives number, time, date, time_period

Comes from · number, time, date, time_period — 49 operators in 6 blocks 🟢49
General purpose operators (2) 🟢2
String operators (3) 🟢3
Numeric operators (18) 🟢18
Time operators (13) 🟢13
Aggregate and Analytic operators (10) 🟢10
Data validation operators (3) 🟢3
Comes from · operators that return any type (their input's) — 34 operators in 7 blocks 🟢34
General purpose operators (5) 🟢5
Join operators (4) 🟢4
Set operators (4) 🟢4
Hierarchical aggregation (1) 🟢1
Aggregate and Analytic operators (8) 🟢8
Conditional operators (3) 🟢3
Clause operators (9) 🟢9
Goes to · number, time, date, time_period — 43 operators in 7 blocks 🟢43
General purpose operators (1) 🟢1
Numeric operators (18) 🟢18
Time operators (11) 🟢11
Hierarchical aggregation (1) 🟢1
Aggregate and Analytic operators (8) 🟢8
Data validation operators (2) 🟢2
Definitions (VTL-DL) (2) 🟢2
Goes to · operators that take any scalar — 48 operators in 8 blocks 🟢48
General purpose operators (6) 🟢6
Join operators (4) 🟢4
Comparison operators (11) 🟢11
Set operators (4) 🟢4
Aggregate and Analytic operators (10) 🟢10
Data validation operators (1) 🟢1
Conditional operators (3) 🟢3
Clause operators (9) 🟢9

Forms

stock_to_flow(DS) on time_period 🟢 · time_period, number → time_period, number

Manual example 3 🟢 pandas ✅ · DuckDB ✅
Tests:

stock_to_flow(DS_3) on annual time_period series.

Manual:

Stock to flow, Example 3, output verbatim: “for each time series, calculates the corresponding flow data by subtracting from the measure values of each regular period the corresponding measure values of the previous one.”

Expected:

The first value of each series, then the differences: A 2.0, 5.0, -3.0, 9.0 and B 4.0, -8.0, 0.0, 6.0, Numbers.

script.vtl
DS_r := stock_to_flow ( DS_3 );
Input DS_3
Id_1Id_2Me_1
A20102.0
A20117.0
A20124.0
A201313.0
B20104.0
B2011-4.0
B2012-4.0
B20132.0
Expected DS_r
Id_1Id_2Me_1
A20102.0
A20115.0
A2012-3.0
A20139.0
B20104.0
B2011-8.0
B20120.0
B20136.0

Files: script.vtl · input/DS_3.csv · input/DS_3.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

Manual example 4 🟢 pandas ✅ · DuckDB ✅
Tests:

stock_to_flow(DS_4) on an annual and a quarterly series of the same phenomenon A, each on its own.

Manual:

Stock to flow, Example 4, output verbatim: “for each time series, calculates the corresponding flow data by subtracting from the measure values of each regular period the corresponding measure values of the previous one.”

Expected:

The years give 2.0, 7.0, 4.0, 13.0 and the quarters of 2010 give 2.0, -3.0, 7.0, -4.0.

script.vtl
DS_r := stock_to_flow ( DS_4 );
Input DS_4
Id_1Id_2Me_1
A20102.0
A20119.0
A201213.0
A201326.0
A2010Q12.0
A2010Q2-1.0
A2010Q36.0
A2010Q42.0
Expected DS_r
Id_1Id_2Me_1
A20102.0
A20117.0
A20124.0
A201313.0
A2010Q12.0
A2010Q2-3.0
A2010Q37.0
A2010Q4-4.0

Files: script.vtl · input/DS_4.csv · input/DS_4.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

stock_to_flow(DS) on date 🟢 · date, number → date, number

Manual example 2 🟢 pandas ✅ · DuckDB ✅
Tests:

stock_to_flow(DS_2) on annual date series, where “conventionally each period is identified by its last day”.

Manual:

Stock to flow, Example 2, output verbatim: “for each time series, calculates the corresponding flow data by subtracting from the measure values of each regular period the corresponding measure values of the previous one.”

Expected:

The first value of each series, then the differences: A 2.0, 5.0, -3.0, 9.0 and B 4.0, -8.0, 0.0, 6.0.

script.vtl
DS_r := stock_to_flow ( DS_2 );
Input DS_2
Id_1Id_2Me_1
A2010-12-312.0
A2011-12-317.0
A2012-12-314.0
A2013-12-3113.0
B2010-12-314.0
B2011-12-31-4.0
B2012-12-31-4.0
B2013-12-312.0
Expected DS_r
Id_1Id_2Me_1
A2010-12-312.0
A2011-12-315.0
A2012-12-31-3.0
A2013-12-319.0
B2010-12-314.0
B2011-12-31-8.0
B2012-12-310.0
B2013-12-316.0

Files: script.vtl · input/DS_2.csv · input/DS_2.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

stock_to_flow(DS) on time 🟢 · time, number → time, number

Manual example 1 🟢 pandas ✅ · DuckDB ✅
Tests:

stock_to_flow(DS_1) on annual series whose reference time identifier is a time.

Manual:

Stock to flow, Example 1: the operator “calculates the corresponding flow data by subtracting from the measure values of each regular period the corresponding measure values of the previous one.”

Expected:

The manual’s values: A 2.0, 5.0, -3.0, 9.0 and B 4.0, -8.0, 0.0, 6.0, a Number Me_1.

Erratum:

The manual’s input structure declares Me_1 as String, but the operand’s measures are measure<number>, the values are numbers, and the manual’s output and its DS_2 to DS_4 have a Number Me_1: the input Me_1 is Number.

Adaptation:

The page writes its time values in “the time format YYYY-MM/YYYY-MM”, as 2010M1/2010M12, which vtlengine cannot load (DataLoadError 0-3-1-6 on both engines). Each YYYYM1/YYYYM12 of the input and the output is written YYYY-01-01/YYYY-12-31, the same interval in the YYYY-MM-DD/YYYY-MM-DD form of the manual’s “Behaviour of Time operators” and of docs/data_types.rst; nothing else changes.

script.vtl
DS_r := stock_to_flow ( DS_1 );
Input DS_1
Id_1Id_2Me_1
A2010-01-01/2010-12-312.0
A2011-01-01/2011-12-317.0
A2012-01-01/2012-12-314.0
A2013-01-01/2013-12-3113.0
B2010-01-01/2010-12-314.0
B2011-01-01/2011-12-31-4.0
B2012-01-01/2012-12-31-4.0
B2013-01-01/2013-12-312.0
Expected DS_r
Id_1Id_2Me_1
A2010-01-01/2010-12-312.0
A2011-01-01/2011-12-315.0
A2012-01-01/2012-12-31-3.0
A2013-01-01/2013-12-319.0
B2010-01-01/2010-12-314.0
B2011-01-01/2011-12-31-8.0
B2012-01-01/2012-12-310.0
B2013-01-01/2013-12-316.0

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗