paddle_quantum.finance.finance

Functions and data simulator class of quantum finance.

class paddle_quantum.finance.finance.DataSimulator(stocks, start=None, end=None)

Bases: object

Used to generate data and calculate relevant parameters for portfolio optimization and portfolio diversification problems.

Parameters:
  • stocks (list) – A list of names of investable stocks

  • start (datetime | None) – The start date of the trading day when the stock data is randomly generated. Defaults to None.

  • end (datetime | None) – The end date of the trading day when the stock data is randomly generated. Defaults to None.

set_data(data)

Decide which data source to use: randomly generated or locally entered.

Parameters:

data (list) – Stock data entered by the user.

randomly_generate()

Randomly generate stock data for experiments based on start date and end date.

Note

To generate random stock data, you need to specify the start date and end date in the format of the datetime package, e.g. start = datetime.datetime(2016, 1, 1).

get_asset_return_mean_vector()

Calculate expected return of each stock.

Returns:

Expected return of all investable stocks.

Return type:

list

get_asset_return_covariance_matrix()

Calculate the covariance matrix between the returns of each stock.

Returns:

The covariance matrix between the returns of each stock.

Return type:

list

get_similarity_matrix()

Calculate the similarity matrix among stocks.

The Dynamic Time Warping algorithm (DTW) is used to calculate the similarity between two stocks.

Returns:

The similarity matrix among stocks.

Return type:

list

paddle_quantum.finance.finance.portfolio_optimization_hamiltonian(penalty, mu, sigma, q, budget)

Construct the hamiltonian of the portfolio optimization problem.

Parameters:
  • penalty (int) – Penalty parameter.

  • mu (list) – Expected return of each stock.

  • sigma (list) – The covariance matrix between the returns of each stock.

  • q (float) – Risk appetite of the decision maker.

  • budget (int) – Budget, i.e. the number of stocks to be invested.

\[C(x) = q \sum_i \sum_j S_{ji}x_ix_j - \sum_{i}x_i \mu_i + A \left(B - \sum_i x_i\right)^2\]

Hint

Mapping Boolean variables \(x_i\) to Hamiltonian matrices under \(x_i \mapsto \frac{I-Z_i}{2}\).

Returns:

The hamiltonian of the portfolio optimization problem.

Return type:

Hamiltonian

paddle_quantum.finance.finance.portfolio_diversification_hamiltonian(penalty, rho, q)

Construct the hamiltonian of the portfolio diversification problem.

Parameters:
  • penalty (int) – Penalty parameter.

  • rho (list) – The similarity matrix among stocks.

  • q (int) – Number of categories for stock clustering.

\[\begin{split}\begin{aligned} C_x &= -\sum_{i=1}^{n}\sum_{j=1}^{n}\rho_{ij}x_{ij} + A\left(q- \sum_{j=1}^n y_j \right)^2 + \sum_{i=1}^n A\left(\sum_{j=1}^n 1- x_{ij} \right)^2 \\ &\quad + \sum_{j=1}^n A\left(x_{jj} - y_j\right)^2 + \sum_{i=1}^n \sum_{j=1}^n A\left(x_{ij}(1 - y_j)\right).\\ \end{aligned}\end{split}\]

Hint

Mapping Boolean variables \(x_{ij}\) to the Hamiltonian matrices under \(x_{ij} \mapsto \frac{I-Z_{ij}}{2}\)

Returns:

The hamiltonian of the portfolio diversification problem.

Return type:

Hamiltonian

paddle_quantum.finance.finance.arbitrage_opportunities_hamiltonian(g, penalty, n, k)

Construct the hamiltonian of the arbitrage opportunity optimization problem.

Parameters:
  • g (DiGraph) – Graphical representation of conversions between different markets.

  • penalty (int) – Penalty parameter.

  • n (int) – Number of currency types, i.e. number of vertices in the graph g.

  • k (int) – Number of vertices contained in the arbitrage loop.

\[C(x) = - P(x) + A\sum_{k=0}^{K-1} \left(1 - \sum_{i=0}^{n-1} x_{i,k}\right)^2 + A\sum_{k=0}^{K-1}\sum_{(i,j)\notin E}x_{i,k}x_{j,k+1}\]

Hint

Mapping Boolean variables \(x_{i,k}\) to the Hamiltonian matrices under \(x_{i,k} \mapsto \frac{I-Z_{i,k}}{2}\).

Returns:

The hamiltonian of the arbitrage opportunity optimization problem.

Return type:

Hamiltonian