Some problems in the world seem to be random (stochastic), there is no regularity, but it is possible that humans have not discovered and mastered the laws of such events, so they are random.
Random Walk is a method of solving random problems, which is closely related to human life, such as the trail of drunken walkers, Brownian motion (Brownian motion), and the rise and fall of stocks, which can be simulated. Random walks have been applied to the fields of mathematics, physics, Biology, medicine, economics and so on.
Suppose there was a drunkard in a place, and every second would take one step in the direction of "East", "South", "West", "North", and where would the drunkard go after 500 paces? 1000 steps? Is the drunkard getting farther away from the original point as time grows? The problem seems to be random and impossible to solve, but if you use a computer program to simulate it, you can easily show the path of the drunken man walking and the distance the drunkard is from the point of origin.
Solution Ideas:
Design four classes, respectively: location (to indicate where the drunkard is located), Direction (indicates the direction of the drunkard's walk, if you want to add or modify the direction, modify it in this class), Field (indicates a drunk's flat area, if you want to increase the number of drunks , which can be modified in this class), Drunk (means the drunkard itself)
The code is as follows:
Importmath, Random, Pylab.classLocation :def __init__(self,x,y):#defines the position of the drunkard, that is, a point on the plane, expressed in x and Y coordinatesself.x=x self.y=ydefMove (SELF,XC,YC):#Enter the change values for x and Y coordinates and return the coordinates after the change returnLocation (self.x+xc,self.y+YC)defgetLocation (self):returnSelf.x,self.ydefGetdistance (Self,other):#enter the coordinates of another point to calculate the straight-line distance between the origin and the other point based on the distance between the x-axis and the y-axisox,oy=other.getlocation () xdist=ox-self.x ydist=oy-Self.yreturnMATH.SQRT (xdist**2+ydist**2)classdirection:possibledirection=("S","W","E","N")#four possible directions def __init__(SELF,DIREC):#define the direction, if this direction is not in the possible four directions, then the error ifDirecinchSelf.possibleDirection:self.direc=DirecElse: RaiseValueError ("In direction:__init__") defMove (self,dist):#input moving distance, return plane distance according to different direction ifself.direc=="S":return(0,-Dist)ifself.direc=="W":return(-dist,0)ifself.direc=="E":return(dist,0)ifself.direc=="N":return(0,dist)Else:RaiseValueError ("In direction:move")classField:def __init__(Self,drunk,loc):#define the drunkard and the plane on which it residesself.drunk=Drunk Self.loc=LocdefMove (self,direc,dist):#enter the direction and move distance, get the change of x and Y coordinates, move the value at the origin, and get the coordinates after the change.Oldloc=Self.loc XC,YC=direc.move (Dist) Self.loc=Oldloc.move (XC,YC)defgetLocation (self):returnSelf.locdefGetdrunk (self):returnSelf.drunkclassDrunk:def __init__(self,name): Self.name=namedefMove (self,field,step=1): ifField.getdrunk ()! =Self :RaiseValueError ("No Such drunk is found on the field") forIinchRange (STEP): Direc=Direction (Random.choice (direction.possibledirection)) Field.move (Direc,1)defperformtrial (step,f): Startloc=f.getlocation () distances=[0] forTinchRange (1,step+1): F.getdrunk (). Move (f) Newloc=f.getlocation () distance=newloc.getdistance (Startloc) distances.append (distance)returnDistancesdrunk=drunk ("Baichi") forIinchRange (3): F=Field (Drunk,location (0,0)) Distances=performtrial (500, F) pylab.plot (distances) Pylab.title ("Baichi Walk") Pylab.xlabel (" Time") Pylab.ylabel ("Distance") pylab.show ()
The results of the operation are as follows:
It can be seen that as time goes by, the drunkard is getting farther away from the original point.
It is usually assumed that a drunken man walks in four directions, and goes back and forth, and is expected to end up not far from the original point. But in fact, every step of the drunk, the previous point will change. (When you take the first step, the 100% chance is farther from the origin, and the second step, 75% of the chance will be farther from the origin, and only 25% of the chance will go back to the origin.) This is because the first step, the base point is the origin, and take the second step, the base point becomes the end of the first step after the location. )
In real life, if one day your stock falls, it means that you want to recover the cost of the possibility is very low. Because the base point has been pulled down ... (Ah ah ah ah ...) Don't tell me the bitter truth!!! )
Reference: MIT Open Class: An Introduction to Computer science and programming (17th episode)
Drunken random walk/random walk problem (random Walk Drunk Python)