{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "1ec0d39a",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/mxy/miniconda3/envs/TFB/lib/python3.11/site-packages/numpy/lib/_histograms_impl.py:902: RuntimeWarning: divide by zero encountered in divide\n",
      "  return n/db/n.sum(), bin_edges\n",
      "/home/mxy/miniconda3/envs/TFB/lib/python3.11/site-packages/numpy/lib/_histograms_impl.py:902: RuntimeWarning: invalid value encountered in divide\n",
      "  return n/db/n.sum(), bin_edges\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 34492 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 30004 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 38016 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 39968 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 86500 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 74428 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 67572 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "time_maes = []\n",
    "time_mses = []\n",
    "freq_maes = []\n",
    "freq_mses = []\n",
    "ratios = []\n",
    "ratios_mae = []\n",
    "for seed in range(10000):\n",
    "    np.random.seed(seed)  # For reproducibility\n",
    "    series_a = np.random.randn(96, 7)\n",
    "    series_b = np.random.randn(96, 7)\n",
    "\n",
    "    # Time-domain MSE\n",
    "    time_mse = np.mean(np.abs(series_a - series_b) ** 2)\n",
    "    # Time-domain MAE\n",
    "    time_mae = np.mean(np.abs(series_a - series_b))\n",
    "    time_mses.append(time_mse)\n",
    "    time_maes.append(time_mae)\n",
    "\n",
    "    # Frequency-domain MAE (complex) via rFFT along the time dimension (axis=0)\n",
    "    fft_a = np.fft.fft(series_a, axis=0)\n",
    "    fft_b = np.fft.fft(series_b, axis=0)\n",
    "    freq_mse = np.mean(np.abs(fft_a - fft_b) ** 2)\n",
    "    ratios.append(freq_mse / time_mse)\n",
    "    freq_mae = np.mean(np.abs(fft_a - fft_b))\n",
    "    ratios_mae.append(freq_mae / time_mae)\n",
    "    freq_mses.append(freq_mse)\n",
    "    freq_maes.append(freq_mae)\n",
    "plt.hist(ratios, bins=50, density=True)\n",
    "plt.xlabel('Frequency-domain MSE / Time-domain MSE Ratio')\n",
    "plt.ylabel('Density')\n",
    "plt.title('Distribution of Frequency-domain MSE / Time-domain MSE Ratios')\n",
    "plt.grid()\n",
    "plt.show()\n",
    "plt.hist(ratios_mae, bins=50, density=True)\n",
    "plt.xlabel('Frequency-domain MAE / Time-domain MAE Ratio')\n",
    "plt.ylabel('Density')\n",
    "plt.title('Distribution of Frequency-domain MAE / Time-domain MAE Ratios')\n",
    "plt.grid()\n",
    "plt.show()\n",
    "# compare mse and mae ratios\n",
    "plt.scatter(ratios, ratios_mae, alpha=0.5)\n",
    "plt.xlabel('Frequency-domain MSE / Time-domain MSE Ratio')\n",
    "plt.ylabel('Frequency-domain MAE / Time-domain MAE Ratio')\n",
    "plt.title('Comparison of MSE and MAE Ratios')\n",
    "plt.grid()\n",
    "plt.show()\n",
    "# comapre mse and mae in time and frequency domains\n",
    "plt.scatter(time_mses, freq_mses, alpha=0.5, label='MSE')\n",
    "plt.xlabel('Time-domain Error')\n",
    "plt.ylabel('Frequency-domain Error')\n",
    "plt.title('Comparison of Time-domain and Frequency-domain Errors')\n",
    "plt.legend()\n",
    "plt.grid()\n",
    "plt.show()     \n",
    "plt.scatter(time_maes, freq_maes, alpha=0.5, label='MAE')\n",
    "plt.xlabel('Time-domain Error')\n",
    "plt.ylabel('Frequency-domain Error')\n",
    "plt.title('Comparison of Time-domain and Frequency-domain Errors')\n",
    "plt.legend()\n",
    "plt.grid()\n",
    "plt.show()     \n",
    "# compare mse and mae in same domain\n",
    "plt.scatter(time_mses, time_maes, alpha=0.5, label='Time-domain')\n",
    "plt.xlabel('MSE')       \n",
    "plt.ylabel('MAE')\n",
    "plt.title('Comparison of MSE and MAE in Time and Frequency Domains')\n",
    "plt.legend()\n",
    "plt.grid()\n",
    "plt.show()\n",
    "plt.scatter(freq_mses, freq_maes, alpha=0.5, label='Frequency-domain')\n",
    "plt.xlabel('MSE')       \n",
    "plt.ylabel('MAE')\n",
    "plt.title('Comparison of MSE and MAE in Time and Frequency Domains')\n",
    "plt.legend()\n",
    "plt.grid()\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
