You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
;; This blog post presents a functional, reproducible implementation of rolling regression in Clojure to estimate
21
+
;; time-varying alpha(α) and beta(β) for student-managed portfolios at the Centre for Investment Management (CIM), The University of Hong Kong.
22
+
;; Unlike traditional CAPM tests based on passive index data, our analysis uses actual(synthetic) trades executed by junior portfolio managers-
23
+
;; undergraduate students who manage simulated equity portfolios over multiple semesters.
24
+
25
+
;; ---
26
+
27
+
;; ## 2. Data and Methodology
28
+
29
+
;; ### 2.1. Data Sources
30
+
31
+
;; We use hypothetical trade data from student portfolio managers at the Centre for Investment Management, including:
32
+
33
+
;; ● Trade dates, tickers, shares, prices
34
+
;; ● Portfolio valuations over time(monthly rebalancing)
35
+
;; ● Cash flows and position weights
36
+
;; ● Returns are computed at the portfolio level, aggregated monthly from individual holdings.
37
+
;; ● Benchmark: S&P 500 total return index
38
+
;; ● Excess returns are calculated for both portfolios and the market.
39
+
;; ● The risk free rate is omitted from analysis for ease of understanding concepts since it does not affect the final results presented in this proposal
40
+
41
+
;; ### 2.2. Rolling Market Model
42
+
43
+
;; For each student portfolio, we estimate the CAPM regression over a rolling 12- or 24-month window:
44
+
45
+
;; Using ordinary least squares(OLS), we obtain a time series of:
46
+
47
+
;; ● Alpha(α): time-varying abnormal return(skill or luck?)
48
+
;; ● Beta(β): evolving sensitivity to market movements(risk awareness?)
49
+
;; ● Windows slide forward monthly, producing a dynamic view of performance.
50
+
51
+
;; ---
52
+
53
+
;; ## 3. Implementation in Clojure
54
+
55
+
;; The full analysis pipeline is implemented in Clojure, designed for clarity, reuse, and integration into
56
+
;; teaching workflows. Below are some core libraries that were used in the project.
57
+
58
+
;; ### Core Libraries
59
+
60
+
;; ● generateme/fastmath: for OLS regression and statistical operations
61
+
;; ● scicloj/tablecloth: for data preparation and transformation
62
+
;; ● clj-python/libpython-clj: to fetch price data using the yfinance library in Python
63
+
;; ● org.clojure/data.csv: for inputting securities for analysis
64
+
;; ● org.clojure/clojure.data.json: to parse JSON strings
65
+
66
+
;; ### Code Preview
67
+
68
+
^{:kindly/hide-codetrue}
69
+
^:kind/code
70
+
(defstock-data
71
+
[["2022-09-01"14.188533001813.9169254303]
72
+
["2022-09-02"14.079690745313.6273431778]
73
+
["2022-09-06"13.711220955613.4456043243]
74
+
["2022-09-07"13.547488289913.6983156204]
75
+
["2022-09-08"13.443606205613.9739990234]
76
+
["2022-09-09"14.140808885914.3705463409]
77
+
["2022-09-12"14.352567755814.4884119034]
78
+
["2022-09-13"13.786215134313.1159820557]
79
+
["2022-09-14"13.238841917413.1129865646]
80
+
["2022-09-15"13.000115265512.9142131805]
81
+
["2022-09-16"12.727426403713.1829051971]
82
+
["2022-09-19"12.997118675513.3666954041]
83
+
["2022-09-20"13.199887497513.1609315872]
84
+
["2022-09-21"13.197890811113.2458353043]
85
+
["2022-09-22"13.055050404112.5466327667]
86
+
["2022-09-23"12.405794274112.5016841888]
87
+
["2022-09-26"12.476713737812.2140140533]
88
+
["2022-09-27"12.492696068612.3988037109]
89
+
["2022-09-28"12.395807208212.7214345932]
90
+
["2022-09-29"12.433764063812.2060251236]
91
+
["2022-09-30"12.073176725512.1251173019]])
92
+
93
+
^{:kindly/hide-codetrue}
94
+
^:kind/code
95
+
(defmarket-data
96
+
[["2022-09-01"3936.72998046883966.8500976562]
97
+
["2022-09-02"3994.65991210943924.2600097656]
98
+
["2022-09-06"3930.88989257813908.1899414062]
99
+
["2022-09-07"3909.42993164063979.8701171875]
100
+
["2022-09-08"3959.93994140624006.1799316406]
101
+
["2022-09-09"4022.93994140624067.3601074219]
102
+
["2022-09-12"4083.6699218754110.41015625]
103
+
["2022-09-13"4037.12011718753932.6899414062]
104
+
["2022-09-14"3940.72998046883946.0100097656]
105
+
["2022-09-15"3932.40991210943901.3500976562]
106
+
["2022-09-16"3880.94995117193873.330078125]
107
+
["2022-09-19"3849.90991210943899.8898925781]
108
+
["2022-09-20"3875.22998046883855.9299316406]
109
+
["2022-09-21"3871.39990234383789.9299316406]
110
+
["2022-09-22"3782.36010742193757.9899902344]
111
+
["2022-09-23"3727.13989257813693.2299804688]
112
+
["2022-09-26"3682.71997070313655.0400390625]
113
+
["2022-09-27"3686.43994140623647.2900390625]
114
+
["2022-09-28"3651.93994140623719.0400390625]
115
+
["2022-09-29"3687.01000976563640.4699707031]
116
+
["2022-09-30"3633.47998046883585.6201171875]])
117
+
118
+
;; The below function uses a simple Python script (with the help of clj-python to integrate Python with Clojure) to fetch open and closing prices for NVIDIA stocks and the S&P 500 Index from Yahoo Finance.
;; Now, this is where we implement our rolling market model, where we regress NVDA stock returns on the market returns using a rolling window to obtain sets of alpha and beta.
198
+
;; To achieve this, we utilize the generateme/fastmath library to implement our very own rolling window regression function.
;; Finally, we use these functions to obtain our sets of alpha and beta. Note that the size of the window is 252 to represent the number of trading days in a year.
;; We present time-series plots of alpha and beta for multiple student portfolios across two academic years. While stock prices
224
+
;; reflect market consensus, alpha and beta decompose returns into risk-adjusted components, revealing whether performance was due to systematic market exposure(beta) or true outperformance(alpha). This provides deeper insight than price alone, enabling informed evaluation of risk, skill, and value creation.
225
+
226
+
;; 
227
+
228
+
;; The above charts show the alpha over time for NVDA(Nvidia) and the historical alpha tracking can help inform future trades.
229
+
;; A close-up analysis of alpha reveals some interesting observations, for example:
230
+
231
+
;; ● The declining trend in NVIDIA’s(NVDA) alpha relative to the S&P 500, as observed from October 2023 to July 2025, reflects a significant shift in the stock’s market performance dynamics.
232
+
;; ● Initially exhibiting strong positive alpha-indicative of substantial outperformance over the broader market-NVDA’s excess return diminished steadily over time, converging toward zero by mid-2025.
233
+
;; ● This erosion of alpha can be attributed to a combination of factors, including the normalization of AI-driven valuation premiums, increased competitive pressures from semiconductor peers, and a broader market environment characterized by robust S&P 500 performance that reduced NVDA’s relative advantage.
234
+
235
+
;; 
236
+
237
+
;; Similarly, the above charts reveals some trends regarding the beta of the NVDA(Nvidia) stock:
238
+
239
+
;; ● The time series of NVIDIA’s(NVDA) beta relative to the S&P 500 reveals a significant evolution in the stock’s sensitivity to market movements over the period from October 2023 to August 2025.
240
+
;; ● Initially, NVDA exhibited a beta around 2.0, indicating that it was approximately twice as volatile as the broader market. Over the course of 2024, beta steadily increased, peaking near 2.9 in early 2025, reflecting heightened market exposure and amplified price swings during periods of strong investor enthusiasm for AI-driven growth.
241
+
;; ● This elevated beta suggests that NVDA was behaving as a high-risk, high-reward asset, with returns highly correlated to-and often magnified by-market trends. However, beginning in April 2025, beta experienced a sharp decline, falling to 1.985 by August 2025, signaling a reduction in its volatility relative to the market. This decrease may reflect a combination of factors: valuation corrections, reduced speculative positioning, and a shift toward more stable, earnings-driven investment behavior.
242
+
243
+
;; Hence, a portfolio manager when looking at the historical performance of NVDA, will be able to evaluate if the trade is adding value to
244
+
;; their portfolio. A stock with a rising alpha and lower beta is a good sign for investment, since it presents a higher return for a lower level of risk. An evaluation of single stocks comprising a portfolio will give a good visual analysis of the stocks risk and return and the overall portfolio. A similar analysis can be conducted at the portfolio level as well to understand the portfolio manager’s performance- alpha creation against the market similar to an AMC tracking a fund’s performance.
;; Rolling alpha and beta are not just statistics-they are mirrors. They allow students to:
253
+
;; ● See the consequences of their strategies
254
+
;; ● Distinguish luck from skill
255
+
;; ● Adjust risk in response to market regimes
256
+
257
+
;; ### 5.2. From Classroom to Portfolio Level
258
+
259
+
;; This system has direct real-world applicability:
260
+
;; ● Can be scaled to real fund management with minimal changes
261
+
;; ● Provides a template for risk dashboards in small asset managers
262
+
;; ● Integrates with automated compliance or alerting(e.g., “beta increased by 30% this month”)
263
+
264
+
;; ---
265
+
266
+
;; ## 6. Conclusion
267
+
268
+
;; By turning student trades into living data, we move beyond theory to reflective practice. And by building in Clojure, we ensure that the system is not a black box—but an open book. In simple words we can decipher, the best financial models don’t just describe the world—they help people learn from it.
269
+
270
+
;; ---
271
+
272
+
;; ## 7. Future Work
273
+
274
+
;; ● Expand to multi-factor rolling models(e.g., FF3: market, size, value)
275
+
;; ● Build a web dashboard using ClojureScript(Reagent) for real-time feedback and interactivity
276
+
;; ● Implement automated alerts(e.g., “your beta has exceeded 1.5”)
277
+
;; ● Add peer benchmarking: compare alpha trajectories across students
278
+
;; ● Integrate with portfolio optimization tools for next-step recommendations
279
+
;; ● Use the pipeline as a capstone module in investment management courses
0 commit comments