Wind direction validation / accuracy method / function

Hi,
I want to share a piece of code that computes accuracy of wind direction. As this is a circular variable, it needs special approach. It would be great to know if this is something you are using similar or you are using other approaches. Thank you

"""
We need two time series with dimensions 'time' and wind speed direction ('Dir')
and wind speed sector ('sector') variables.

To take into account the time series itself and not only the wind speed
distribution, the populations of each sector in the reference time series
are used for adjusting each sector's importance in the time series.

Therefore, if we define the error (anomaly) as the difference between each
model's wind speed sector and the reference's, and we use the reference
populations as their respective weights, these will emphasize anomalies that
occur more frequently, and the total sum will manage not only each anomaly but
also each contribution to the analyzed period.
"""

import pandas as pd
import numpy as np


def get_population(dataframe: pd.DataFrame, to_group: str, by: str) \
        -> pd.Series:
    grouped = dataframe[[to_group, by]].groupby(by=by)
    return grouped.count()[to_group] * 100 / dataframe.shape[0]


def compute_wmse(model: np.array, reference: np.array, weights: np.array) \
        -> np.array:
    squared_errors = ((reference - model) ** 2).fillna(reference ** 2)
    return np.average(a=squared_errors, weights=weights) / 100


def wind_rose_anomaly(model, reference):
    reference_sectors_population = get_population(dataframe=reference,
                                                  to_group='Dir',
                                                  by='sector').to_numpy()
    model_sectors_populations = get_population(dataframe=model,
                                               to_group='Dir',
                                               by='sector').to_numpy()

    return compute_wmse(model=model_sectors_populations,
                        reference=reference_sectors_population,
                        weights=reference_sectors_population)
1 Like