# Different behaviors on Latin Hypercube Sampling from two packages

**URL:** <https://discourse.julialang.org/t/different-behaviors-on-latin-hypercube-sampling-from-two-packages/93076>\
**Category:** Statistics\
**Created:** [January 17, 2023, 8:50am UTC](https://discourse.julialang.org/t/different-behaviors-on-latin-hypercube-sampling-from-two-packages/93076 "2023-01-17T08:50:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![MJulia](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@MJulia](https://discourse.julialang.org/u/MJulia)\
**Post date:** [January 17, 2023, 8:50am UTC](https://discourse.julialang.org/t/different-behaviors-on-latin-hypercube-sampling-from-two-packages/93076/1 "2023-01-17T08:50:19Z")

</div>

I tried QuasiMonteCarlo.jl and LatinHypercubeSampling.jl.

#for QuasiMonteCarlo.jl  
s1 = QuasiMonteCarlo.sample(25, [0.0,0.0], [1.0,1.0], LatinHypercubeSample())

2×25 Matrix{Float64}:  
0.4 0.56 0.96 1.0 0.68 0.16 … 0.48 0.52 0.28 0.32 0.84 0.24  
0.16 0.28 0.96 0.68 1.0 0.08 0.76 0.36 0.6 0.04 0.2 0.92

#for LatinHypercubeSampling.jl  
s2 = scaleLHC(randomLHC(25,2),[(0.0,1.0),(0.0,1.0)])’

2×25 adjoint(::Matrix{Float64}) with eltype Float64:  
0.583333 0.791667 0.708333 0.666667 … 0.208333 0.916667 1.0  
0.0416667 1.0 0.333333 0.0833333 0.5 0.208333 0.458333

The results from QuasiMonteCarlo.jl are evenly distributed at the edges of 25 evenly spaced regions in [0, 1] for both dimensions. The results from LatinHypercubeSampling.jl seem to be randomly located in the 25 evenly spaced regions in [0, 1].

Any idea how to use QuasiMonteCarlo.jl to get results similar to that of LatinHypercubeSampling.jl?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [January 17, 2023, 3:25pm UTC](https://discourse.julialang.org/t/different-behaviors-on-latin-hypercube-sampling-from-two-packages/93076/2 "2023-01-17T15:25:13Z")

</div>

> [@MJulia](#):
>
> Any idea how to use QuasiMonteCarlo.jl to get results similar to that of LatinHypercubeSampling.jl?

Interesting, since it’s just using LatinHypercubeSampling.jl to do the sampling. Open an issue.

---

<div class="post-metadata">

**Author:** ![MJulia](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@MJulia](https://discourse.julialang.org/u/MJulia)\
**Post date:** [January 19, 2023, 2:04pm UTC](https://discourse.julialang.org/t/different-behaviors-on-latin-hypercube-sampling-from-two-packages/93076/3 "2023-01-19T14:04:38Z")

</div>

The cause was found. Two packages handle the boundaries of a closed region differently. For a N-sample case from a region [a,b], QuasiMonteCarlo.jl divides the region [a,b] into N subregions with N+1 edges at (a, a+(b-a)/(N), a+(b-a)/(N)\*2, a+(b-a)/(N)\*3, …, b), but the package ignores the very left edge [a] during sampling. LatinHypercubeSampling.jl divides the region [a,b] into N-1 subregions with N edges at (a, a+(b-a)/(N-1), a+(b-a)/(N-1)\*2, a+(b-a)/(N-1)\*3, …, b) and uses all edges for sampling. Both packages only choose samples from these edges, their behaviors are different from R package “lhs”, which draws samples not only at the edges of subregions, but also in the subregions.

The two packages would show same behavior if one chooses the left boundary for LatinHypercubeSampling.jl considering the above difference. It can be demonstrated in the following simplified one-dimensional case:

s1 = QuasiMonteCarlo.sample(25, [0.0], [1.0], LatinHypercubeSample())  
sort(s1[1,:])’  
s2 = scaleLHC(randomLHC(25,1), [(0.04,1.0)])’  
sort(s2[1,:])’

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [January 20, 2023, 7:02pm UTC](https://discourse.julialang.org/t/different-behaviors-on-latin-hypercube-sampling-from-two-packages/93076/4 "2023-01-20T19:02:17Z")

</div>

Is this on master? There’s a lot of changes on the current master in comparison to the release version.

---

<div class="post-metadata">

**Author:** ![MJulia](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@MJulia](https://discourse.julialang.org/u/MJulia)\
**Post date:** [January 22, 2023, 9:58am UTC](https://discourse.julialang.org/t/different-behaviors-on-latin-hypercube-sampling-from-two-packages/93076/5 "2023-01-22T09:58:18Z")

</div>

No. I used QuasiMonteCarlo v0.2.19 and LatinHypercubeSampling v1.8.0

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [January 22, 2023, 1:19pm UTC](https://discourse.julialang.org/t/different-behaviors-on-latin-hypercube-sampling-from-two-packages/93076/6 "2023-01-22T13:19:08Z")

</div>

Yeah try current master and see if that helps.

---

<div class="post-metadata">

**Author:** ![MJulia](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@MJulia](https://discourse.julialang.org/u/MJulia)\
**Post date:** [January 23, 2023, 6:20am UTC](https://discourse.julialang.org/t/different-behaviors-on-latin-hypercube-sampling-from-two-packages/93076/7 "2023-01-23T06:20:49Z")

</div>

For the master of QuasiMonteCarlo.jl, sampled points locate at the centers of subregions now.  
For the master of LatinHypercubeSampling.jl, sampled points still locate at the edges of subregions.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [January 23, 2023, 7:27am UTC](https://discourse.julialang.org/t/different-behaviors-on-latin-hypercube-sampling-from-two-packages/93076/8 "2023-01-23T07:27:58Z")

</div>

Interesting, can you open an issue?

---

<div class="post-metadata">

**Author:** ![MJulia](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@MJulia](https://discourse.julialang.org/u/MJulia)\
**Post date:** [January 23, 2023, 11:51am UTC](https://discourse.julialang.org/t/different-behaviors-on-latin-hypercube-sampling-from-two-packages/93076/9 "2023-01-23T11:51:22Z")

</div>

Yea.

> <https://github.com/SciML/QuasiMonteCarlo.jl/issues/69>
>
> Please try the following case:
> 
> s1 = QuasiMonteCarlo.sample(25, \[0.0\], \[1.0\], …LatinHypercubeSample())
> sort(s1\[1,:\])’
> s2 = QuasiMonteCarlo.sample(25, \[0.0\], \[1.0\], LatinHypercubeSample())
> sort(s2\[1,:\])’
> s3 = QuasiMonteCarlo.sample(25, \[0.0\], \[1.0\], LatinHypercubeSample())
> sort(s3\[1,:\])’
> s4 = QuasiMonteCarlo.sample(25, \[0.0\], \[1.0\], LatinHypercubeSample())
> sort(s4\[1,:\])’
> 
> Output:
> (sort(s1\[1, :\]))' = \[0.02 0.06 0.1 0.13999999999999999 0.18 0.22 0.26 0.30000000000000004 0.34 0.38 0.42000000000000004 0.46 0.5 0.54 0.5800000000000001 0.62 0.66 0.7000000000000001 0.74 0.78 0.8200000000000001 0.86 0.9 0.9400000000000001 0.98\]
> (sort(s2\[1, :\]))' = \[0.02 0.06 0.1 0.13999999999999999 0.18 0.22 0.26 0.30000000000000004 0.34 0.38 0.42000000000000004 0.46 0.5 0.54 0.5800000000000001 0.62 0.66 0.7000000000000001 0.74 0.78 0.8200000000000001 0.86 0.9 0.9400000000000001 0.98\]
> (sort(s3\[1, :\]))' = \[0.02 0.06 0.1 0.13999999999999999 0.18 0.22 0.26 0.30000000000000004 0.34 0.38 0.42000000000000004 0.46 0.5 0.54 0.5800000000000001 0.62 0.66 0.7000000000000001 0.74 0.78 0.8200000000000001 0.86 0.9 0.9400000000000001 0.98\]
> (sort(s4\[1, :\]))' = \[0.02 0.06 0.1 0.13999999999999999 0.18 0.22 0.26 0.30000000000000004 0.34 0.38 0.42000000000000004 0.46 0.5 0.54 0.5800000000000001 0.62 0.66 0.7000000000000001 0.74 0.78 0.8200000000000001 0.86 0.9 0.9400000000000001 0.98\]
> 
> All samples are located at the centers of 25 evenly divided subregions: such as \[0,0.04\], \[0.04, 0.08\], ...., \[0.96, 1.0\].
> Meanwhile, the results from four times sampling are same except their orders before sorting.

> <https://github.com/MrUrq/LatinHypercubeSampling.jl/issues/22>
>
> Please try the following case:
> 
> s1 = scaleLHC(randomLHC(25,1), \[(0.00,1.0)\])
> …s2 = scaleLHC(randomLHC(25,1), \[(0.00,1.0)\])
> s3 = scaleLHC(randomLHC(25,1), \[(0.00,1.0)\])
> s4 = scaleLHC(randomLHC(25,1), \[(0.00,1.0)\])
> 
> @show sort(s1\[:,1\])
> @show sort(s2\[:,1\])
> @show sort(s3\[:,1\])
> @show sort(s4\[:,1\])
> 
> Output:
> sort(s1\[:, 1\]) = \[0.0, 0.041666666666666664, 0.08333333333333333, 0.125, 0.16666666666666666, 0.20833333333333334, 0.25, 0.2916666666666667, 0.3333333333333333, 0.375, 0.4166666666666667, 0.4583333333333333, 0.5, 0.5416666666666666, 0.5833333333333334, 0.625, 0.6666666666666666, 0.7083333333333334, 0.75, 0.7916666666666666, 0.8333333333333334, 0.875, 0.9166666666666666, 0.9583333333333334, 1.0\]
> sort(s2\[:, 1\]) = \[0.0, 0.041666666666666664, 0.08333333333333333, 0.125, 0.16666666666666666, 0.20833333333333334, 0.25, 0.2916666666666667, 0.3333333333333333, 0.375, 0.4166666666666667, 0.4583333333333333, 0.5, 0.5416666666666666, 0.5833333333333334, 0.625, 0.6666666666666666, 0.7083333333333334, 0.75, 0.7916666666666666, 0.8333333333333334, 0.875, 0.9166666666666666, 0.9583333333333334, 1.0\]
> sort(s3\[:, 1\]) = \[0.0, 0.041666666666666664, 0.08333333333333333, 0.125, 0.16666666666666666, 0.20833333333333334, 0.25, 0.2916666666666667, 0.3333333333333333, 0.375, 0.4166666666666667, 0.4583333333333333, 0.5, 0.5416666666666666, 0.5833333333333334, 0.625, 0.6666666666666666, 0.7083333333333334, 0.75, 0.7916666666666666, 0.8333333333333334, 0.875, 0.9166666666666666, 0.9583333333333334, 1.0\]
> sort(s4\[:, 1\]) = \[0.0, 0.041666666666666664, 0.08333333333333333, 0.125, 0.16666666666666666, 0.20833333333333334, 0.25, 0.2916666666666667, 0.3333333333333333, 0.375, 0.4166666666666667, 0.4583333333333333, 0.5, 0.5416666666666666, 0.5833333333333334, 0.625, 0.6666666666666666, 0.7083333333333334, 0.75, 0.7916666666666666, 0.8333333333333334, 0.875, 0.9166666666666666, 0.9583333333333334, 1.0\]
> 
> All samples (25) are located at the edges of (24) evenly divided subregions: such as \[0,1/24\], \[1/24, 2/24\], ...., \[23/24, 1.0\].
> Meanwhile, the results from four times sampling are same except their orders before sorting.
