Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

1d random walk in python stack exchange

# Python code for 1-D random walk. 
import random 
import numpy as np 
import matplotlib.pyplot as plt 

# Probability to move up or down 
prob = [0.05, 0.95]   

n = 1000 # number of steps

# statically defining the starting position 
start = 2  
positions = [start] 

# creating the random points 
rr = np.random.random(n) 
downp = rr < prob[0] 
upp = rr > prob[1] 

t = 1

step = (1/n)**0.5

for idownp, iupp in zip(downp, upp): 
    down = step if idownp and positions[-1] > 1 else 0
    up = step if iupp and positions[-1] < 4 else 0
    positions.append(positions[-1] - down + up) 

# plotting down the graph of the random walk in 1D 
x = [i*t/n for i in range(n+1)]
plt.plot(x, positions) 
plt.xlabel('Time (seconds)')
plt.ylabel('Distance')
plt.title(f"Random Walk ({n} steps in {t} seconds)")
plt.grid(True)
plt.savefig("random_walk.png")


plt.show()
Comment

1d random walk in python stack exchange


# Python code for 1-D random walk. 
import random 
import numpy as np 
import matplotlib.pyplot as plt 

# Probability to move up or down 
prob = [0.05, 0.95]   

n = 1000 # number of steps

# statically defining the starting position 
start = 2  
positions = [start] 

# creating the random points 
rr = np.random.random(n) 
downp = rr < prob[0] 
upp = rr > prob[1] 

t = 1

step = (1/n)**0.5

for idownp, iupp in zip(downp, upp): 
    down = step if idownp and positions[-1] > 1 else 0
    up = step if iupp and positions[-1] < 4 else 0
    positions.append(positions[-1] - down + up) 

# plotting down the graph of the random walk in 1D 
x = [i*t/n for i in range(n+1)]
plt.plot(x, positions) 
plt.xlabel('Time (seconds)')
plt.ylabel('Distance')
plt.title(f"Random Walk ({n} steps in {t} seconds)")
plt.grid(True)
plt.savefig("random_walk.png")


plt.show() 

Comment

PREVIOUS NEXT
Code Example
Python :: plotly dash datatable column width 
Python :: matplotlib boxplot change size of outliers 
Python :: recorrer lista desde el final python 
Python :: maya python override color rgb 
Python :: how to make a bot send whatever you dm it into a server discord.py 
Python :: Getting the string and the regex of the matched object 
Python :: tokyo timezone python 
Python :: cv2 remove black borders on images 
Python :: dependency inversion 
Python :: class python __call__ 
Python :: list object attributes python 
Python :: spacy create tokenizer 
Python :: how to make a pattern in python in one line 
Python :: getch backspace pytohn 
Python :: python raise exception with custom message 
Python :: py environment variables register in flask 
Python :: python word encode asci 
Python :: standard streams with python3 
Python :: python int to scientific string 
Python :: python global lists 
Python :: how to get checkbutton from a list 
Python :: Panda Python - Calculating what percentage of values are true and false out of total in boolean column 
Python :: k fold cross validation xgboost python 
Python :: find difference between two pandas dataframes 
Python :: get script text selenium python 
Python :: python system performance 
Python :: convert png rgba to rgb pyhton 
Python :: python remove specific character from string 
Python :: how to pass primary key to url django 
Python :: print index in for loop python 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =