def test_thematic_beta(): replace = Replacer() # mock getting PTA report mock = replace('gs_quant.markets.report.ThematicReport.get', Mock()) mock.return_value = ThematicReport(id='report_id') # mock getting thematic exposure mock = replace('gs_quant.markets.report.ThematicReport.get_thematic_betas', Mock()) mock.return_value = pd.DataFrame(thematic_data) # mock getting asset mock = Stock('MAA0NE9QX2ABETG6', 'Test Asset') xrefs = replace('gs_quant.timeseries.measures.GsAssetApi.get_asset_xrefs', Mock()) xrefs.return_value = [ GsTemporalXRef(datetime.date(2019, 1, 1), datetime.date(2952, 12, 31), XRef(ticker='basket_ticker', )) ] replace('gs_quant.markets.securities.SecurityMaster.get_asset', Mock()).return_value = mock with DataContext(datetime.date(2020, 7, 12), datetime.date(2020, 7, 15)): actual = mr.thematic_beta('report_id', 'basket_ticker') assert all(actual.values == [1, 1, 1, 1]) replace.restore()
def thematic_beta(report_id: str, basket_ticker: str, *, source: str = None, real_time: bool = False, request_id: Optional[str] = None) -> pd.Series: """ Thematic beta values of a portfolio to a requested GS thematic flagship basket :param report_id: portfolio thematic analytics report id :param basket_ticker: ticker for thematic basket :param source: name of function caller :param real_time: whether to retrieve intraday data instead of EOD :param request_id: server request id :return: Timeseries of daily thematic beta of portfolio to requested flagship basket """ thematic_report = ThematicReport.get(report_id) asset = SecurityMaster.get_asset(basket_ticker, AssetIdentifier.TICKER) df = thematic_report.get_thematic_betas( start_date=DataContext.current.start_date, end_date=DataContext.current.end_date, basket_ids=[asset.get_marquee_id()]) if not df.empty: df.set_index('date', inplace=True) df.index = pd.to_datetime(df.index) return _extract_series_from_df(df, QueryType.THEMATIC_BETA)
def get_thematic_report(self) -> ThematicReport: if self.positioned_entity_type in [EntityType.PORTFOLIO, EntityType.ASSET]: position_source_type = self.positioned_entity_type.value.capitalize() reports = GsReportApi.get_reports(limit=100, position_source_type=position_source_type, position_source_id=self.id, report_type=f'{position_source_type} Thematic Analytics') if len(reports) == 0: raise MqError(f'This {position_source_type} has no thematic analytics report.') return ThematicReport.from_target(reports[0]) raise NotImplementedError
position_source_id='PORTFOLIOID', report_type=ReportType.Portfolio_Factor_Risk, status=ReportStatus.done ) fake_ppa = PerformanceReport(report_id='PPAID', position_source_type=PositionSourceType.Portfolio, position_source_id='PORTFOLIOID', report_type=ReportType.Portfolio_Performance_Analytics, parameters=None, status=ReportStatus.done ) fake_pta = ThematicReport(report_id='PTAID', position_source_type=PositionSourceType.Portfolio, position_source_id='PORTFOLIOID', report_type=ReportType.Portfolio_Thematic_Analytics, parameters=None, status=ReportStatus.done ) factor_risk_results = [ { 'date': '2021-01-02', 'factor': 'factor1', 'pnl': 123, 'proportionOfRisk': 100, 'exposure': 200, 'annualRisk': 3928, 'dailyRisk': 202 }, {