{"id":10177,"date":"2018-02-18T20:44:18","date_gmt":"2018-02-18T19:44:18","guid":{"rendered":"http:\/\/djalil.chafai.net\/blog\/?p=10177"},"modified":"2018-06-28T15:53:08","modified_gmt":"2018-06-28T13:53:08","slug":"gumbel-fit-with-julia","status":"publish","type":"post","link":"https:\/\/djalil.chafai.net\/blog\/2018\/02\/18\/gumbel-fit-with-julia\/","title":{"rendered":"Gumbel fit with Julia"},"content":{"rendered":"<p>&nbsp;<\/p>\n<figure id=\"attachment_10179\" aria-describedby=\"caption-attachment-10179\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/djalil.chafai.net\/blog\/wp-content\/uploads\/2018\/02\/gumbelfitexample.png\"><img loading=\"lazy\" class=\"size-full wp-image-10179\" src=\"http:\/\/djalil.chafai.net\/blog\/wp-content\/uploads\/2018\/02\/gumbelfitexample.png\" alt=\"Gumbel fit example\" width=\"600\" height=\"400\" srcset=\"https:\/\/djalil.chafai.net\/blog\/wp-content\/uploads\/2018\/02\/gumbelfitexample.png 600w, https:\/\/djalil.chafai.net\/blog\/wp-content\/uploads\/2018\/02\/gumbelfitexample-300x200.png 300w, https:\/\/djalil.chafai.net\/blog\/wp-content\/uploads\/2018\/02\/gumbelfitexample-150x100.png 150w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-10179\" class=\"wp-caption-text\">Gumbel fit example<\/figcaption><\/figure>\n<p>The\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Gumbel_distribution\">Gumbel distribution<\/a> appears in the modelling of extreme values phenomena and the famous\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Fisher%E2%80%93Tippett%E2%80%93Gnedenko_theorem\">Fisher\u2013Tippett\u2013Gnedenko theorem<\/a>. Its cumulative distribution function is given by<\/p>\n<p>$$x\\in\\mathbb{R}\\mapsto\\exp\\left(-\\exp\\left(-\\frac{x-\\mu}{\\sigma}\\right)\\right)$$<\/p>\n<p>where $\\mu\\in\\mathbb{R}$ and $\\sigma\\in(0,\\infty)$ are location and scale parameters. Its mean and variance are<\/p>\n<p>$$\\mu+\\gamma\\sigma\\quad\\text{and}\\quad\\frac{\\pi^2\\sigma^2}{6},$$<\/p>\n<p>where $\\gamma=0.577\\ldots$ is\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Euler%E2%80%93Mascheroni_constant\">Euler's constant<\/a>. The <a href=\"https:\/\/en.wikipedia.org\/wiki\/Maximum_likelihood_estimation\">maximum likelihood estimators<\/a> $\\hat{\\mu}$ and $\\hat{\\sigma}$ satisfy<\/p>\n<p>$$<br \/>\n\\hat{\\mu} =-\\hat{\\sigma}\\log\\left(\\frac{1}{n}\\sum_{i=1}^{n}\\mathrm{e}^{-x_i\/\\hat{\\sigma}}\\right)\\quad\\text{and}\\quad\\hat{\\sigma} = \\frac{\\sum_{i=1}^nx_i}{n}-\\frac{\\sum_{i=1}^{n} x_i\\mathrm{e}^{-x_i\/\\hat{\\sigma}}}{ \\sum_{i=1}^{n} \\mathrm{e}^{-x_i\/\\hat{\\sigma}}}.<br \/>\n$$<\/p>\n<p style=\"text-align: justify;\">Note that the second formula is implicit. Unfortunately, the current version of the <a href=\"http:\/\/julialang.org\/\">Julia<\/a> package\u00a0<a href=\"https:\/\/github.com\/JuliaStats\/Distributions.jl\">Distributions.jl<\/a>\u00a0does not implement yet the Gumbel distro in fit()\/fit_mle(). Here is a home brewed program to fit a Gumbel distribution on data using maximum likelihood and moments. The produced graphic is above. Note that the <a href=\"https:\/\/en.wikipedia.org\/wiki\/R_(programming_language)\">GNU R software<\/a> implements a Gumbel fit in the <a href=\"https:\/\/cran.r-project.org\/web\/packages\/fitdistrplus\/index.html\">fitdistrplus<\/a> package with a notable contribution from my colleague <a href=\"https:\/\/scholar.google.fr\/citations?user=6QBOJTkAAAAJ\">Christophe Dutang<\/a>.<\/p>\n<pre class=\"prettyprint\"># Pkg.add(\"Roots\") # https:\/\/github.com\/JuliaMath\/Roots.jl\r\nusing Roots\r\n\"\"\" \r\nComputes approximately the Maximum Likelihood Estimator (MLE) of \r\nthe position and scale parameters of a Gumbel distribution with\r\ncumulative distribution function x-&gt; exp(-exp(-(x-mu)\/sigma)).\r\nHas mean mu+eulergamma*sigma and variance sigma^2*pi^2\/6.\r\nThe MLE for mu is a function of data and the MLE for sigma.\r\nThe MLE for sigma is the root of a nonlinear function of data,\r\ncomputed numerically with function fzero() from package Roots.\r\n\r\nReference: page 24 of book ISBN 1-86094-224-5.\r\nSamuel Kotz and Saralees Nadarajah\r\nExtreme value distributions.Theory and applications. \r\nImperial College Press, London, 2000. viii+187 pp. \r\nhttps:\/\/ams.org\/mathscinet-getitem?mr=1892574 \r\n\r\n``no distribution should be stated without an explanation of how the\r\nparameters are estimated even at the risk that the methods used will \r\nnot stand up to the present rigorous requirements of mathematically \r\nminded statisticians''. E. J. Gumbel (1958).\r\n\"\"\"\r\nfunction gumbel_fit(data)\r\n  f(x) = x - mean(data) + sum(data.*exp(-data\/x)) \/ sum(exp(-data\/x)) \r\n  sigma = fzero(f,sqrt(6)*std(data)\/pi) # moment estimator as initial value\r\n  mu = - sigma * log(sum(exp(-data\/sigma))\/length(data))\r\n  return mu , sigma\r\nend #function\r\n<\/pre>\n<pre class=\"prettyprint\"># Pkg.add(\"Plots\") # https:\/\/github.com\/JuliaPlots\/Plots.jl\r\n# Pkg.add(\"PyPlot\") # https:\/\/github.com\/JuliaPy\/PyPlot.jl\r\n# http:\/\/docs.juliaplots.org\/\r\nusing Plots\r\nfunction gumbel_fit_plots(data,label,filename) \r\n  n = length(data)\r\n  data = reshape(data,n,1)\r\n  mu , sigma = gumbel_fit(data)\r\n  X = collect(linspace(minimum(data),maximum(data),1000))\r\n  tmp = (X-mu)\/sigma\r\n  Y = exp(-tmp-exp(-tmp))\/sigma\r\n  pyplot()\r\n  histogram!(data,\r\n             nbins = round(Int,sqrt(n)),\r\n             normed = true,\r\n             label = @sprintf(\"%s histogram n=%i\",label,n),\r\n             color = :white)\r\n  plot!(X,Y,\r\n        title = @sprintf(\"Gumbel density mu=%2.2f sigma=%2.2f\",mu,sigma),\r\n        titlefont = font(\"Times\", 10),\r\n        label = \"Gumbel density\",\r\n        lw = 3,\r\n        linestyle = :solid, \r\n        linecolor = :darkblue,\r\n        grid = false,\r\n        border = false)\r\n        gui()\r\n  #savefig(string(filename,\".svg\"))\r\n  savefig(string(filename,\".png\"))\r\nend #function\r\n<\/pre>\n<pre class=\"prettyprint\">function gumbel_fit_example()\r\n  gumbel_fit_plots(-log(-log(rand(1,1000))),\"Sample\",\"gumbelfitexample\") \r\nend #function<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; The&nbsp;Gumbel distribution appears in the modelling of extreme values phenomena and the famous&nbsp;Fisher&ndash;Tippett&ndash;Gnedenko theorem. Its cumulative distribution function is given by $$x\\in\\mathbb{R}\\mapsto\\exp\\left(-\\exp\\left(-\\frac{x-\\mu}{\\sigma}\\right)\\right)$$ where $\\mu\\in\\mathbb{R}$&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/djalil.chafai.net\/blog\/2018\/02\/18\/gumbel-fit-with-julia\/\">Continue reading<span class=\"screen-reader-text\">Gumbel fit with Julia<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":282},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/djalil.chafai.net\/blog\/wp-json\/wp\/v2\/posts\/10177"}],"collection":[{"href":"https:\/\/djalil.chafai.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/djalil.chafai.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/djalil.chafai.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/djalil.chafai.net\/blog\/wp-json\/wp\/v2\/comments?post=10177"}],"version-history":[{"count":37,"href":"https:\/\/djalil.chafai.net\/blog\/wp-json\/wp\/v2\/posts\/10177\/revisions"}],"predecessor-version":[{"id":10413,"href":"https:\/\/djalil.chafai.net\/blog\/wp-json\/wp\/v2\/posts\/10177\/revisions\/10413"}],"wp:attachment":[{"href":"https:\/\/djalil.chafai.net\/blog\/wp-json\/wp\/v2\/media?parent=10177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/djalil.chafai.net\/blog\/wp-json\/wp\/v2\/categories?post=10177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/djalil.chafai.net\/blog\/wp-json\/wp\/v2\/tags?post=10177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}