EWM === .. cpp:class:: pandas::EWM Window operation class for rolling/expanding calculations. Example ------- .. code-block:: cpp #include using namespace pandas; // Use EWM EWM obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``EWM(const Series& series, double alpha, bool adjust, bool ignore_na, size_t min_periods)`` - pd_ewm.h:56 - Construction ------------ .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``static EWM from_alpha(const Series& series, double alpha, bool adjust = true, bool ignore_na = false, size_t min_periods = 0)`` - static EWM - pd_ewm.h:128 - * - ``static EWM from_com(const Series& series, double com, bool adjust = true, bool ignore_na = false, size_t min_periods = 0)`` - static EWM - pd_ewm.h:94 - * - ``static EWM from_halflife(const Series& series, double halflife, bool adjust = true, bool ignore_na = false, size_t min_periods = 0)`` - static EWM - pd_ewm.h:111 - * - ``static EWM from_span(const Series& series, double span, bool adjust = true, bool ignore_na = false, size_t min_periods = 0)`` - static EWM - pd_ewm.h:77 - * - ``static EWM from_time_halflife(const Series& series, std::vector timestamps, double halflife_seconds, bool adjust = true, bool ignore_na = false, size_t min_periods = 0)`` - static EWM - pd_ewm.h:146 - Indexing / Selection -------------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``double get_value(size_t i) const`` - double - pd_ewm.h:498 - :ref:`View ` Statistics ---------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``Series mean() const`` - Series - pd_ewm.h:170 - :ref:`View ` * - ``size_t min_periods() const`` - size_t - pd_ewm.h:491 - * - ``Series std_(int ddof = 1) const`` - Series - pd_ewm.h:346 - :ref:`View ` * - ``Series var(int ddof = 1) const`` - Series - pd_ewm.h:259 - :ref:`View ` Aggregation ----------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``EWM ewm(series, 0.5, adjust, ignore_na, min_periods)`` - EWM - pd_ewm.h:155 - :ref:`View ` Other Methods ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``bool adjust() const`` - bool - pd_ewm.h:489 - * - ``double alpha() const`` - double - pd_ewm.h:488 - * - ``Series corr(const Series& other) const`` - Series - pd_ewm.h:454 - :ref:`View ` * - ``Series cov(const Series& other, int ddof = 1) const`` - Series - pd_ewm.h:364 - :ref:`View ` * - ``bool ignore_na() const`` - bool - pd_ewm.h:490 - * - ``propagate_source_index(res)`` - - pd_ewm.h:174 - * - ``void propagate_source_index(Series& result) const`` - void - pd_ewm.h:509 - * - ``propagate_source_index(res)`` - - pd_ewm.h:583 - * - ``propagate_source_index(res)`` - - pd_ewm.h:663 - * - ``propagate_source_index(res)`` - - pd_ewm.h:740 - * - ``const Series& series() const`` - const Series& - pd_ewm.h:492 - :ref:`View ` * - ``void set_precomputed_mean(std::vector values)`` - void - pd_ewm.h:162 - * - ``Series time_based_cov(const Series& other, int ddof = 1) const`` - Series - pd_ewm.h:670 - * - ``Series time_based_mean() const`` - Series - pd_ewm.h:539 - * - ``Series time_based_var(int ddof = 1) const`` - Series - pd_ewm.h:590 - * - ``double time_decay(size_t i, size_t prev_i) const`` - double - pd_ewm.h:530 - Code Examples ------------- The following examples are extracted from the test suite. .. _example-ewm-get_value-0: .. dropdown:: get_value (pd_test_2_all.cpp:17379) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 17369 :emphasize-lines: 11 namespace dataframe_tests_transform { // Helper to check if two doubles are approximately equal bool approx_equal(double a, double b, double tol = 1e-9) { if (std::isnan(a) && std::isnan(b)) return true; if (std::isnan(a) || std::isnan(b)) return false; return std::abs(a - b) < tol; } // Helper to get double value from DataFrame double get_value(const pandas::DataFrame& df, size_t row, size_t col) { return df.iloc(row, col); } void pd_test_transform_callable() { std::cout << "========= transform with callable ====================="; std::map> data = { {"A", {1.0, 4.0, 9.0, 16.0}}, {"B", {2.0, 3.0, 4.0, 5.0}} }; .. _example-ewm-mean-1: .. dropdown:: mean (pd_test_1_all.cpp:282) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 272 :emphasize-lines: 11 std::optional(true), std::optional(true) }); auto s = arr.sum(); if (!s.has_value() || s.value() != 3) { std::cout << " [FAIL] : in pd_test_boolean_array_reductions() : sum should be 3" << std::endl; throw std::runtime_error("pd_test_boolean_array_reductions failed: sum"); } auto m = arr.mean(); if (!m.has_value() || std::abs(m.value() - 0.75) > 0.001) { std::cout << " [FAIL] : in pd_test_boolean_array_reductions() : mean should be 0.75" << std::endl; throw std::runtime_error("pd_test_boolean_array_reductions failed: mean"); } std::cout << " -> tests passed" << std::endl; } void pd_test_boolean_array_dtype() { std::cout << "========= BooleanArray: dtype ======================= "; .. _example-ewm-std_-2: .. dropdown:: std_ (pd_test_1_all.cpp:20752) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 20742 :emphasize-lines: 11 throw std::runtime_error("pd_test_rolling_min_periods failed: with min_periods=1, idx 1 should be 3.0"); } std::cout << " -> tests passed" << std::endl; } void pd_test_rolling_std() { std::cout << "========= Rolling std ==========================="; pandas::Series s({1.0, 2.0, 3.0, 4.0, 5.0}); auto result = s.rolling(3).std_(); // std([1,2,3]) = 1.0 (ddof=1) // std([2,3,4]) = 1.0 // std([3,4,5]) = 1.0 bool passed = std::abs(result[2] - 1.0) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_rolling_std() : rolling std should be 1.0" << std::endl; throw std::runtime_error("pd_test_rolling_std failed: rolling std should be 1.0"); } .. _example-ewm-var-3: .. dropdown:: var (pd_test_1_all.cpp:20890) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 20880 :emphasize-lines: 11 throw std::runtime_error("pd_test_expanding_std failed: expanding std values incorrect"); } std::cout << " -> tests passed" << std::endl; } void pd_test_expanding_var() { std::cout << "========= Expanding var ========================="; pandas::Series s({1.0, 2.0, 3.0, 4.0, 5.0}); auto result = s.expanding().var(); // Expanding var (ddof=1): NaN, 0.5, 1.0, 1.6667, 2.5 bool passed = std::isnan(result[0]) && std::abs(result[1] - 0.5) < 0.001 && std::abs(result[2] - 1.0) < 0.001 && std::abs(result[3] - 1.6667) < 0.001 && std::abs(result[4] - 2.5) < 0.001; if (!passed) { std::cout << " [FAIL] : in pd_test_expanding_var() : expanding var values incorrect" << std::endl; throw std::runtime_error("pd_test_expanding_var failed: expanding var values incorrect"); .. _example-ewm-ewm-4: .. dropdown:: ewm (pd_test_3_all.cpp:2961) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2951 :emphasize-lines: 11 // Test expanding sum pandas::DataFrame expanding_sum = df.expanding().sum(); if (expanding_sum.nrows() != 5 || expanding_sum.ncols() != 2) { throw std::runtime_error("expanding().sum() shape failed"); } std::cout << " -> tests passed" << std::endl; } void pd_test_3_all_df_ewm() { std::cout << "========= DataFrame.ewm() ================================"; std::map> data = { {"A", {1.0, 2.0, 3.0, 4.0, 5.0}}, {"B", {10.0, 20.0, 30.0, 40.0, 50.0}} }; pandas::DataFrame df(data); // Test ewm mean with span=3 pandas::DataFrame ewm_mean = df.ewm(std::nullopt, 3.0).mean(); if (ewm_mean.nrows() != 5 || ewm_mean.ncols() != 2) { .. _example-ewm-corr-5: .. dropdown:: corr (pd_test_1_all.cpp:4655) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4645 :emphasize-lines: 11 } void pd_test_aggregation_dataframe_corr() { std::cout << "========= DataFrame corr ========================"; std::map> data; data["A"] = {1.0, 2.0, 3.0, 4.0, 5.0}; data["B"] = {2.0, 4.0, 6.0, 8.0, 10.0}; // Perfect correlation pandas::DataFrame df(data); auto corr_df = df.corr(); // Check dimensions bool passed = corr_df.nrows() == 2 && corr_df.ncols() == 2; if (!passed) { std::cout << " [FAIL] : in pd_test_aggregation_dataframe_corr() : corr should be 2x2" << std::endl; throw std::runtime_error("pd_test_aggregation_dataframe_corr failed: corr should be 2x2"); } // Diagonal should be 1.0 std::string aa = corr_df["A"].get_value_str(0); .. _example-ewm-cov-6: .. dropdown:: cov (pd_test_1_all.cpp:4690) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 4680 :emphasize-lines: 11 std::cout << " -> tests passed" << std::endl; } void pd_test_aggregation_dataframe_cov() { std::cout << "========= DataFrame cov ========================="; std::map> data; data["A"] = {1.0, 2.0, 3.0}; pandas::DataFrame df(data); auto cov_df = df.cov(); // Check dimensions bool passed = cov_df.nrows() == 1 && cov_df.ncols() == 1; if (!passed) { std::cout << " [FAIL] : in pd_test_aggregation_dataframe_cov() : cov should be 1x1" << std::endl; throw std::runtime_error("pd_test_aggregation_dataframe_cov failed: cov should be 1x1"); } // Var(A) = 1.0 with ddof=1 std::string aa = cov_df["A"].get_value_str(0); .. _example-ewm-series-7: .. dropdown:: series (pd_test_2_all.cpp:2307) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 2297 :emphasize-lines: 11 std::vector index = {"a", "b", "c", "d", "e"}; std::map> data1; data1["col1"] = {1.0, 2.0, 3.0, 4.0, 5.0}; data1["col2"] = {2.0, 4.0, 6.0, 8.0, 10.0}; // Perfectly correlated with col1 pandas::DataFrame df1(data1, std::make_unique>(index)); // Series with same index and values that correlate with df columns pandas::Series series({1.0, 2.0, 3.0, 4.0, 5.0}); series.set_index(pandas::Index(index)); pandas::Series result = df1.corrwith(series); bool passed = true; // col1 should have correlation 1.0 with series if (!approx_equal(result[0], 1.0)) { std::cout << "\n [FAIL] : Expected correlation 1.0 for col1, got " << result[0] << std::endl; passed = false; }