Baselight
1-- Calculate growth stability metrics for investment risk assessment
2WITH yearly_growth AS (
3    SELECT 
4        economy,
5        year,
6        value as gdp_growth
7    FROM "@worldbank.world_development_indicators.ny_gdp_mktp_kd_zg"
8    WHERE year >= 2010 AND year <= 2024
9        AND economy IN ('CHN', 'IND', 'VNM', 'IDN', 'KOR', 'SGP', 
10                        'USA', 'JPN', 'DEU', 'BRA', 'MEX', 'THA', 'MYS', 'PHL')
11        AND value IS NOT NULL
12),
13growth_stats AS (
14    SELECT 
15        economy,
16        AVG(gdp_growth) as avg_growth,
17        STDDEV(gdp_growth) as volatility,
18        MIN(gdp_growth) as worst_year,
19        MAX(gdp_growth) as best_year,
20        COUNT(*) as data_points,
21        SUM(CASE WHEN gdp_growth < 0 THEN 1 ELSE 0 END) as negative_years
22    FROM yearly_growth
23    GROUP BY economy
24)
25SELECT 
26    gs.*,
27    (gs.avg_growth / NULLIF(gs.volatility, 0)) as sharpe_ratio,
28    (gs.negative_years * 100.0 / gs.data_points) as pct_negative_years,
29    pc.value as latest_gdp_per_capita
30FROM growth_stats gs
31LEFT JOIN "@worldbank.world_development_indicators.ny_gdp_pcap_kd" pc
32    ON gs.economy = pc.economy AND pc.year = 2024
33ORDER BY sharpe_ratio DESC NULLS LAST
economyavg_growthvolatilityworst_yearbest_yeardata_pointsnegative_yearssharpe_ratiopct_negative_yearslatest_gdp_per_capita
VNM6.1247459401192871.6316977079356212.553728526481318.537500467557881503.753603323907433304017.74604111554
CHN6.7374594137993082.17497238237552672.3401884307841410.59627197616821503.0977218232263652013121.6769905565
IDN4.745621119904961.9824379518491297-2.065511829341656.223854180623661512.39383084624588446.6666666666666674367.86266621191
KOR2.9208509553900971.6229495607332793-0.7094153593976816.804824917836711411.799717641298977.142857142857143null
IND6.2374043183219073.666256657195039-5.777724706868019.689592491928751511.70130050935768056.6666666666666672396.71271858102
USA2.40343085656183451.6140693856456876-2.163029138665126.055052933045561511.4890505191016766.66666666666666766682.6147044553
MYS4.5928624650430033.109305395915068-5.456846584267018.861821875780461511.47713456229709646.66666666666666711868.3647368199
PHL5.27102370901884854.192258702808119-9.518294740452977.580982127855631511.25732310019134456.6666666666666673925.3048697218
SGP4.4096115496222263.981391578110547-3.8147088551071314.51974971089941511.10755535171823026.66666666666666767706.8336149739
THA2.5985877600479653.093989577118966-6.050038468516227.513390532616241510.83988251908323996.6666666666666676573.44430044414
DEU1.34281138774308872.0561956440295153-4.095137516825264.146767550404891530.65305623598714992044108.7012570242
MEX1.96035223328052323.269572551890271-8.354034565763646.048483457046321520.599574471025934313.33333333333333410313.4868482109
BRA1.70521989013224773.1180751506919453-3.545763392694257.528225818153641530.5468822294914333209564.57607719223
JPN0.87048885140090761.8034051868303773-4.168764571467434.097917919431811520.482691775402320113.33333333333333437144.9142660116

Share link

Anyone who has the link will be able to view this.