# Adapt code to Julia

**URL:** <https://discourse.julialang.org/t/adapt-code-to-julia/69707>\
**Category:** New to Julia\
**Tags:** plotting, statistics, plots\
**Created:** [October 13, 2021, 6:07pm UTC](https://discourse.julialang.org/t/adapt-code-to-julia/69707 "2021-10-13T18:07:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinicius\_de\_lima](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinicius_de_lima/32/28717_2.png) [@vinicius\_de\_lima](https://discourse.julialang.org/u/vinicius_de_lima)\
**Post date:** [October 13, 2021, 6:07pm UTC](https://discourse.julialang.org/t/adapt-code-to-julia/69707/1 "2021-10-13T18:07:48Z")

</div>

I’m trying to adapt the following matlab code to julia

```matlab
% MATLAB code to perform the cross validation
lambda = 1;
n = 1000;
X = exprnd(1/lambda,[n,1]);
m = 6:200;
J = zeros(1,195);

for i=1:195
    [num,binc] = hist(X,m(i));
    h = n/m(i);
    J(i) = 2/((n-1)*h)-((n+1)/((n-1)*h))*sum( (num/n).^2 );
end

plot(m,J,'LineWidth',4,'Color',[0.9,0.2,0.0]);

```

Similar implemetation in Python:

```python
# Python code to perform the cross validation
import numpy as np
import matplotlib.pyplot as plt
lambd = 1
n = 1000
X = np.random.exponential(1/lambd, size=n)
m = np.arange(5,200)
J = np.zeros((195))

for i in range(0,195):
  hist,bins = np.histogram(X,bins=m[i])
  h = n/m[i]
  J[i] = 2/((n-1)*h)-((n+1)/((n-1)*h))*np.sum((hist/n)**2)

plt.plot(m,J);

```

Which output is:

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

With the Julia code:

```julia
using Plots,Random,Distributions,StatsBase
λ = 1
n = 1_000
d = Exponential(1/λ)
X = rand(d,n)
m = 6:200
J = zeros(195)

for i = 1:195
    hist = fit(Histogram,X,nbins = m[i])
    h = n/m[i]
    J[i] = 2/((n-1)*h)-((n+1)/((n-1)*h))*sum( (hist.weights/n).^2 )
end

plot(m,J)

```

But the ouput is :

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

Am I missing something?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 13, 2021, 6:42pm UTC](https://discourse.julialang.org/t/adapt-code-to-julia/69707/2 "2021-10-13T18:42:12Z")

</div>

The problem is that `nbins` is not being respected by `fit`:

```julia
julia> h = fit(Histogram, rand(100), nbins=7)
Histogram{Int64, 1, Tuple{StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}}}}
edges:
  0.0:0.2:1.0
weights: [15, 26, 15, 23, 21]
closed: left
isdensity: false

julia> h = fit(Histogram, rand(100), nbins=8)
Histogram{Int64, 1, Tuple{StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}}}}
edges:
  0.0:0.2:1.0
weights: [24, 17, 23, 15, 21]
closed: left
isdensity: false

```

Seems wrong to me. Maybe someone has something to say about that.

---

<div class="post-metadata">

**Author:** ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)\
**Post date:** [October 13, 2021, 8:44pm UTC](https://discourse.julialang.org/t/adapt-code-to-julia/69707/3 "2021-10-13T20:44:32Z")

</div>

It seems StatsBase.histrange has some extra logic in it, but I don’t know the reason why.

The documentation states

> `nbins` : if no `edges` argument is supplied, the **approximate** number of bins to use along each dimension (can be either a single integer, or a tuple of integers).

One can, however, provide the edges directly to avoid this.

```julia
julia> fit(Histogram, rand(100), range(0.0,1.0,length=7+1))
Histogram{Int64, 1, Tuple{StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}}}}
edges:
  0.0:0.14285714285714285:1.0
weights: [13, 9, 13, 18, 16, 19, 12]
closed: left
isdensity: false

julia> fit(Histogram, rand(100), range(0.0,1.0,length=10+1))
Histogram{Int64, 1, Tuple{StepRangeLen{Float64, Base.TwicePrecision{Float64}, Base.TwicePrecision{Float64}}}}
edges:
  0.0:0.1:1.0
weights: [8, 10, 9, 8, 7, 16, 11, 10, 13, 8]
closed: left
isdensity: false

```

I also found an issue regarding this topic:

> <https://github.com/JuliaStats/StatsBase.jl/issues/410>
>
> When fitting a histogram to data with nbins=2 I get the results corresponding to… 3 bins.
> Same happens if I pass 0.0:0.5:1.5 and closed=:left instead. I get 3 bins.
> 
> \`\`\`julia
> julia\> h = fit(Histogram, \[0,1,0,0,1,1,1,1\], nbins=2, closed=:right)
> Histogram{Int64,1,Tuple{StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}}}
> edges:
> -0.5:0.5:1.0
> weights: \[3, 0, 5\]
> closed: right
> isdensity: false
> 
> julia\> h = fit(Histogram, \[0,1,0,0,1,1,1,1\], nbins=3, closed=:right)
> Histogram{Int64,1,Tuple{StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}}}
> edges:
> -0.5:0.5:1.0
> weights: \[3, 0, 5\]
> closed: right
> isdensity: false
> \`\`\`
> I guess this is a bug, otherwise how to avoid this issue?

---

<div class="post-metadata">

**Author:** ![vinicius\_de\_lima](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vinicius_de_lima/32/28717_2.png) [@vinicius\_de\_lima](https://discourse.julialang.org/u/vinicius_de_lima)\
**Post date:** [October 13, 2021, 10:41pm UTC](https://discourse.julialang.org/t/adapt-code-to-julia/69707/4 "2021-10-13T22:41:29Z")

</div>

Changing `fit(Histogram,X,nbins = m[i])` to `fit(Histogram,X,range(minimum(X),maximum(X),length = m[i]+1))` did exactly what I needed, thank you!
