menu
Contribute

Functions

GS Quant uses Sphinx to generate python API documentation automatically from the source code.

Type Hinting

Since Python 3.5, functions can use type hinting to document both parameter and return types:

import pandas as pd

def abs(x: pd.Series) -> pd.Series:
    """Docstring"""

    # function code

Each variable can be typed through the syntax param: type. The abs function above has a parameter x which is documented as type pd.Series, which is a pandas Series object. The -> syntax is used to declare the return type of the function. The abs function above also returns a pandas Series object.

Docstring

Functions should use Docstrings per PEP 257 standard to document usage. Python function documentation should have the following components:

  • Function title
  • Parameters and return type
  • Usage info
  • Examples
  • See also

Here's an example of a doc string:

def my_func(x: int) -> str:
    """
    Title / one line description

    :param x: description of parameter x
    :return: description of return value

    **Usage**

    Describe how to use the function should be used

    **Examples**

    Provide one or more examples:

    result = my_func(1)

    **See also**

    :func:`his_func` :func:`her_func`
    """
    # function code

Mathematical Formulas

The GS Quant documentation supports inline LaTex within python docstrings in order to render mathematical formulae. This can be declared using the :math: declaration. For example:

import pandas as pd

def abs(x: pd.Series) -> pd.Series:
    """Absolute value

    :math:`R_t = |X_t|`
    """
    # function code

Everything inside the math block quote marks will be rendered as LaTeX-formatted math.

Full Example

See below for an example of a fully documented function:

def abs(x: pd.Series) -> pd.Series:
    """
    Absolute value of each element in series

    :param x: date-based time series of prices
    :return: date-based time series of absolute value

    **Usage**

    Return the absolute value of :math:`X`. For each value in time series :math:`X_t`, return :math:`X_t` if :math:`X_t`
    is greater than or equal to 0; otherwise return :math:`-X_t`:

    :math:`R_t = |X_t|`

    Equivalent to :math:`R_t = \sqrt{X_t^2}`

    **Examples**

    Generate price series and take absolute value of :math:`X_t-100`

    >>> prices = generate_series(100) - 100
    >>> abs_(prices)

    **See also**

    :func:`exp` :func:`sqrt`

    """
    return abs(x)

Related Content


This site is for informational purposes only and does not constitute an offer to sell, or the solicitation of an offer to buy, any security. The Goldman Sachs Marquee® platform is for institutional and professional clients only. Some of the services and products described on this site may not be available in certain jurisdictions or to certain types of client. Please contact your Goldman Sachs sales representative with any questions. Nothing on this site constitutes an offer, or an invitation to make an offer from Goldman Sachs to purchase or sell a product. This site is given for purely indicative purposes and does not create any contractual relationship between you and Goldman Sachs. Any market information contained on the site (including but not limited to pricing levels) is based on data available to Goldman Sachs at a given moment and may change from time to time. There is no representation that any transaction can or could have been effected on such terms or at such prices. Please see https://www.goldmansachs.com/disclaimer/sec-div-disclaimers-for-electronic-comms.html for additional information. © 2023 Goldman Sachs. All rights reserved.
Transaction Banking services are offered by Goldman Sachs Bank USA (“GS Bank”). GS Bank is a New York State chartered bank, a member of the Federal Reserve System and a Member FDIC. © 2023 Goldman Sachs. All rights reserved.
Not all products and functionality mentioned on this website are currently available through our API platform.
All loans and deposit products are provided by Goldman Sachs Bank USA, Salt Lake City Branch. Member FDIC.
Brokerage and investment advisory services offered by our investment products are provided by Goldman Sachs & Co. LLC (`‘GS&CO.`’), which is an SEC registered broker-dealer and investment adviser, and member FINRA/SIPC. Research our firm at FINRA's BrokerCheck. Custody and clearing services are provided by Apex Clearing Corporation, a registered broker-dealer and member FINRA/SIPC. Please consider your objectives before investing. A diversified portfolio does not ensure a profit or protect against a loss. Past performance does not guarantee future results. Investment outcomes and projections are forward-looking statements and hypothetical in nature. Neither this website nor any of its contents shall constitute an offer, solicitation, or advice to buy or sell securities in any jurisdictions where GS&Co. is not registered. Any information provided prior to opening an investment account is on the basis that it will not constitute investment advice and that GS&Co. is not a fiduciary to any person by reason of providing such information. For more information about our investment offerings, visit our Full Disclosures.
Investment products are: NOT FDIC INSURED ∙ NOT A DEPOSIT OR OTHER OBLIGATION OF, OR GUARANTEED BY, GOLDMAN SACHS BANK USA ∙ SUBJECT TO INVESTMENT RISKS, INCLUDING POSSIBLE LOSS OF THE PRINCIPAL AMOUNT INVESTED