在Java中,可以使用JFreeChart庫來進行ARIMA模型結果的可視化。以下是一個簡單的示例代碼,用來將ARIMA模型擬合的結果可視化:
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
public class ARIMAVisualization extends ApplicationFrame {
public ARIMAVisualization(String title, double[] actualData, double[] fittedData) {
super(title);
XYSeries actualSeries = new XYSeries("Actual Data");
for (int i = 0; i < actualData.length; i++) {
actualSeries.add(i, actualData[i]);
}
XYSeries fittedSeries = new XYSeries("Fitted Data");
for (int i = 0; i < fittedData.length; i++) {
fittedSeries.add(i, fittedData[i]);
}
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(actualSeries);
dataset.addSeries(fittedSeries);
JFreeChart chart = ChartFactory.createXYLineChart(
"ARIMA Model Fitting Results",
"Time",
"Value",
dataset,
PlotOrientation.VERTICAL,
true,
true,
false
);
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(800, 600));
setContentPane(chartPanel);
}
public static void main(String[] args) {
double[] actualData = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
double[] fittedData = {12, 22, 32, 42, 52, 62, 72, 82, 92, 102};
ARIMAVisualization demo = new ARIMAVisualization("ARIMA Model Visualization", actualData, fittedData);
demo.pack();
demo.setVisible(true);
}
}
在上面的示例代碼中,我們首先創建了一個主類ARIMAVisualization
,繼承自ApplicationFrame
,然后在構造函數中初始化了實際數據和擬合數據的XYSeries
對象,并將它們添加到XYSeriesCollection
對象中。接著創建了一個JFreeChart
對象,并使用ChartFactory
類的createXYLineChart
方法創建了一個折線圖,并將實際數據和擬合數據添加到圖表中。最后創建了一個ChartPanel
對象,并將圖表添加到ChartPanel
中,設置圖表的尺寸并顯示。
你可以根據自己的實際數據和ARIMA模型擬合結果,修改實際數據actualData
和擬合數據fittedData
的數值,運行代碼進行可視化展示。