Function with For Loop by Row

55 Views Asked by At

I am trying to achieve a monte carlo simulation by row (name) in this case, I am able to get the monte carlo simulations but the random normal distribution it is doing is consistent across all names when I am seeking for it to be independent and random by row. I have exhausted all my ideas and I am seeking help now :)

The result this results in is clearly 'row' specific meaning the rnd.gauss(mean, std) is consistent when I want it to truly be unique and random by row/name. I have tried adding a for loop utilizing 'name' but it is not working as expected.

import random as rnd
import pandas as pd

data = [['lj', 22.72, 37, 9.8], ['nc', 13.24, 30.9, 4.4],['bm', 13.77, 26.3, 9.3], ['jl', 12, 25.9, 7.2]]
df = pd.DataFrame(data, columns= ['Name', 'Proj', 'Ceil', 'Floor'])

mean = df['Proj'].astype(float)
std = ((df['Ceil'].astype(float) - df['Floor'].astype(float))/2).astype(float)
num_of_sims = 10
name = df['Name']

output = pd.DataFrame([])

def simulation (n):
    simulated_projection = 0
    simulated_projection = rnd.gauss(mean, std)
    return simulated_projection

for i in range(num_of_sims):
    projection = simulation(num_of_sims)
    output = output.append(projection, True)
0 1 2 3
0 19.057055 9.671322 11.480660 9.481726
1 28.160946 18.540922 17.170591 15.740650
2 27.912110 18.298490 17.015069 15.569576
3 35.364650 25.559236 21.672906 20.693197
4 8.840950 -0.281869 5.095593 2.458153
5 27.978372 18.363047 17.056483 15.615131
6 47.647661 37.526140 29.349788 29.137767
7 12.740099 3.516935 7.532562 5.138818
8 20.443244 11.021837 12.347027 10.434730
9 33.104873 23.357615 20.260545 19.139600

The output is the table above, you can see that the rnd.gauss(mean, std) is creating the desired randomized normal distribution per each column however the randomization per row is consistent. For example row 3 shows 35 for column 0, column 1 is also a larger value 25, column 2 also a large value 21 etc. Row 4 show 8 for column 0, column 1 is also small -0.28, column 2 is also small 5 etc.

What I am looking for is the randomization to be independent by row (def simulation) to achieve a more randomized sampling per simulation.

0

There are 0 best solutions below