How to repeat a process with different value of variable in Python

Viewed 27

For each value of T, I get a list of velocities and then I find the standard deviation. I want to try with different values of T, each time I can get a list of velocities for each T and then find the standard deviation without copying and running the code separately. Any ideas how can I do that? Only in Python please. Thank you!

T2 = 1 #change as needed
N = 100  #number of loops (random walkers)
random_walk = np.empty((N,100)) #first element is row, second is column
for i in range (len(random_walk)):
    U_pos = 0
    U_neg = 0 
    U = U_pos - U_neg
    P_neg = 0.5*(1 - np.tanh(U/(2*T2)))
    P_pos = 0.5*(1 + np.tanh(U/(2*T2)))
    for j in range (len(random_walk[0])):
        random_walk[i][0] = 0
        if (random() > 0) and (P_neg > random()):  
            movement = -1
            U_j = np.random.pareto(2,1)+0.5
            if U_j > U_neg: 
                U_neg = U_j
            else: 
                U_neg = U_neg
            U = U_pos - U_neg
            P_neg = 0.5*(1 - np.tanh(U/(2*T2)))
            P_pos = 0.5*(1 + np.tanh(U/(2*T2)))
        else:
            movement = 1
            U_j = np.random.pareto(2,1)+0.5
            if U_j > U_pos:
                U_pos = U_j
            else: 
                U_pos = U_pos
            U = U_pos - U_neg
            P_pos = 0.5*(1 + np.tanh(U/(2*T2)))
            P_neg = 0.5*(1 - np.tanh(U/(2*T2)))   
        random_walk[i][j] = random_walk[i][j-1] + movement
1 Answers

You might convert your code block into a function that can be called. Either the function call could utilize a specific list of numbers, or could be values entered by a user as in the following "proof of principle" example I composed.

import numpy as np
import random as random

def velocity(T2):
    N = 100  #number of loops (random walkers)
    random_walk = np.empty((N,100)) #first element is row, second is column
    for i in range (len(random_walk)):
        U_pos = 0
        U_neg = 0 
        U = U_pos - U_neg
        P_neg = 0.5*(1 - np.tanh(U/(2*T2)))
        P_pos = 0.5*(1 + np.tanh(U/(2*T2)))
        for j in range (len(random_walk[0])):
            random_walk[i][0] = 0
            x = random.random()
            if (x > 0) and (P_neg > x):  
                movement = -1
                U_j = np.random.pareto(2,1)+0.5
                if U_j > U_neg: 
                    U_neg = U_j
                else: 
                    U_neg = U_neg
                U = U_pos - U_neg
                P_neg = 0.5*(1 - np.tanh(U/(2*T2)))
                P_pos = 0.5*(1 + np.tanh(U/(2*T2)))
            else:
                movement = 1
                U_j = np.random.pareto(2,1)+0.5
                if U_j > U_pos:
                    U_pos = U_j
                else: 
                    U_pos = U_pos
                U = U_pos - U_neg
                P_pos = 0.5*(1 + np.tanh(U/(2*T2)))
                P_neg = 0.5*(1 - np.tanh(U/(2*T2)))   
            random_walk[i][j] = random_walk[i][j-1] + movement
        return random_walk
        
while True:
    data = int(input('Please enter a value to evaluate or 0 to exit: '))
    if data == 0:
        break
    print(velocity(data))

Following was some sample input and output from this program example.

@Una:~/Python_Programs/Velocity$ python3 Velocity.py 
Please enter a value to evaluate or 0 to exit: 2
[[  0.  -1.  -2. ... -95. -96. -97.]
 [  0.   0.   0. ...   0.   0.   0.]
 [  0.   0.   0. ...   0.   0.   0.]
 ...
 [  0.   0.   0. ...   0.   0.   0.]
 [  0.   0.   0. ...   0.   0.   0.]
 [  0.   0.   0. ...   0.   0.   0.]]
Please enter a value to evaluate or 0 to exit: 14
[[  0.  -1.   0. ... -13. -14. -15.]
 [  0.   0.   0. ...   0.   0.   0.]
 [  0.   0.   0. ...   0.   0.   0.]
 ...
 [  0.   0.   0. ...   0.   0.   0.]
 [  0.   0.   0. ...   0.   0.   0.]
 [  0.   0.   0. ...   0.   0.   0.]]
Please enter a value to evaluate or 0 to exit: 0

That's just one of a possible way you could do this, but this should give you food for thought.

Related