# How to determine distribution of real-world data and then apply same distribution to model with Agents.jl

**URL:** https://discourse.julialang.org/t/how-to-determine-distribution-of-real-world-data-and-then-apply-same-distribution-to-model-with-agents-jl/119617
**Category:** Modelling & Simulations
**Tags:** distributions
**Created:** [September 19, 2024, 6:52pm UTC](https://discourse.julialang.org/t/how-to-determine-distribution-of-real-world-data-and-then-apply-same-distribution-to-model-with-agents-jl/119617 "2024-09-19T18:52:26Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![tfranks](https://avatars.discourse-cdn.com/v4/letter/t/898d66/32.png) [@tfranks](https://discourse.julialang.org/u/tfranks)
#### Post date: [September 19, 2024, 6:52pm UTC](https://discourse.julialang.org/t/how-to-determine-distribution-of-real-world-data-and-then-apply-same-distribution-to-model-with-agents-jl/119617/1 "2024-09-19T18:52:26Z")

</div>

Hello,

Goal: trying to determine distribution from 4Q22 data set and apply the same distribution to a network model using Agents.jl. See 4Q22 data on xy-grid.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/e/8e6e51221b30a5b5866da40999633ecf1b0c829a.jpeg)

Background: I had an earlier post on Discourse where I guessed that the distribution was Poisson, but the data are not normally distributed. See actual grid of data below,

Latest attempt: I used the “fit” function in Distributions.jl to get the following info on the 4Q22 data.  
Gamma result: Gamma{Float64}(α=14.01547510539733, θ=6.195715980420822)  
Exponential result: Exponential{Float64}(θ=86.83590308370044)  
Poisson result: Poisson{Float64}(λ=86.83590308370044)

Gamma seems the best option, but still looks normally distributed. See xy, xyz grids and code below. Should I take a different approach? Am I misunderstanding or misapplying the results from “fit”?

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/9/89ac8ba56235fdf2b214fc3c56ea0b0da522a1fa.png)  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/f/2fe9f8bd9d357175667ff730d3d39f8101bf8735.png)

Thank you. Best.

> Blockquote##Gamma distribution for x and y coordinates  
> xes = rand(Gamma(14), n\_banks)  
> yes = rand(Gamma(6), n\_banks)

```
#=
##Erlang distribution for x and y coordinates
shape = 7
xrate = .5
yrate = 1.0
uninsured_values = rand(Erlang(shape, xrate), n_banks)
investments_values = rand(Erlang(shape, yrate), n_banks)
=#

#add Bank agents;
#for i in 1:n_banks
for (x, y) in zip(xes, yes) #Poisson
    add_agent!(Bank, model,
    #pos=(x,y), #Poisson
    pos=(round(Int, x), round(Int, y)), #Gamma
    #uninsured = uninsured_values[i],
    #investments = investments_values[i],
    uninsured = rand(), #Poisson
    investments = 2 * rand(), #Poisson
    totDep = 100,    
    AFS_securities = rand(1:10),
    social_network = rand(1:3),    
    vul = false,
    health = true,
    acolor = :black,
    interest_rate = rand(1:10),
    )
end

```

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [September 20, 2024, 6:30am UTC](https://discourse.julialang.org/t/how-to-determine-distribution-of-real-world-data-and-then-apply-same-distribution-to-model-with-agents-jl/119617/2 "2024-09-20T06:30:32Z")

</div>

Why don’t you just sample from the empirical distribution?

---

<div class="post-metadata">

### Author: ![tfranks](https://avatars.discourse-cdn.com/v4/letter/t/898d66/32.png) [@tfranks](https://discourse.julialang.org/u/tfranks)
#### Post date: [September 20, 2024, 11:54am UTC](https://discourse.julialang.org/t/how-to-determine-distribution-of-real-world-data-and-then-apply-same-distribution-to-model-with-agents-jl/119617/3 "2024-09-20T11:54:17Z")

</div>

Hello,

How would I sample the distribution?

I may be misinterpreting my data. The way the agent-based model is set-up, each bank agent gets placed on a 2D or 3D grid. On the 2D grid, I can “eyeball” it to see that there is a normal distribution since all quadrants appear to be equally covered. Thanks for your insights.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [September 20, 2024, 12:02pm UTC](https://discourse.julialang.org/t/how-to-determine-distribution-of-real-world-data-and-then-apply-same-distribution-to-model-with-agents-jl/119617/4 "2024-09-20T12:02:05Z")

</div>

I’m not sure I really understand your problem, but what I think it is currently is you have a population of real world observations with some values of “investments” and “uninsured” and you now want to build an Agents.jl model in which there are simulated agents with those values as well, and you want the distribution of investment and uninsured across the agents in your model to follow the distribution that you observe.

If that’s right, then why don’t you just instantiate the agents by drawing from the actual data you have? I’m not sure what format your data is in, but for most collections you can just do `rand(collection, n)` to get `n` random draws.

E.g. if your data is a vector of tuples `(investment, uninsured)`:

```julia
julia> data = [(120, 34), (75, 65), (160, 23)]
3-element Vector{Tuple{Int64, Int64}}:
 (120, 34)
 (75, 65)
 (160, 23)

julia> rand(data)
(120, 34)

julia> rand(data, 5)
5-element Vector{Tuple{Int64, Int64}}:
 (120, 34)
 (75, 65)
 (75, 65)
 (75, 65)
 (120, 34)

```

---

<div class="post-metadata">

### Author: ![tfranks](https://avatars.discourse-cdn.com/v4/letter/t/898d66/32.png) [@tfranks](https://discourse.julialang.org/u/tfranks)
#### Post date: [September 20, 2024, 12:14pm UTC](https://discourse.julialang.org/t/how-to-determine-distribution-of-real-world-data-and-then-apply-same-distribution-to-model-with-agents-jl/119617/5 "2024-09-20T12:14:22Z")

</div>

Ahhh…so I have a CSV file with 10K rows of the real world data. What would the code look like to call the dataframe and add agents accordingly? Instead of creating the agents with random variables, I’d call the dataframe? Thanks again.

> Blockquote  
> df = CSV.read(“////CallReport/4Q22CallDataDistribution3.csv”, DataFrame)

uninsured = rand(df, :Uninsured),  
investments = 2 \* rand(df, :Investments),

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [September 20, 2024, 12:26pm UTC](https://discourse.julialang.org/t/how-to-determine-distribution-of-real-world-data-and-then-apply-same-distribution-to-model-with-agents-jl/119617/6 "2024-09-20T12:26:56Z")

</div>

You probably want to capture the correlation between uninsured and investment, so you’d want to sample rows rathern than a value from each column separately. Something like:

```julia
function draw_values(df)
    row = rand(1:nrow(df))
    return df[row, [:Investments, :Uninsured]
end

```

and then an agent is initialized as

```julia
vals = draw_values(df)
add_agent!(Bank, model, 
    pos = (vals.Investments, vals.Uninsured),
    (...)
)

```

(take with a grain of salt, it’s been a very long time since I’ve used Agents.jl…)

---

<div class="post-metadata">

### Author: ![tfranks](https://avatars.discourse-cdn.com/v4/letter/t/898d66/32.png) [@tfranks](https://discourse.julialang.org/u/tfranks)
#### Post date: [September 20, 2024, 12:28pm UTC](https://discourse.julialang.org/t/how-to-determine-distribution-of-real-world-data-and-then-apply-same-distribution-to-model-with-agents-jl/119617/7 "2024-09-20T12:28:20Z")

</div>

Wow. Now I get it! Thank you ever so much. You’re the best.

Have a great day.
