3d-gaussian-distribution.tex 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. % Thanks to Jake for the template
  2. % http://tex.stackexchange.com/a/31715/5645
  3. % and the help here
  4. % http://tex.stackexchange.com/a/198793/5645
  5. \documentclass{standalone}
  6. \usepackage{pgfplots}
  7. \pgfplotsset{compat=1.10}
  8. \begin{document}
  9. \pgfplotsset{
  10. colormap={whitered}{color(0cm)=(white); color(1cm)=(orange!75!red)},
  11. colormap={whiteblue}{color(0cm)=(white); color(1cm)=(blue)},
  12. }
  13. \begin{tikzpicture}[
  14. declare function={mu11=60;},
  15. declare function={mu12=20;},
  16. declare function={sigma11=5;},
  17. declare function={sigma12=5;},
  18. declare function={mu21=70;},
  19. declare function={mu22=40;},
  20. declare function={sigma21=5;},
  21. declare function={sigma22=5;},
  22. declare function={rho=0.8;},
  23. declare function={normal(\m,\s)=1/(2*\s*sqrt(pi))*exp(-(x-\m)^2/(2*\s^2));},
  24. declare function={bivar(\ma,\sa,\mb,\sb,\rho)=
  25. 1/(2*pi*\sa*\sb*\rho) * exp(-((x-\ma)^2/\sa^2 + (y-\mb)^2/\sb^2 - (2*\rho*(x-\ma)*(y-\mb))/(\sa*\sb)))/(2*(1-\rho*\rho));}]
  26. \begin{axis}[
  27. width=15cm,
  28. view={-15}{70},
  29. enlargelimits=false,
  30. grid=major,
  31. domain=40:90,
  32. y domain=0:60,
  33. samples=60,
  34. xlabel=$x_1$,
  35. ylabel=$x_2$,
  36. zlabel={$P$}
  37. ]
  38. \addplot3 [
  39. surf,
  40. colormap={bluewhitered}{color(0cm)=(blue); color(0.5cm)=(white); color(1cm)=(orange!75!red)},
  41. point meta={
  42. (
  43. bivar(mu11,sigma11,mu12,sigma12,rho)>
  44. bivar(mu21,sigma21,mu22,sigma22,rho)?
  45. bivar(mu11,sigma11,mu12,sigma12,rho):
  46. -bivar(mu21,sigma21,mu22,sigma22,rho)
  47. )
  48. }
  49. ] {
  50. max(
  51. bivar(mu11,sigma11,mu12,sigma12,rho),
  52. bivar(mu21,sigma21,mu22,sigma22,rho)
  53. )};
  54. \draw [black!50] (axis cs:-1,0,0) -- (axis cs:4,0,0);
  55. \draw [black!50] (axis cs:0,-1,0) -- (axis cs:0,4,0);
  56. \node at (axis cs:-1,1,0.18) [pin=165:$P(x_1)$] {};
  57. \node at (axis cs:1.5,4,0.32) [pin=-15:$P(x_2)$] {};
  58. \end{axis}
  59. \end{tikzpicture}
  60. \end{document}