I’ve been pushing the data analysis in CatInCloud.io as far as I can. I'm joining sentiment scores to price action, modeling volatility, and trying to capture the "pulse" of the market. But as the dataset grew to millions of rows, standard line charts started to feel insufficient.
They tell you where the price is, but they don't show you the forces moving it.
That’s when I realized I wasn’t trying to build a dashboard. I was staring at a physics problem.
Instead of treating the stock market as a static ledger, I decided to model it as a dynamic system of particles, entropy, and vectors. Here is the physics and calculus powering the visualization engine.
1. The "Frame of Reference" Problem (Procrustes Analysis)
In physics, there is no such thing as absolute motion; there is only motion relative to a frame of reference.
The core feature of my app is the "History" playback. As you watch the market evolve day-by-day using dimensionality reduction algorithms (like UMAP), you run into a stability problem. These algorithms are stochastic. If you run them on Monday’s data and then on Tuesday’s, the "continent" of Tech stocks might rotate 90 degrees or flip upside down.
To the user, this looks like the entire market is spinning. To fix this, I needed to lock the inertial frame of reference.
I implemented Procrustes Analysis. I treat the previous day's market map as a "reference shape." When the new day's data arrives, I don't just plot it; I calculate the optimal rigid-body transformation (translation and rotation) to align it with the previous day's anchors.
The math relies on the Singular Value Decomposition (SVD) of a correlation matrix H, derived from "Anchor Tickers" (stable giants like SPY and AAPL) that exist in both frames.
# The Core Alignment Logic
H = np.dot(BB.T, AA) # Correlation matrix of Anchors
U, S, Vt = np.linalg.svd(H) # Singular Value Decomposition
R = np.dot(U, Vt) # The Rotation Matrix
The "Parity" Edge Case:
There was one catch: SVD doesn't care about "up" or "down." Sometimes, the mathematically optimal fit is a mirror reflection of the previous day. In a physical simulation, this is disorienting.
I added a determinant check—effectively checking the "chirality" or handedness of the transformation. If the determinant is negative, I force a flip of the axis to preserve the orientation of the universe.
if np.linalg.det(R) < 0:
Vt[1, :] *= -1 # Flip the axis to prevent mirroring
R = np.dot(U, Vt) # Recalculate rotation
2. Defining "Market Energy" (Kinetic Heuristics)
In classical mechanics, Kinetic Energy is K = ½mv².
But in the market, what is "mass"? Market Cap? Volume? And what is "velocity"? Price change? If I used a strict Newtonian formula, Nvidia would be a supermassive black hole swallowing the screen, and small caps would be invisible dust.
I needed a metric that captured market attention. I wanted to not only know how far a stock moved, but how much the market cared about that move.
In the frontend processing layer, I define a custom "Energy" metric that unifies vector motion with sentiment analysis.
// Heuristic: Speed * Sentiment
const energy = (Math.abs(vx) + Math.abs(vy)) * (1 + Math.abs(stock.sentiment));
Why Manhattan Distance?
You’ll notice I used the Manhattan Distance (|vx| + |vy|) rather than Euclidean geometry for velocity. This was a deliberate choice. In a volatile market, we aren't measuring the shortest path between two prices; we are measuring the total agitation.
This formula ensures that a small stock exploding on bad news (high agitation, negative sentiment) glows just as brightly as a mega-cap rallying on earnings. It visualizes "heat" rather than just dollars.
3. Sector Gravity (Weighted Centers of Mass)
The map isn't just a cloud of gas; it has structure. Stocks belong to Sectors (Technology, Energy, Finance). These labels can't just float randomly; they need to act as gravitational centers for their constituents.
I calculate the Weighted Center of Mass for each sector. A sector's position isn't a simple average; it's weighted by the "Energy" of its tickers.
Xcenter = Σ(xi · wi) / Σwi
If a massive, high-energy stock like Nvidia moves, it exerts a "gravitational pull" on the entire Technology label, dragging the sector map with it. This creates a visual system where the heaviest, most active particles define the geography of the map.
4. The Observer Effect (Logarithmic Scaling)
Finally, there is the problem of the observer. The simulation happens in an abstract vector space, but the user is observing it through a viewport (phone screen/monitor).
To map the infinite canvas to a finite screen, I use an affine transformation with a logarithmic zoom scale.
const scaleX = (screenWidth - PADDING) / dataWidth;
const scaleY = (screenHeight - PADDING) / dataHeight;
const fitScale = Math.min(scaleX, scaleY);
const autoZoom = Math.log2(fitScale);
Because mapping libraries (like DeckGL) operate on base-2 zoom levels (z), calculating the log2 of our linear scale factor allows the app to perfectly auto-center the chaos. It ensures that whether you are looking at a 10-stock portfolio or the entire Russell 2000, the "universe" always fits your view.
Conclusion: Math as a Lens
In physics, we use math to reduce the complexity of the universe into understandable laws. In Data Engineering, we usually stop at the SQL query.
But by applying concepts from linear algebra and mechanics to stock market data, we can do more than just list prices. We can visualize the thermodynamics of the market. We stop looking at static rows and start observing the true state of the system, revealing where heat is building, where momentum is shifting, and how the entropy is evolving.
It’s not just a dashboard. It’s a simulation.
See the Engine Live
Launch the app to see the physics engine in action. No login required.