# How to fit a skew gaussian?

**URL:** <https://root-forum.cern.ch/t/how-to-fit-a-skew-gaussian/50922>\
**Category:** Newbie\
**Created:** [July 22, 2022, 10:14pm UTC](https://root-forum.cern.ch/t/how-to-fit-a-skew-gaussian/50922 "2022-07-22T22:14:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Divyang](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/7ea924/32.png) [@Divyang](https://root-forum.cern.ch/u/Divyang)\
**Post date:** [July 22, 2022, 10:14pm UTC](https://root-forum.cern.ch/t/how-to-fit-a-skew-gaussian/50922/1 "2022-07-22T22:14:30Z")

</div>

Hello everyone,

How can I fit the skew-gaussian on the histogram plot. As one can notice in the attached plot, I tried to fit the gaussian curve but lots of counts are not covered by the gaussian.

I referred to the wikipedia page of skew-gaussia ([Skew normal distribution - Wikipedia](https://en.wikipedia.org/wiki/Skew_normal_distribution)) but as the formula involves the integration, I don’t know how to implement it in ROOT.

Any suggestion would be a great help.

Thanks,  
Divyang.

![can6](https://root-forum.cern.ch/uploads/default/original/3X/b/c/bcf16178511dd48a42b457d9aec95cd6b3f6201d.jpeg)

---

<div class="post-metadata">

**Author:** ![jonas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jonas/32/24756_2.png) [@jonas](https://root-forum.cern.ch/u/jonas)\
**Post date:** [July 23, 2022, 12:58am UTC](https://root-forum.cern.ch/t/how-to-fit-a-skew-gaussian/50922/2 "2022-07-23T00:58:33Z")

</div>

Hi @Divyang!

No big deal with the integral, as you can also read on Wikipedia in the “Definition” section. You can formulate the PDF in terms of the error function, which is also available in ROOT or standard C++ (`std::erf`). With this script I can reproduce the figure from Wikipedia, for example:

```c++
// testSkewedgauss.C

double skewedgauss(double * x, double *p) {
    double xi = p[0];
    double omega = p[1];
    double alpha = p[2];
    double arg = (x[0] - xi) / omega;
    double smallphi = TMath::Gaus(arg, 0.0, 1.0, true);
    double bigphi = 0.5 * (1 + std::erf(alpha * arg/std::sqrt(2)));
    return 2./omega * smallphi * bigphi;
}

void testSkewedgauss() {

   std::vector<double> shapes{-4.0, -1.0, 0.0, 1.0, 4.0};
   std::vector<int> colors{kCyan, kGreen, kPink, kOrange, kBlue};

   auto c1 = new TCanvas();
   auto legend = new TLegend(0.15,0.65,0.28,0.85);
   legend->SetBorderSize(0);

   for(std::size_t i = 0; i < shapes.size(); ++i) {
      std::string name = "f" + std::to_string(i);
      TF1 *f = new TF1(name.c_str(), skewedgauss,-3.0, 3.0, 3);
      f->SetTitle(";x;Density");
      f->SetParNames("location","scale","shape");
      f->SetLineColor(colors[i]);
      f->SetParameters(0.0, 1.0, shapes[i]);

      f->Draw(i == 0 ? "" : "SAME");
      auto label = "#alpha = " + std::to_string(i);
      legend->AddEntry(name.c_str(),label.c_str(),"l");
   }

   legend->Draw();
   c1->Draw();

   c1->SaveAs("skewedgauss.png");
}

```

 ![skewedgauss](https://root-forum.cern.ch/uploads/default/original/3X/2/d/2d61951010706497af669f9d916bca500d470d6e.png)

Hope this helps!  
Jonas

---

<div class="post-metadata">

**Author:** ![Divyang](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/7ea924/32.png) [@Divyang](https://root-forum.cern.ch/u/Divyang)\
**Post date:** [July 23, 2022, 2:02am UTC](https://root-forum.cern.ch/t/how-to-fit-a-skew-gaussian/50922/3 "2022-07-23T02:02:39Z")

</div>

Thanks, @jonas!

I didn’t know about the erf function. ROOT is very powerful 💪 ✊.

Thanks again,  
Divyang.

---

<div class="post-metadata">

**Author:** ![Divyang](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/7ea924/32.png) [@Divyang](https://root-forum.cern.ch/u/Divyang)\
**Post date:** [July 23, 2022, 7:44pm UTC](https://root-forum.cern.ch/t/how-to-fit-a-skew-gaussian/50922/4 "2022-07-23T19:44:18Z")

</div>

Just want to mentioned that these curves are normalized. Hence, I added one more parameter, amplitude. Here is the function I used for fitting the histogram I mentioned in the question,

```auto
double Skewed_Gauss(double *x, double *p)
{
  double shift = p[0]; // first argument is shift
  double spread = p[1]; // second argument is spread
  double skewness = p[2]; // third argument is skewness
  double amplitude = p[3]; // fourth argument is amplitude

  double argument = (x[0] - shift)/ spread;

  double Norm_gaus = TMath::Gaus(argument, 0.0, 1.0, true);
  double CDF = 0.5 * (1 + erf(skewness* argument/sqrt(2)) );

  return amplitude* Norm_gaus* CDF;
}

```

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [July 26, 2022, 8:53am UTC](https://root-forum.cern.ch/t/how-to-fit-a-skew-gaussian/50922/5 "2022-07-26T08:53:04Z")

</div>

See also:

> [@Clarifying a user defined function (skewed gaussian)](https://root-forum.cern.ch/t/clarifying-a-user-defined-function-skewed-gaussian/42186/3):
>
> Hi, I don’t think what provided there is a good implementation. (x\<[1]) means that this term is equal to zero when false (x greater than Gaussian mean, parameter [1]) and has value = 1 when is true. Now in that implementation you multiply with a negative term which for some value of x \< [1] you will get negative sigma. For the skewed normal I would use the implementation as suggested in [https://en.wikipedia.org/wiki/Skew\_normal\_distribution](https://en.wikipedia.org/wiki/Skew_normal_distribution) TF1 \* f = new TF1("f","2.\*gaus(x,[0],[1],[2])\*ROOT::…

and:

[https://root.cern/doc/master/langaus\_8C.html](https://root.cern/doc/master/langaus_8C.html)
