<jupyter_start><jupyter_text>Pull Portfolio Performance and Factor Risk DataThis tutorial uses a pre-populated portfolio (accessible to all internal users) and walks through pulling performance and factor risk data *(for demo, not client distribution, purposes)*.  Step 1: Authenticate and Initialize Your Session<jupyter_code>import datetime as dt

from gs_quant.markets.portfolio_manager import PortfolioManager
from gs_quant.markets.report import PerformanceReport, FactorRiskReport
from gs_quant.session import GsSession, Environment

GsSession.use(Environment.PROD)<jupyter_output><empty_output><jupyter_text>Step 2: Get all Portfolio ReportsThe `PortfolioManager` class allows for easy retrieval and scheduling of portfolio reports. Simply running:<jupyter_code>all_reports = PortfolioManager('MPZV9A0F1EMQGG79').get_reports()<jupyter_output><empty_output><jupyter_text>will return a list of `Report` objects that represent the reports associated with the portfolio. Step 3: Find Portfolio Performance Analytics Report and Pull DataThe GS Quant `Report` class is inherited by report subclasses, like `FactorRiskReport` and `PerformanceReport`, each of which corresponds to a type of Marquee report. Each subclass then has additional functions specific to its report type. In this case, we'd like to find the `PerformanceReport` associated with this portfolio, and leverage its functions to retrieve data. In this example, we will pull all historical PnL, gross exposure, and net exposure available.<jupyter_code>performance_report = list(filter(lambda report: isinstance(report, PerformanceReport), all_reports))[0]
pnl = performance_report.get_pnl(start_date=dt.date(2021, 1, 1), end_date=dt.date(2021, 6, 1))
gross_exposure = performance_report.get_gross_exposure(start_date=dt.date(2021, 1, 1), end_date=dt.date(2021, 6, 1))
net_exposure = performance_report.get_net_exposure(start_date=dt.date(2021, 1, 1), end_date=dt.date(2021, 6, 1))

print(f'PnL: \n{pnl.__str__()}')
print(f'Gross Exposure: \n{gross_exposure.__str__()}')
print(f'Net Exposure: \n{net_exposure.__str__()}')<jupyter_output><empty_output><jupyter_text>Step 4: Find Factor Risk Report and Pull DataNow let's find a factor risk report and pull its correlating `FactorRiskReport`.<jupyter_code>risk_model_id = 'BARRA_USSLOWL'
factor_risk_reports = list(filter(lambda report: isinstance(report, FactorRiskReport) and report.get_risk_model_id() == risk_model_id, all_reports))
if len(factor_risk_reports) == 0:
    print(f'This portfolio does not have a factor risk report with the risk model {risk_model_id}. Please create a new factor risk report and schedule it before proceeding.')
factor_risk_report = factor_risk_reports[0]

print(f'Factor risk model found with ID: "{factor_risk_report.id}".')<jupyter_output><empty_output><jupyter_text>Now that we have our factor risk report, we can leverage the functionality of the `FactorRiskReport` class to pull attribution and risk data. In this example, let's pull historical data on factor, specific, and total PnL for the first five months of 2021:<jupyter_code>factor_pnl = factor_risk_report.get_factor_pnl(factor_name='Factor', start_date=dt.date(2021, 1, 1), end_date=dt.date(2021, 6, 1))
specific_pnl = factor_risk_report.get_factor_pnl(factor_name='Specific', start_date=dt.date(2021, 1, 1), end_date=dt.date(2021, 6, 1))
total_pnl = factor_risk_report.get_factor_pnl(factor_name='Total', start_date=dt.date(2021, 1, 1), end_date=dt.date(2021, 6, 1))

print(f'Factor: \n{factor_pnl.__str__()}')
print(f'Specific: \n{specific_pnl.__str__()}')
print(f'Total: \n{total_pnl.__str__()}')<jupyter_output><empty_output><jupyter_text>Now let's pull the breakdown of proportion of risk among the different factor types over time:<jupyter_code>market_prop_of_risk = factor_risk_report.get_factor_proportion_of_risk(factor_name='Market', start_date=dt.date(2021, 1, 1), end_date=dt.date(2021, 6, 1))
style_prop_of_risk = factor_risk_report.get_factor_proportion_of_risk(factor_name='Style', start_date=dt.date(2021, 1, 1), end_date=dt.date(2021, 6, 1))
industry_prop_of_risk = factor_risk_report.get_factor_proportion_of_risk(factor_name='Industry', start_date=dt.date(2021, 1, 1), end_date=dt.date(2021, 6, 1))
country_prop_of_risk = factor_risk_report.get_factor_proportion_of_risk(factor_name='Country', start_date=dt.date(2021, 1, 1), end_date=dt.date(2021, 6, 1))

print(f'Market: \n{market_prop_of_risk.__str__()}')
print(f'Style: \n{style_prop_of_risk.__str__()}')
print(f'Industry: \n{industry_prop_of_risk.__str__()}')
print(f'Country: \n{country_prop_of_risk.__str__()}')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10583153347732181, 'fraction_numerical': 0.02937365010799136, 'mean_word_length': 8.336693548387096, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '292241', 'license': 'Apache-2.0', 'n_tokens_mistral': 1688, 'n_tokens_neox': 1509, 'n_words': 395, 'path': 'gs_quant/documentation/03_portfolios/tutorials/Pull Portfolio Risk Data.ipynb', 'repo_name': 'dannyb2018/gs-quant', 'size': '4630'}