← Back to all writing

Ising the Stock Market

April 2020·8 min read·Physics

So here is the deal, I have been trying to convince my professor that we should start implementing our new codes (which solves not so super heavy equations with 4-point Feynman diagrams) in Python than FORTRAN. I have known about numba for sometime and had wanted to try it, so what better way than to introduce the idea of emergence using numba to prove a point!

Our goal is to make a rudimentary model to understand the crowd mentality in stock market, i.e. how the market as a whole reacts to negative or positive news.

Say the market is made up of people with everyone living in a hypothetical flat land who trade a particular sector (like financial sector or tech stocks). In this world, everyone is in a state of "buy" or "sell" which we will take it to be or . Thus the measure that market tries to minimize is given by

Here is the state operator, meaning given a state of market it would pick the sentiment of the person. defines the influence of our nearby trader circle. We also need a final ingredient which we will call randomness which controls how our group's sector is not self contained, meaning our traders often get influenced by what happens in other sectors too.

To see how signifies a market, what we have said is that whenever we are same or different from our neighbors, we pay a cost of and thus the market as an entirety tries to pay the lowest cost.

We will start with an initial configuration of market that is generated by random and calculate what is and then switch the sentiment of someone at random and see if it reduces the cost. If it does, we will take that as the new normalcy of market. If it doesn't reduce the cost, we will still accept it with a probability of . Thus as time proceeds, we will see what happens to the total market sentiment.

The Monte Carlo Simulation

The core simulation code uses Numba's JIT compilation for performance:

@nb.jit(nopython=True)
def mcmove(config, beta, N, J=1):
    for i in range(N):
        for j in range(N):
            a = np.random.randint(0, N)
            b = np.random.randint(0, N)
            s = config[a, b]
            nb_1 = (config[(a+1)%N,b] + config[a,(b+1)%N]
                   + config[(a-1)%N,b] + config[a,(b-1)%N])
            cost = 2*J*s*nb_1
            if cost < 0:
                s *= -1
            elif rand() < np.exp(-cost*beta):
                s *= -1
            config[a, b] = s
    return config

Our simple and idealistic model shows some very exciting results. For small , the market sentiment is rather robust and the "herd behavior" is maintained. Once the market is in a state of "bull" or "bear", it continues on that state for a while till a peaking stimulus is reached. This contrasts the big case where the market is quite rabidly fluctuating with sentiments. This is indeed true in reality too. For instance, a small limit would correspond to what analysts usually call Macro, or essentially the long time dynamics.

For a non-physics mind, this whole idea of modeling the above way stems from the Ising model. Physicists use this same model to study the dynamics and evolution of spins in a lattice which leads to magnetism. Every system in physics is modeled by writing a physical and experimentally motivated that you think the system would try to minimize and then use it to predict and infer things about the system.

Numba Performance

The point of this blog was also to see how Numba performs. We almost have 200 times increase in speed and without Numba, it is almost impossible to run more than runs on an everyday laptop. Well now I have some data to show to my professor.

← Back to all writing