Exemple #1
0
    def _create_mse_debug_chart(self, alphas, chosen_solution, mean_square_errors, min_se_solution):
        mse_chart = LineChart()
        mean_square_errors_paths = QFDataFrame(data=mean_square_errors, index=pd.Index(alphas))
        mse_chart.add_decorator(TitleDecorator("Cross-validated avg. MSE of Elastic Net fit"))
        for _, path in mean_square_errors_paths.iteritems():
            mse_chart.add_decorator(DataElementDecorator(path))
        mse_chart.add_decorator(VerticalLineDecorator(x=min_se_solution, linestyle='-.'))
        mse_chart.add_decorator(VerticalLineDecorator(x=chosen_solution, linestyle='-'))
        mse_chart.plot()
        mse_chart.axes.invert_xaxis()

        return mse_chart
Exemple #2
0
    def _create_coeffs_debug_chart(self, alphas, chosen_solution, coeffs_path, min_se_solution):
        coeffs_chart = LineChart()
        coefficients_paths = QFDataFrame(data=coeffs_path, index=pd.Index(alphas))
        for _, path in coefficients_paths.iteritems():
            coeffs_chart.add_decorator(DataElementDecorator(path))
        coeffs_chart.add_decorator(TitleDecorator("Elastic Net (using Cross Validation)"))
        coeffs_chart.add_decorator(VerticalLineDecorator(x=min_se_solution, linestyle='-.'))
        coeffs_chart.add_decorator(VerticalLineDecorator(x=chosen_solution, linestyle='-'))
        coeffs_chart.plot()
        coeffs_chart.axes.invert_xaxis()

        return coeffs_chart
Exemple #3
0
    def table_for_df(df: QFDataFrame,
                     frequency: Frequency = Frequency.DAILY) -> str:
        """
        df
            DataFrame of returns or prices of assets to be analysed

        frequency
            (optional) frequency of the returns or price sampling in the DataFrame. By default daily frequency is used

        Returns
        ----------
        returns a table similar to the one below:

        Analysed period: start_date - end_date, using frequency data
        Name            total_ret        cagr         vol      up_vol    down_vol ...
        Asset1              63.93       28.27       19.15       14.06       14.35 ...
        Asset2              66.26       29.19       20.74       14.86       15.54 ...
        Asset3              66.26       29.19       20.74       14.86       15.54 ...
        ...                   ...         ...         ...         ...         ... ...

        """
        name_ta_list = [(name, TimeseriesAnalysis(asset_tms, frequency))
                        for name, asset_tms in df.iteritems()]
        first_ta = name_ta_list[0][1]

        result = "Analysed period: {} - {}, using {} data\n".format(
            date_to_str(first_ta.start_date), date_to_str(first_ta.end_date),
            frequency)

        header_without_dates = ""
        for value in first_ta.get_short_names()[2:]:
            header_without_dates += '{:>12}'.format(value)

        result += ("{:12} {}\n".format("Name", header_without_dates))

        for name, ta in name_ta_list:
            values = ""
            for value in ta.get_measures()[2:]:
                values += '{:>12}'.format(value)
            result += ("{:12} {}\n".format(name.as_string(), values))
        return result
def main():
    # GENERATE DATA
    regressors_and_fund_df = QFDataFrame(data=[[1, 3, 5], [2, 3, 1], [3, 1, 2],
                                               [4, 2, 3], [5, 3, 4]],
                                         index=pd.bdate_range(
                                             start='2015-01-01', periods=5),
                                         columns=['a', 'b', 'c'])

    # add data to the chart and the legend
    marker_props_template = {'alpha': 0.5}
    stemline_props_template = {'linestyle': '-.', 'linewidth': 0.2}
    baseline_props = {'visible': True}

    colors = cycle(Chart.get_axes_colors())
    chart = LineChart(start_x=str_to_date('2014-12-31'),
                      end_x=str_to_date('2015-01-08'))
    legend = LegendDecorator()

    for name, series in regressors_and_fund_df.iteritems():
        marker_props = marker_props_template.copy()
        stemline_props = stemline_props_template.copy()

        color = next(colors)
        marker_props['markeredgecolor'] = color
        marker_props['markerfacecolor'] = color
        stemline_props['color'] = color
        data_elem = StemDecorator(series,
                                  marker_props=marker_props,
                                  stemline_props=stemline_props,
                                  baseline_props=baseline_props)
        chart.add_decorator(data_elem)
        legend.add_entry(data_elem, name)

    chart.add_decorator(legend)
    chart.plot()

    plt.show(block=True)