> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bigdata.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Risk Analyzer

> Identifying Corporate Exposure to Risks

## Why It Matters

Understanding how companies are exposed to highly uncertain scenarios and risk channels, like geopolitical and economic risks, is critical for informed decision-making. As shifting policies, sanctions, and trade barriers redefine market dynamics, organizations must proactively assess their vulnerability to emerging threats.

## What It Does

The cookbook REST modules (`src/bigdata_rest.py`, `src/search_helper.py`, and `src/labeling.py`) combined with the Bigdata.com REST API and `bigdata-smart-batching` systematically analyze corporate exposure to specific risk channels using unstructured data from news, earnings calls, and regulatory filings. Designed for risk analysts, portfolio managers, and investment professionals, the workflow replaces the removed `RiskAnalyzer` SDK class with composable REST + OpenAI functions.

## How It Works

The `RiskAnalyzer` combines **hybrid semantic search**, **risk factor taxonomies**, and **structured validation techniques** to deliver:

* **Targeted extraction of risk signals** and supporting evidence from massive unstructured datasets
* **Standardized exposure metrics** to compare risk across firms, sectors, or portfolios
* **Actionable insights** that inform investment strategies and enterprise risk decisions
* **Time-based monitoring** to track how exposure levels shift in response to world events

## A Real-World Use Case

This cookbook illustrates the full workflow through a practical example: identifying companies impacted by new U.S. import tariffs on China. You'll learn how to convert unstructured narrative (news articles) into structured, quantifiable risk intelligence.

**Ready to get started? Let's dive in!**

<div style={{display: 'flex', gap: '10px', alignItems: 'center', margin: '0', lineHeight: '1'}}>
  <a href="https://colab.research.google.com/drive/1vvUVAeKAA1GiANJmPhpWHhOxvwz7ed0c?usp=sharing" target="_blank" style={{textDecoration: 'none'}}>
    <img alt="Open in Colab" noZoom src="https://colab.research.google.com/assets/colab-badge.svg" />
  </a>

  <a href="https://github.com/Bigdata-com/bigdata-cookbook/tree/main/Risk_Analyzer" target="_blank" style={{textDecoration: 'none'}}>
    <img alt="Open in GitHub" noZoom src="https://img.shields.io/badge/GitHub-View%20Repository-black?style=flat&logo=github" />
  </a>
</div>

## Prerequisites

To run the Risk Analyzer workflow, you can choose between three options:

* ▶️ **Colab cookbook**
  * Use this if you prefer running the workflow in a cloud environment.
  * Follow the instructions written directly inside the cookbook.
  * API keys must be configured as described within the Colab file itself.

