New in Wolfram
Mathematica
8: Nonparametric, Derived, and Formula Distributions
◄
previous
|
next
►
Core Algorithms
Employ Nonparametric Data Models in Any Number of Dimensions
A bivariate density estimate of volcanic craters in Uganda.
In[1]:=
X
dat = ExampleData[{"Statistics", "UgandaVolcanoes"}]; poly = ExampleData[{"Statistics", "WesternUgandaBorder"}];
In[2]:=
X
\[ScriptCapitalD] = SmoothKernelDistribution[dat, "SheatherJones"];
In[3]:=
X
inPolyQ = Compile[{{polygon, _Real, 2}, {x, _Real}, {y, _Real}}, Block[{polySides = Length[polygon], X = polygon[[All, 1]], Y = polygon[[All, 2]], Xi, Yi, Yip1, wn = 0, i = 1}, While[i < polySides, Yi = Y[[i]]; Yip1 = Y[[i + 1]]; If[Yi <= y, If[Yip1 > y, Xi = X[[i]]; If[(X[[i + 1]] - Xi) (y - Yi) - (x - Xi) (Yip1 - Yi) > 0, wn++;];];, If[Yip1 <= y, Xi = X[[i]]; If[(X[[i + 1]] - Xi) (y - Yi) - (x - Xi) (Yip1 - Yi) < 0, wn--;];];]; i++]; ! wn == 0]]; dens = ContourPlot[ Evaluate@PDF[\[ScriptCapitalD], {x, y}], {x, 0, 3000}, {y, 550, 4500}, PlotRange -> All, PlotPoints -> 50, Contours -> 15, RegionFunction -> (inPolyQ[poly, #1, #2] &), ColorFunction -> "TemperatureMap"];
In[4]:=
X
Show[dens, ListLinePlot[poly, PlotStyle -> Thick], ListPlot[dat, PlotMarkers -> {Automatic, 8}, PlotStyle -> Black], ImageSize -> {381, 373}, GridLines -> Automatic]
Out[4]=