# Unable to fit LogNormal distribution to the given data

**URL:** <https://uqworld.org/t/unable-to-fit-lognormal-distribution-to-the-given-data/1778>\
**Category:** Community Q&A and How To\
**Tags:** input\
**Created:** [January 21, 2023, 3:02am UTC](https://uqworld.org/t/unable-to-fit-lognormal-distribution-to-the-given-data/1778 "2023-01-21T03:02:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![HZhang](https://uqworld.org/letter_avatar_proxy/v4/letter/h/df788c/32.png) [@HZhang](https://uqworld.org/u/HZhang)\
**Post date:** [January 21, 2023, 3:02am UTC](https://uqworld.org/t/unable-to-fit-lognormal-distribution-to-the-given-data/1778/1 "2023-01-21T03:02:17Z")

</div>

Hi everyone,

I just tried to use the statistical inference module in UQLab to fit a lognormal distribution to a set of data but got the this warning:

_Warning: cannot fit LogNormal distribution to the given data_

The distribution of my data looks like this:  
 ![Screenshot 2023-01-20 215912](https://uqworld.org/uploads/default/original/2X/c/c0364ea85c325bafbb512288c5ebb044cf9f5c1d.png)

What would be the potential reason for getting this warning?

Thanks!

---

<div class="post-metadata">

**Author:** ![styfen.schaer](https://uqworld.org/user_avatar/uqworld.org/styfen.schaer/32/849_2.png) [@styfen.schaer](https://uqworld.org/u/styfen.schaer)\
**Post date:** [January 23, 2023, 8:59am UTC](https://uqworld.org/t/unable-to-fit-lognormal-distribution-to-the-given-data/1778/2 "2023-01-23T08:59:12Z")

</div>

Dear @HZhang

Please post the full error stack. Or even better, share your data and code so we can reproduce the error.

Best regards  
Styfen

---

<div class="post-metadata">

**Author:** ![HZhang](https://uqworld.org/letter_avatar_proxy/v4/letter/h/df788c/32.png) [@HZhang](https://uqworld.org/u/HZhang)\
**Post date:** [January 23, 2023, 4:14pm UTC](https://uqworld.org/t/unable-to-fit-lognormal-distribution-to-the-given-data/1778/3 "2023-01-23T16:14:26Z")

</div>

Hi @styfen.schaer,

Thank you very much for the reply!

The code I used is:

```matlab
iOpts.Inference.Data = X;
iOpts.Marginals.Type = {'LogNormal'};
iOpts.Marginals.Bounds = [min(X) max(X)];
InputHat = uq_createInput(iOpts);

```

The warning and error I got are:

```matlab
Warning: cannot fit LogNormal distribution to the given data 
> In uq_infer_marginals (line 611)
In uq_initialize_uq_default_input (line 141)
In uq_core_module/run_initialization_script (line 208)
In uq_core_input/add_module (line 95)
In uq_createInput (line 114) 
Error using uq_initialize_uq_default_input
Index exceeds the number of array elements. Index must not exceed 0.

Error in uq_core_module/run_initialization_script (line 208)
                            initHandle(obj);

Error in uq_core_input/add_module (line 95)
                success = this.run_initialization_script(obj);

Error in uq_createInput (line 114)
eval(str);

```

And my input data is:  
X =  
202794.565217391  
181967.014492754  
193851.086956522  
179644.821256039  
184515.371980676  
192293.816425121  
180376.086956522  
188410.869565217  
184982.173913044  
182611.956521739  
190732.826086957  
176592.966183575  
182200.415458937  
185961.028985507  
183354.777777778  
182083.714975845  
178752.391304348  
187712.608695652  
189605.536231884  
181385.893719807  
179628.241545894  
178747.608695652  
196323.956521739  
179802.101449275  
176265.120772947  
179323.391304348  
184637.391304348  
182691.855072464  
180493.260869565  
196328.478260870  
180519.879227053  
180840.497584541  
182300.444444444  
186686.739130435  
192257.227053140  
188421.265700483  
193937.149758454  
178413.603864734  
203236.956521739  
183410.222222222

---

<div class="post-metadata">

**Author:** ![styfen.schaer](https://uqworld.org/user_avatar/uqworld.org/styfen.schaer/32/849_2.png) [@styfen.schaer](https://uqworld.org/u/styfen.schaer)\
**Post date:** [February 2, 2023, 6:19pm UTC](https://uqworld.org/t/unable-to-fit-lognormal-distribution-to-the-given-data/1778/4 "2023-02-02T18:19:20Z")

</div>

Dear @HZhang

We investigated your issue and found that the error originates from a numerical issue in Matlab’s optimization algorithm when fitting the distribution parameters, which is caught and raised by UQLab. Scaling your data by multiplying all values by 10^{-5} would be a working workaround. However, be careful because this will shift the mean of the logarithms of your scaled data by ln(10^{-5}), but the standard deviation will stay the same and the confidence bounds should still be valid too.

Best regards  
Styfen

---

<div class="post-metadata">

**Author:** ![HZhang](https://uqworld.org/letter_avatar_proxy/v4/letter/h/df788c/32.png) [@HZhang](https://uqworld.org/u/HZhang)\
**Post date:** [February 5, 2023, 4:15am UTC](https://uqworld.org/t/unable-to-fit-lognormal-distribution-to-the-given-data/1778/5 "2023-02-05T04:15:51Z")

</div>

Hi @styfen.schaer

It worked after scaling the data as you mentioned. Thank you very much!

Best,

Hongzhou