* 💻 **GitHub cookbook**
  * Use this if you prefer working locally or in a custom environment.
  * Follow the setup and execution instructions in the [`README.md`](https://github.com/Bigdata-com/bigdata-cookbook/blob/main/Risk_Analyzer/README.md).
  * Copy `.env.example` to `.env` and set:
    * `BIGDATA_API_KEY` — your [Bigdata.com API key](https://docs.bigdata.com/api-reference/introduction#api-key)
    * `OPENAI_API_KEY` — your OpenAI API key (required for LLM labeling and summarization)
    * See the cookbook [`README.md`](https://github.com/Bigdata-com/bigdata-cookbook/blob/main/Risk_Analyzer/README.md) for full setup instructions.

* 🐳 **Docker Installation**
  * [Docker installation](https://github.com/Bigdata-com/bigdata-cookbook/blob/main/Risk_Analyzer/README.md#docker-installation-and-usage) is available for containerized deployment.
  * Provides an alternative setup method with containerized deployment, simplifying the environment configuration for those preferring Docker-based solutions.

## Setup and Imports

Below is the Python code required for setting up our environment and importing necessary libraries.

```python [expandable] theme={null}
import os
import json

import pandas as pd
from dotenv import load_dotenv
from pathlib import Path
from openai import OpenAI

from src.bigdata_rest import BigdataRestClient, load_universe, company_ids_from_universe
from src.search_helper import run_universe_search
from src.labeling import generate_risk_scenarios, classify_risk_chunks, build_company_risk_matrix
from src.tool import display_figures

load_dotenv(Path.cwd() / ".env")
BIGDATA_API_KEY = os.getenv("BIGDATA_API_KEY")
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")

rest_client = BigdataRestClient()
openai_client = OpenAI(api_key=OPENAI_API_KEY)

# Define output file paths for our results
output_dir = "output"
os.makedirs(output_dir, exist_ok=True)

export_path = f"{output_dir}/risk_analyzer_results.xlsx"
```

## Defining Your Risk Analysis Parameters

To perform a portfolio risk analysis, we need to define several key parameters:

* **Main Theme** (`main_theme`): The risk scenario to analyze (e.g. US Import Tariffs against China)
* **Focus** (`focus`): The analyst focus that provides an expert perspective on the scenario and helps break it down into risk factors
* **Company Universe** (`companies`): The set of companies to screen
* **Control Entities** (`control_entities`): The countries, people, or organizations that characterize the risk scenario
* **Keywords** (`keywords`): The key concepts of the risk scenario
* **Time Period** (`start_date` and `end_date`): The date range over which to run the search
* **Document Type** (`document_type`): Specify which documents to search over (transcripts, filings, news)
* **Fiscal Year** (`fiscal_year`): If the document type is transcripts or filings, fiscal year needs to be specified
* **Sources** (`sources`): Specify set of sources within a document type, for example which news outlets (available via Bigdata API) you wish to search over
* **Model Selection** (`llm_model`): The AI model used for semantic analysis
* **Rerank Threshold** (`rerank_threshold`): By setting this value, you're enabling the cross-encoder which reranks the results and selects those whose relevance is above the percentile you specify (0.7 being the 70th percentile). More information on the re-ranker can be found [here](https://sdk.bigdata.com/en/latest/how_to_guides/rerank_search.html).
* **Export Path** (`export_path`): The path to export the results in an Excel file

```python theme={null}
# Risk Definition  
main_theme = 'New US import tariffs against China would impact American companies'
focus = "Provide a detailed taxonomy of risks describing how new American import tariffs against China will impact US companies, their operations and strategy. Cover trade-relations risks, foreign market access risks, supply chain risks, US market sales and revenue risks (including price impacts), and intellectual property risks, provide at least 4 sub-scenarios for each risk factor."

# Company Universe (CSV, not a watchlist)
universe = load_universe("../Thematic_Screener_CLI/40_companies.csv")
company_ids = company_ids_from_universe(universe)
id_to_name = dict(zip(universe["RP_ENTITY_ID"], universe["COMPANY_NAME"]))

# LLM Specification (gpt-5.6-luna: omit temperature/top_p)
llm_model = "gpt-5.6-luna"

# Query Configuration  
scope = "news"  # REST document scope

# Specify Time Range  
start_date = "2025-04-01"
end_date = "2025-06-30"

# Risk Scenario Parameters (contextual; not passed as REST filters)
countries_at_risk = {'place':['China']}
keywords = ['Tariffs']

chunk_percentage = 0.02
requests_per_minute = 200
```

## Generate a Risk Taxonomy

Use OpenAI via `generate_risk_scenarios` to break the main theme into sub-scenarios:

```python theme={null}
risk_scenarios = generate_risk_scenarios(
    main_theme, focus=focus, n=4, model=llm_model, client=openai_client
)
```

## Retrieve Content

With the risk taxonomy and screening parameters, use `run_universe_search` (`src/search_helper.py` + `bigdata-smart-batching`) to retrieve news chunks for your company universe:

```python theme={null}
df_sentences = run_universe_search(
    company_ids,
    risk_scenarios,
    start_date=start_date,
    end_date=end_date,
    scope=scope,
    chunk_percentage=chunk_percentage,
    requests_per_minute=requests_per_minute,
    id_to_name=id_to_name,
)
```

## Label the Results

Classify each chunk against the risk sub-scenarios with `classify_risk_chunks`:

```python theme={null}
df_labeled = classify_risk_chunks(
    df_sentences,
    risk_scenarios,
    main_theme,
    model=llm_model,
    client=openai_client,
)
```

## Assess Risk Exposure

Build the company-level risk matrix and visualize results:

```python theme={null}
df_company = build_company_risk_matrix(
    df_labeled,
    risk_scenarios,
    industry_by_id=industry_by_id,
    sector_by_id=sector_by_id,
)

display_figures(df_company, interactive=False, n_companies=len(df_company))
```

<Frame>
  <img src="https://mintcdn.com/ravenpackinternational/CNBS3sA25r4pc1xD/images/risk-analyzer/risk_heatmap.png?fit=max&auto=format&n=CNBS3sA25r4pc1xD&q=85&s=c558f259ec8ece1395e8f69654aaea92" alt="Risk exposure heatmap" width="1200" height="600" data-path="images/risk-analyzer/risk_heatmap.png" />
</Frame>

<Frame>
  <img src="https://mintcdn.com/ravenpackinternational/CNBS3sA25r4pc1xD/images/risk-analyzer/risk_total_by_comp.png?fit=max&auto=format&n=CNBS3sA25r4pc1xD&q=85&s=9e5eb7425bcd6c3189a19626aa16cd4a" alt="Risk exposure score" width="1200" height="600" data-path="images/risk-analyzer/risk_total_by_comp.png" />
</Frame>

<Frame>
  <img src="https://mintcdn.com/ravenpackinternational/CNBS3sA25r4pc1xD/images/risk-analyzer/risk_top_by_comp.png?fit=max&auto=format&n=CNBS3sA25r4pc1xD&q=85&s=8fa3742ebc21f87afd9d8a976de315c0" alt="top Risk thematics" width="1200" height="600" data-path="images/risk-analyzer/risk_top_by_comp.png" />
</Frame>

<Frame>
  <img src="https://mintcdn.com/ravenpackinternational/CNBS3sA25r4pc1xD/images/risk-analyzer/risk_subscenario_total.png?fit=max&auto=format&n=CNBS3sA25r4pc1xD&q=85&s=390457cee1bdd69ee8e1a7085001d688" alt="Risk scores" width="1200" height="600" data-path="images/risk-analyzer/risk_subscenario_total.png" />
</Frame>

<Frame>
  <img src="https://mintcdn.com/ravenpackinternational/CNBS3sA25r4pc1xD/images/risk-analyzer/risk_industry_heatmap.png?fit=max&auto=format&n=CNBS3sA25r4pc1xD&q=85&s=34f02ae18c93196fda1f2aea3fb3563a" alt="Industry-level Risk exposure heatmap" width="1200" height="500" data-path="images/risk-analyzer/risk_industry_heatmap.png" />
</Frame>

## Extract Key Insights

The analysis reveals key insights about corporate exposure to U.S. import tariffs against China:

<Card title="Supply Chain Dependencies Drive Exposure">
  Companies with heavy reliance on Chinese manufacturing and supply chains show the highest exposure scores, indicating vulnerability to cost increases and operational disruptions from new tariff policies.
</Card>

<Card title="Technology Sector Shows Concentrated Risk">
  Technology companies demonstrate significant exposure due to their dependence on Chinese semiconductor and component manufacturing, with potential impacts on both costs and market access.
</Card>

<Card title="Consumer Goods Face Price Pressure">
  Consumer-facing companies show exposure through potential margin compression as they navigate between absorbing tariff costs and passing them on to customers.
</Card>

<Card title="Strategic Positioning Varies Widely">
  Companies with diversified supply chains and domestic alternatives show lower risk scores, highlighting the importance of supply chain resilience strategies.
</Card>

### Industry Risk Patterns

#### High-Risk Sectors

* **Technology and Semiconductors** show the highest average exposure due to supply chain concentration in China
* **Consumer Discretionary** companies face significant margin pressure from potential tariff costs
* **Industrial Manufacturing** with Chinese operations face operational complexity increases

#### Strategic Responses

* Companies with **supply chain diversification** strategies show lower risk scores
* Firms with **domestic manufacturing capabilities** demonstrate greater resilience
* Organizations with **flexible sourcing strategies** appear better positioned to navigate tariff impacts

## Export the Results

Export labeled results and the company risk matrix to CSV or Excel for further analysis.

```python theme={null}
df_labeled.to_csv(f"{output_dir}/risk_labeled.csv", index=False)
df_company.to_csv(f"{output_dir}/risk_company_matrix.csv", index=False)
```

## Conclusion

The Risk Analyzer provides a comprehensive framework for identifying and quantifying corporate exposure to specific risk scenarios. By leveraging advanced information retrieval and LLM-powered analysis, this workflow transforms unstructured data into actionable risk intelligence.

Through the automated analysis of U.S. import tariff exposure, you can:

1. **Identify vulnerable companies** - Discover which firms in your portfolio face the highest exposure to tariff-related risks through their operational dependencies and market positions

2. **Compare across industries** - Understand how different sectors are affected by trade policy changes, enabling sector-level hedging and diversification strategies

3. **Monitor risk evolution** - Track how company exposure changes over time as they adapt their strategies or as policy developments unfold

4. **Generate investment insights** - Use risk exposure scores to inform position sizing, hedging decisions, and portfolio construction in volatile geopolitical environments

5. **Support risk management** - Provide quantitative backing for risk committee discussions and regulatory reporting requirements

**Investment Strategy Implications:**

* Consider underweighting companies with high exposure scores in anticipation of tariff implementation
* Use sector-level exposure analysis to guide allocation decisions and hedging strategies
* Monitor risk score changes to identify companies successfully adapting to trade policy challenges

Whether you're conducting portfolio stress testing, building risk-aware investment strategies, or assessing geopolitical exposure across your holdings, the Risk Analyzer automates the research process while maintaining the depth and rigor required for professional risk analysis. The standardized scoring methodology ensures consistent evaluation across companies, sectors, and time periods, making it an invaluable tool for systematic risk assessment in an increasingly complex global environment.
