So I’ve copied the mathematics used there and ported it across to Python, but with a bit of OOP rearrangement to give an object which will compute values on demand. In retrospect, a full-on generator class might be more pythonic, but I’ll leave that for now.

#below comment from R source code:#"The iterative procedure should stop when reaching a predefined error.#However, for simplicity I have only constructed a for loop. To be improved!"
P,p=P0,p0for i inrange(10):
r=matmult4(P,matmult4(G,p))
r=r/sum(r)
p=p+0.5*(p0-r)
P=numpy.diag(p)

N=numpy.diag([1.0/i for i in matmult4(G,p)])
MTM=matmult4(N,matmult4(G,P))self.MTMcum= numpy.cumsum(MTM,1)

#increment time by one hour and return new valuedef getNext(self):self.randoms1.append(random.uniform(0,1))self.randoms2.append(random.uniform(0,1))self.state=next(j for j,v inenumerate(self.MTMcum[self.state])if v >self.randoms1[-1])self.states_series.append(self.state)self.speed_series.append(self.WindSpeed[self.state]-0.5+(self.randoms2[-1]*int(self.l_categ)))self.time=self.time+datetime.timedelta(hours=1)returnself.getCurrent()

So, to use, create the resource object, which will initialise and create the first values. getCurrent() gives the current time and speed, getNext() increments the time by an hour and returns the next set of values. Each returned dataset also includes binned data by rounded wind speed value for a histogram, as well as the average speed to date.

Looking at the codes makes my head spin. I really admire people who can easily decipher the meaning of that. Actually I took IT as my major course before but I really couldn’t handle the things that they are doing, it led me to shifting to another course which is engineering. My brother had a really hard time getting his degree on IT and I didn’t want to share the same fate. This is a very wonderful post that you have shared to us.