{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Fitting Weibull Distributions\n\nThis example demonstrates how to fit Weibull parameters to a wind speed distribution\nusing the moments-based Weibull fitting method used by WAsP.\n\nLets start by fiting Weibull parameters to a known first moment (the mean wind speed), third moment, and the frequency fraction that is\ngreater than the mean (first moment). To fit the Weibull parameters, we will use the :py:func:`windkit.weibull.fit_weibull_wasp_m1_m3_fgtm` function.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import windkit as wk\nimport numpy as np\nfrom scipy.stats import weibull_min\nimport matplotlib.pyplot as plt\n\nfirst_moment = 7.0\nthird_moment = 600.0\nfreq_gt_mean = 0.46\n\nA, k = wk.weibull.fit_weibull_wasp_m1_m3_fgtm(first_moment, third_moment, freq_gt_mean)\nprint(f\"Weibull A: {A:.2f}, k: {k:.2f}\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Deriving relevant statistics from a wind speed distribution\nNow, let's go back and instead start from a wind speed distribution and derive the first moment, third moment,\nand frequency fraction greater than the mean. We will use a Weibull distribution with\nthe known parameters from above.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "size = 100_000  # number of samples\nws_samples = weibull_min.rvs(k, loc=0, scale=A, size=size, random_state=0)\n\nbins = np.linspace(0.0, 30.0, 31)\ncenters = 0.5 * (bins[:-1] + bins[1:])\nceils = np.ceil(centers)\n\nhist, bin_edges = np.histogram(ws_samples, bins=bins, density=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Caculate statistics\nNow, we can calculate the first moment, third moment, and the frequency fraction greater than the mean\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "first_moment = centers @ hist\nthird_moment = (centers**3) @ hist\ncdf = np.cumsum(hist)\nfreq_gt_mean = 1.0 - np.interp(first_moment, ceils, cdf)\n\n\nprint(f\"First Moment: {first_moment:.2f} m/s\")\nprint(f\"Third Moment: {third_moment:.2f} m^3/s^3\")\nprint(f\"Frequency Fraction Greater Than Mean: {freq_gt_mean:.2f}\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Fit Weibull distribution using the calculated moments\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "A_fit, k_fit = wk.weibull.fit_weibull_wasp_m1_m3_fgtm(\n    first_moment, third_moment, freq_gt_mean\n)\nprint(f\"Fitted Weibull A: {A_fit:.2f}, k: {k_fit:.2f}\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Visualizing the distribution\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "x = np.linspace(0, 30, 1000)\n\nplt.bar(\n    centers,\n    hist,\n    width=np.diff(bins),\n    align=\"center\",\n    alpha=0.5,\n    label=\"Sampled Distribution\",\n)\nplt.plot(\n    x,\n    weibull_min.pdf(x, k_fit, loc=0, scale=A_fit),\n    label=f\"Weibull Fit (A={A_fit:.2f}, k={k_fit:.2f})\",\n    color=\"red\",\n)\nplt.legend()\nplt.xlabel(\"Wind Speed (m/s)\")\nplt.ylabel(\"Probability Density\")\nplt.title(\"Weibull Distribution Fit\")"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "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.14.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}