# Optimization giving wildly different answers depending on box constraints

**URL:** <https://discourse.julialang.org/t/optimization-giving-wildly-different-answers-depending-on-box-constraints/35244>\
**Category:** Optimization (Mathematical)\
**Created:** [February 27, 2020, 5:19pm UTC](https://discourse.julialang.org/t/optimization-giving-wildly-different-answers-depending-on-box-constraints/35244 "2020-02-27T17:19:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lesshaste](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lesshaste/32/12302_2.png) [@lesshaste](https://discourse.julialang.org/u/lesshaste)\
**Post date:** [February 27, 2020, 5:19pm UTC](https://discourse.julialang.org/t/optimization-giving-wildly-different-answers-depending-on-box-constraints/35244/1 "2020-02-27T17:19:46Z")

</div>

I am trying to fit a skew normal distribution to some data. I only have data from the left tail. My code is as follow:

```julia
using Optim
using Distributions
using QuadGK unction T(h, a)
    return 1/(2*π) * quadgk(x -> exp(-(1/2) * h^2 * (1+x^2)) / (1 + x^2), 0 , a)[1]
end

function skewnormalcdf(args, x)
    ξ, ω, α = args
    return @. cdf(Normal(ξ, ω), x) - 2 * T((x - ξ)/ω, α)
end

function skewcost(x)
    ξ, ω, α = x
    return sum((empiricalcdf .- skewnormalcdf([ξ, ω, α], collect(1:13))).^2) 
end

empiricalcdf =BigFloat[5.2909066017292616379563696682453155517578125e-17, 2.998035847356916860912894586658405236403970364197066576597176324261995183917406e-15, 2.893916126231428870186285918394794483524045786436811752754107927550555889872896e-12, 2.893916126231428870186285918394794483524045786436811752754107927550555889872896e-12, 5.984024927027286297718736980025470237800934633258129628285530100177506962477736e-11, 9.828060393355408458878449871353338972538513412036598513660056548532639730808569e-10, 1.306578361293908361005883734780968634960112544877883919057134339745908022112353e-08, 1.426321476573129163499158095212362359685712423190289439313890855808378017172133e-07, 1.291322552024760413190906159876634677313715744603532551931414495220685166587682e-06, 9.765050285646564493780162129498592850243974466341922554440277386489442472405909e-06, 6.197926915196306576612885050087411198022520616606169147964179640314236999394659e-05, 0.0003312274093856579135864289998536855534145377316597168805215086298514038385190639, 0.001493228789321792139431079924969027888633593921034312152826696867055363237815693]

```

I then run different optimizations, slightly varying the box constraint.

```julia
for i in 0.5:0.1:5
       res = optimize(skewcost, [-100.0, 0.01, -i], [100.0, 10.0, i], [0.0, 1.0, 0.0])
       print(i, " ", Optim.minimum(res), "\n")
end

```

This gives:

```julia
0.5 4.337097424258804861792584334740937857712109849288063017519229224922004079649252e-09
0.6 5.1821543534925344101899612836301844966882658580752592020492136602764512939768e-09
0.7 1.40492718489139474069100010374867146150911986075983943252882730190293674075533e-11
0.8 1.068130801871595462372296005777866561060835777925479111674990387217733899630118e-10
0.9 2.916118373940864220527601909879196352799409921233779418545835384493061320631925e-12
1.0 4.114063479495389995882494808456921266509896597604495516366778082815765217204441e-12
1.1 1.283124942550091086493349593936278766322030197355030198233324364027465299190772e-12
1.2 1.04191988167077517052998942235016873047702726226059701093568027966650220291751e-11
1.3 2.745317513091285627296920708669145573311390432906398171482720539544213323435897e-13
1.4 1.16418951208752561481889464181206453390919975569854514336878234457719063963556e-12
1.5 6.351032595434875504286882444881045336907308380944350366920398498227349854857981e-13
1.6 1.669456499895190746991196962748833185378922673635550772356298954983393493092299e-11
1.7 6.673040821354296127377618571820430031918447172757031348263465637441977761342772e-13
1.8 5.41987814523154058650399581616480199068048529911221970562523797332151913371414e-14
1.9 3.256292209504446603407638940194957284068344160141416449575471606599789603051439e-12
2.0 4.772111297418343088851366937851904497172882996782722568127808352860839328768979e-13
2.1 2.431260341547170974705459264121988138926290747760109731617228005635169410300702e-12
2.2 7.547105164295734417423299986665593449392952146791285668780747676212803349714907e-12
2.3 2.172501805059870515211933044799083616714788699120815152352534916981394734497903e-12
2.4 5.71027372242846628347840533950717970258872772069119435826078852769662713941485e-09
2.5 4.862843473961244567134854704527168803662852647360339596861399586272749240688299e-13
2.6 3.426754009409042500506176716180693151511540066055721381948993213449861254853354e-11
2.7 5.383511123847564035733625261659265854873809599963814723903525423018299057288747e-10
2.8 3.123034487588670432919056962216881983284338588905795997040476036503170438745448e-13
2.9 2.195683618890999760783854401011128285857034141139365889382417518773954701519229e-12
3.0 3.324759403344652752441181854249721350004967361633183384376595790222992138143735e-13
3.1 3.374915885409991403965019236184450168510353458446078253172888863461290599336321e-13
3.2 2.434337554927044905580056952267415838276811325130895649822303525268462425538114e-12
3.3 4.802074402430022703861059485522988853042105581966851634744434401280173464252615e-12
3.4 1.988371922560574987261854284970730309500424737493713815911608169029037666538586e-12
3.5 2.749253439701993126942752000643122555859437559070663787961561372617622149571722e-12
3.6 3.783236260783879340431030401627976768414191092583629841675287555995397581824257e-13
3.7 2.358895575868717462364486167749381208983803012193290196461358768246200164804414e-12
3.8 1.033683500366723327865878694111520807683920124743995958824215109150748802271321e-10
3.9 3.209187863352080549712170431270865404456012312458360313848557503301579700892068e-12
4.0 7.422008561624809585537930629158062105224282024702676917704379558537052974037055e-13
4.1 1.803121296995209225430863951139089471546423973504661087856434590154217990360054e-12
4.2 5.182483573441776133465684733796395559032817005696900544471928553828713273305454e-13
4.3 8.422128900049079464227026093804259043007005457048372272547952934397021837017561e-13
4.4 5.351140586564285462443561540482615223509503496661722969524010384740640650539294e-13
4.5 7.4994820281135230259072177782976131820045734646420746139859177207496543903621e-13
4.6 4.986181267406673860006957816642930183468217461298147187336990673010958339573762e-13
4.7 2.214163909825792215159470156111823423168348483196942827320198253726318881300034e-12
4.8 1.233922519787192611662903440358827640705326501550269909403249963234085982113462e-10
4.9 2.406336608394897702747626800837665473201310442134746131616591222312936940390304e-12
5.0 5.811347645553924505825189249151876279862349405694662298483118120716376904526018e-11

```

What is going on and how can I produce an optimization I trust for this problem?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 27, 2020, 5:33pm UTC](https://discourse.julialang.org/t/optimization-giving-wildly-different-answers-depending-on-box-constraints/35244/2 "2020-02-27T17:33:05Z")

</div>

Please don’t start yet another topic about

> [@How to fit a normal distribution to some points in the tail](https://discourse.julialang.org/t/how-to-fit-a-normal-distribution-to-some-points-in-the-tail/35233):
>
> I have some empirical values for the cdf of a distribution which should be close to Normal. These are for x = 1, 2, … , 13. They are as follows: [5.2909066017292616e-17, 2.998035847356917e-15, 2.893916126231429e-12, 2.893916126231429e-12, 5.984024927027287e-11, 9.828060393355409e-10, 1.3065783612939083e-8, 1.4263214765731293e-7, 1.2913225520247604e-6, 9.765050285646565e-6, 6.197926915196307e-5, 0.00033122740938565793, 0.001493228789321792] I would like to find a normal distribution that fits t…

and

> [@How to fit a normal approximation to data in Julia](https://discourse.julialang.org/t/how-to-fit-a-normal-approximation-to-data-in-julia/35045):
>
> I have an unnormalized array of counts for a probability mass function (derived from sampling from the true function). The array should be indexed from 0. 12 is the number of times a 0 occurred in the sample. 199 is the number of times a 1 occurred etc. counts = [12, 199, 3125, 29616, 175426, 662401, 1608671, 2477850, 2441350, 1580290, 709625, 235426, 60899, 12643, 2126, 296, 41, 4, 0, 0] I want to fit a normal approximation to this. My first attempt is to estimate its standard deviation. …

Instead of giving your problem more exposure (as you may expect), it just scatters solutions all over the place.

---

<div class="post-metadata">

**Author:** ![lesshaste](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lesshaste/32/12302_2.png) [@lesshaste](https://discourse.julialang.org/u/lesshaste)\
**Post date:** [February 27, 2020, 5:54pm UTC](https://discourse.julialang.org/t/optimization-giving-wildly-different-answers-depending-on-box-constraints/35244/3 "2020-02-27T17:54:08Z")

</div>

OK no problem. I thought the problems might be different enough but I will stick them all in the same place as you suggest.
