# Replicating power law distribution

**URL:** https://discourse.julialang.org/t/replicating-power-law-distribution/125021
**Category:** General Usage
**Tags:** curve-fitting, graphs
**Created:** [January 21, 2025, 2:55pm UTC](https://discourse.julialang.org/t/replicating-power-law-distribution/125021 "2025-01-21T14:55:05Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [January 21, 2025, 2:55pm UTC](https://discourse.julialang.org/t/replicating-power-law-distribution/125021/1 "2025-01-21T14:55:05Z")

</div>

Hi,

I am trying to generate a power law degree distribution using `barabasi_albert` from Graphs.jl. I am having trouble replicating the figure 4 on page 7 of this [resource](https://www.math.nagoya-u.ac.jp/~richard/teaching/s2024/SML_Adam_2.pdf). Please forgive me in advance if I am doing something wrong, or have a conceptual misunderstanding. My understanding is that the slope of the degree distribution should be close to -3 in log space. In the example provided in the link, the slope is -2.79. However, I obtain -2.12. Can someone please provide some guidance?

Thank you!

> **Code**
>
> ```julia
> using CurveFit
> using Graphs 
> using Plots
> 
> graph = barabasi_albert(100_000, 3, 3; is_directed = true)
> 
> degrees = degree(graph)
> unique_degrees = unique(degrees)
> degree_probs = map(d -> mean(d .== degrees), unique_degrees)
> 
> log_log_plot = Plots.scatter(
> unique_degrees,
> degree_probs,
> xaxis = (:log10, font(8)),
> yaxis = (:log10, font(8)),
> grid = false,
> leg = false,
> markersize = 2.5,
> xlabel = "degree",
> ylabel = "probability",
> )
> fit = curve_fit(LinearFit, log10.(unique_degrees), log10.(degree_probs))
> plot!(unique_degrees, 10 .^ fit.(log10.(unique_degrees)), color = :black)
> plot!(1:1000, (1:1000).^(-3), color = :blue)
> 
> ```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/b/0be09177a2a9fc2f7a9f88d7b29913f698c84cf5.png)

---

<div class="post-metadata">

### Author: ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)
#### Post date: [January 21, 2025, 5:54pm UTC](https://discourse.julialang.org/t/replicating-power-law-distribution/125021/2 "2025-01-21T17:54:57Z")

</div>

Estimating powerlaws via linear regression hardly ever works – see [Power-law distributions in empirical data](https://arxiv.org/abs/0706.1062) for details and proper ML based estimation algorithms. Unfortunately, there only seems to be an abandoned package in Julia …  
In any case, here is a quick estimate on your data using formula (B.17) from the paper and an arbitrary x\_{min}:

```julia-repl
julia> xmin = 23
23

julia> x = degrees[degrees .> xmin];

julia> 1 + inv(mean(log.(x ./ (xmin - 1/2))))
2.810142626941318

```

which is already much closer to the analytic exponent.

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [January 21, 2025, 7:21pm UTC](https://discourse.julialang.org/t/replicating-power-law-distribution/125021/3 "2025-01-21T19:21:09Z")

</div>

Thanks for your help!

Indeed, I was wondering about the tail, which is flattened because the minimum empirical probability must be 1/n.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [January 21, 2025, 8:43pm UTC](https://discourse.julialang.org/t/replicating-power-law-distribution/125021/4 "2025-01-21T20:43:35Z")

</div>

FWIW, using RobustModels.jl, one can obtain the following result:

 ![RobustModels_fit_to_power_law](https://global.discourse-cdn.com/julialang/original/3X/d/b/db59a189fac52c51bb6132fc250e3e6850e6e128.jpeg)

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [January 21, 2025, 10:24pm UTC](https://discourse.julialang.org/t/replicating-power-law-distribution/125021/5 "2025-01-21T22:24:08Z")

</div>

Nice find. Thanks!
