DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • The Perceptron Algorithm and the Kernel Trick
  • Running PyTorch on GPUs
  • Building Intelligent AI Agents Using Semantic Kernels and Azure OpenAI Models: A Step-By-Step Guide
  • Seccomp, eBPF, and the Importance of Kernel System Call Filtering

Trending

  • Building a Real-Time Change Data Capture Pipeline With Debezium, Kafka, and PostgreSQL
  • Web Crawling for RAG With Crawl4AI
  • Enhancing Business Decision-Making Through Advanced Data Visualization Techniques
  • The Ultimate Guide to Code Formatting: Prettier vs ESLint vs Biome

Kernel Density Estimation

Learn how to create an interactive Gaussian Kernel Density Estimation plot with Highcharts.

By 
Mustapha Mekhatria user avatar
Mustapha Mekhatria
·
Jul. 20, 20 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
5.4K Views

Join the DZone community and get the full member experience.

Join For Free

Kernel density estimation is a useful statistical method to estimate the overall shape of a random variable distribution. In other words, kernel density estimation, also known as KDE, helps us to “smooth” and explore data that doesn’t follow any typical probability density distribution, such as normal distribution, binomial distribution, etc. In this tutorial, we will show you how to create an interactive kernel density estimation in Javascript and plot the result using the Highcharts library. Let’s first explore the KDE plot; then we will dive into the code. The demo below displays a Gaussian kernel density estimate of a random dataset:

This chart helps us to estimate the probability distribution of our random data set, and we can see that the data are concentrated mainly at the beginning and at the end of the chart. Basically, for each data points in red, we plot a Gaussian kernel function in orange, then we sum all the kernel functions together to create the density estimate in blue (see demo):

By the way, there are many kernel function types such as Gaussian, Uniform, Epanechnikov, etc. The one we use is the Gaussian kernel, as it offers a smooth pattern. The mathematical representation of the Gaussian kernel is: Gaussian kernel Now, you have an idea about how the kernel density estimation looks like, let’s take a look at the code behind it. There are four main steps in the code:

  1. Create the Gaussian kernel function.
  2. Process the density estimate points.
  3. Process the kernel points.
  4. Plot the whole data points.

Gaussian Kernel Function

The following code represents the Gaussian kernel function:

Java
 




x


 
1
function GaussKDE(xi, x) {
2
  return (1 / Math.sqrt(2 * Math.PI)) * Math.exp(Math.pow(xi - x, 2) / -2);
3
}


Where x represents the main data (observation), and xi represents the range to plot the kernels and the density estimate function. In our case, the xi range is from 88 to 107 to be sure to cover the range of the observation data that is from 93 to 102.

Density Estimate Points

The following loop creates the density estimate points using the GaussKDE() function and the range represented by the array xiData:

Java
 




xxxxxxxxxx
1
11


 
1
//Create the density estimate
2
for (i = 0; i < xiData.length; i++) {
3
  let temp = 0;
4
  kernel.push([]);
5
  kernel[i].push(new Array(dataSource.length));
6
  for (j = 0; j < dataSource.length; j++) {
7
    temp = temp + GaussKDE(xiData[i], dataSource[j]);
8
    kernel[i][j] = GaussKDE(xiData[i], dataSource[j]);
9
  }
10
  data.push([xiData[i], (1 / N) * temp]);
11
}


Kernels Points

This step is required only if you would like to display the kernel points (orange charts); otherwise, you are already good with the density estimate step. Here is the code to process the data points for each kernel:

Java
 




xxxxxxxxxx
1


 
1
//Create the kernels
2
for (i = 0; i < dataSource.length; i++) {
3
  kernelChart.push([]);
4
  kernelChart[i].push(new Array(kernel.length));
5
  for (j = 0; j < kernel.length; j++) {
6
    kernelChart[i].push([xiData[j], (1 / N) * kernel[j][i]]);
7
  }
8
}
9

          


Basically, this loop is just about adding the range xiData to each kernel array that was already processed in the density estimate step.

Plot the Points

Once all the data points are processed, it is time to use Highcharts to render the series. The density estimate and the kernels are spline chart types, whereas the observations are plotted as a scatter plot:

Java
 




xxxxxxxxxx
1
55


 
1
Highcharts.chart("container", {
2
  chart: {
3
    type: "spline",
4
    animation: true
5
  },
6
  title: {
7
    text: "Gaussian Kernel Density Estimation (KDE)"
8
  },
9
  yAxis: {
10
    title: { text: null }
11
  },
12
  tooltip: {
13
    valueDecimals: 3
14
  },
15
  plotOptions: {
16
    series: {
17
      marker: {
18
        enabled: false
19
      },
20
      dashStyle: "shortdot",
21
      color: "#ff8d1e",
22
      pointStart: xiData[0],
23
      animation: {
24
        duration: animationTime
25
      }
26
    }
27
  },
28
  series: [
29
    {
30
      type: "scatter",
31
      name: "Observation",
32
      marker: {
33
        enabled: true,
34
        radius: 5,
35
        fillColor: "#ff1e1f"
36
      },
37
      data: dataPoint,
38
      tooltip: {
39
        headerFormat: "{series.name}:",
40
        pointFormat: "<b>{point.x}</b>"
41
      },
42
      zIndex: 9
43
    },
44
    {
45
      name: "KDE",
46
      dashStyle: "solid",
47
      lineWidth: 2,
48
      color: "#1E90FF",
49
      data: data
50
    },
51
    {
52
      name: "k(" + dataSource[0] + ")",
53
      data: kernelChart[0]
54
    },...  ]
55
});


Now, you are ready to explore your own data using the power of the Kernel density estimation plot. Feel free to share your comments or questions in the comment section below.

Kernel (operating system)

Opinions expressed by DZone contributors are their own.

Related

  • The Perceptron Algorithm and the Kernel Trick
  • Running PyTorch on GPUs
  • Building Intelligent AI Agents Using Semantic Kernels and Azure OpenAI Models: A Step-By-Step Guide
  • Seccomp, eBPF, and the Importance of Kernel System Call Filtering

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!