-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathDynamic_Visualization_of_a_2D_Gaussian_Distribution.java
72 lines (63 loc) · 2.8 KB
/
Dynamic_Visualization_of_a_2D_Gaussian_Distribution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.SurfaceChart;
import javafx.scene.chart.XYZChart;
import javafx.stage.Stage;
public class GaussianDistribution3DVisualization extends Application {
@Override
public void start(Stage primaryStage) {
// Define the parameters for the Gaussian distribution
double meanX = 0.0;
double meanY = 0.0;
double sigmaX = 1.0;
double sigmaY = 1.0;
double covarianceXY = 0.5;
// Create a meshgrid for the 2D plot
int numPoints = 100;
double[] x = new double[numPoints];
double[] y = new double[numPoints];
double[][] z = new double[numPoints][numPoints];
double step = 10.0 / (numPoints - 1);
for (int i = 0; i < numPoints; i++) {
x[i] = -5.0 + i * step;
y[i] = -5.0 + i * step;
for (int j = 0; j < numPoints; j++) {
double xVal = x[i];
double yVal = y[j];
z[i][j] = gaussianProbabilityDensity(xVal, yVal, meanX, meanY, sigmaX, sigmaY, covarianceXY);
}
}
// Create the 3D surface plot
NumberAxis xAxis = new NumberAxis(-5, 5, step);
NumberAxis yAxis = new NumberAxis(-5, 5, step);
SurfaceChart<Number, Number, Number> surfaceChart = new SurfaceChart<>(xAxis, yAxis);
surfaceChart.setTitle("2D Gaussian Distribution");
// Add the data to the chart
XYZChart.Series<Number, Number, Number> series = new XYZChart.Series<>();
for (int i = 0; i < numPoints; i++) {
for (int j = 0; j < numPoints; j++) {
series.getData().add(new XYZChart.Data<>(x[i], y[j], z[i][j]));
}
}
surfaceChart.getData().add(series);
// Create and show the scene
Scene scene = new Scene(new Group(surfaceChart), 800, 600);
primaryStage.setTitle("2D Gaussian Distribution 3D Visualization");
primaryStage.setScene(scene);
primaryStage.show();
}
// Gaussian probability density function
private double gaussianProbabilityDensity(double x, double y, double meanX, double meanY, double sigmaX, double sigmaY, double covarianceXY) {
double xTerm = Math.pow(x - meanX, 2) / (2 * Math.pow(sigmaX, 2));
double yTerm = Math.pow(y - meanY, 2) / (2 * Math.pow(sigmaY, 2));
double xyTerm = (x - meanX) * (y - meanY) * covarianceXY / (sigmaX * sigmaY);
double exponent = -(xTerm + yTerm - xyTerm);
double coefficient = 1 / (2 * Math.PI * sigmaX * sigmaY * Math.sqrt(1 - Math.pow(covarianceXY, 2)));
return coefficient * Math.exp(exponent);
}
public static void main(String[] args) {
launch(args);
}
}