def factor_covariance(risk_model_id: str,
                      factor_name_1: str,
                      factor_name_2: str,
                      *,
                      source: str = None,
                      real_time: bool = False,
                      request_id: Optional[str] = None) -> pd.Series:
    """
    Covariance time-series between two factors in a risk model

    :param risk_model_id: risk model entity
    :param factor_name_1: first factor name
    :param factor_name_2: second factor name
    :param source: name of function caller
    :param real_time: whether to retrieve intraday data instead of EOD
    :param request_id: server request id
    :return: Time-series of covariances between the two factors across available risk model dates
    """

    factor_1 = Factor.get(risk_model_id, factor_name_1)
    factor_2 = Factor.get(risk_model_id, factor_name_2)
    covariance_curve = factor_1.covariance(factor_2,
                                           DataContext.current.start_date,
                                           DataContext.current.end_date,
                                           ReturnFormat.JSON)
    return __format_plot_measure_results(covariance_curve,
                                         QueryType.COVARIANCE)
def factor_zscore(asset: Asset,
                  risk_model_id: str,
                  factor_name: str,
                  *,
                  source: str = None,
                  real_time: bool = False,
                  request_id: Optional[str] = None) -> pd.Series:
    """
    Asset factor exposure (in the form of z-scores) for a factor using specified risk model

    :param asset: asset object loaded from security master
    :param risk_model_id: requested risk model id
    :param factor_name: requested factor name
    :param source: name of function caller
    :param real_time: whether to retrieve intraday data instead of EOD
    :param request_id: service request id, if any
    :return: Time-series of asset factor exposure across available risk model dates
    """
    risk_model = FactorRiskModel.get(risk_model_id)
    factor = Factor.get(risk_model_id, factor_name)
    gsid = asset.get_identifier('GSID')

    # Query risk model data
    query_results = risk_model.get_data(
        measures=[
            Measure.Factor_Name, Measure.Universe_Factor_Exposure,
            Measure.Asset_Universe
        ],
        start_date=DataContext.current.start_time,
        end_date=DataContext.current.end_time,
        assets=DataAssetsRequest(identifier=UniverseIdentifier.gsid,
                                 universe=[gsid])).get('results', [])

    # Get the factor data from query results
    z_scores = {}
    for result in query_results:
        exposures = result.get('assetData', {}).get('factorExposure', [])
        if exposures:
            z_scores[result['date']] = exposures[0].get(factor.id)

    return __format_plot_measure_results(z_scores, QueryType.FACTOR_EXPOSURE)
def factor_volatility(risk_model_id: str,
                      factor_name: str,
                      *,
                      source: str = None,
                      real_time: bool = False,
                      request_id: Optional[str] = None) -> pd.Series:
    """
    Volatility timeseries for a factor in a risk model

    :param risk_model_id: risk model entity
    :param factor_name: factor name
    :param source: name of function caller
    :param real_time: whether to retrieve intra-day data instead of EOD
    :param request_id: server request id
    :return: Time-series of a factor's volatility across available risk model dates
    """
    factor = Factor.get(risk_model_id, factor_name)
    volatility = factor.volatility(DataContext.current.start_date,
                                   DataContext.current.end_date,
                                   ReturnFormat.JSON)
    return __format_plot_measure_results(volatility,
                                         QueryType.VOLATILITY,
                                         multiplier=100)
def factor_performance(risk_model_id: str,
                       factor_name: str,
                       *,
                       source: str = None,
                       real_time: bool = False,
                       request_id: Optional[str] = None) -> pd.Series:
    """
    Factor returns as a price time-series for a factor in a risk model

    :param risk_model_id: risk model entity
    :param factor_name: factor name
    :param source: name of function caller
    :param real_time: whether to retrieve intraday data instead of EOD
    :param request_id: server request id
    :return: Time-series of factor returns as a price series across available risk model dates
    """

    factor = Factor.get(risk_model_id, factor_name)
    factor_returns = factor.returns(DataContext.current.start_date,
                                    DataContext.current.end_date,
                                    ReturnFormat.JSON)
    factor_return_timeseries = pd.Series(factor_returns)
    return prices(factor_return_timeseries, 100)