Domain: Financial & Accounting

Using the attached dataset of daily asset prices (columns correspond to assets), construct a Vector Autoregression (VAR) model.

1) Transform the data:
   - Compute log returns as log(P_t) - log(P_{t-1})
   - Take the first differences of these log returns.

2) Select the lag length p from {1,…,10} using AIC.
   Fit the VAR(p) with no deterministic trend terms.

Using only the in-sample fitted model:

1) Compute the orthogonalized Forecast Error Variance Decomposition (using Cholesky identification and the dataset column order) at horizon 20.
   For each variable, report the variance proportions explained by shocks from each variable.
   Round all values to 6 decimals and ensure each set of proportions sums to 1.

2) Identify the variable with the largest own-shock contribution at horizon 20.
   For that variable:
      - Compute orthogonalized impulse responses for horizons 0 through 30.
      - For each response series, extract the maximum absolute response.
      - Normalize these maxima so they sum to 100.
      - Return the results sorted from highest to lowest percentage, rounded to 3 decimals.

3) From the residual covariance matrix, compute:
      - the determinant
      - the trace
      - the condition number defined as the ratio of the largest to the smallest eigenvalue
   Round all values to 6 decimals.

Output format:
Return exactly one Python dictionary (no extra text) with keys:
"FEVD", "Dominant_IRF_Normalized", "Covariance_Diagnostics".


Grading rubric:
criterion: Is FEVD for GammaAsset due to GammaAsset equal to 0.918162?
weight: 15
criterion: Is the trace of the residual covariance matrix equal to 0.001454?
weight: 20
criterion: Is the normalized IRF percentage for AlphaAsset equal to 61.389?
weight: 15
criterion: Is the normalized IRF percentage for BetaAsset equal to 23.951?
weight: 15
criterion: Is the normalized IRF percentage for GammaAsset equal to 14.66?
weight: 15
criterion: Is the condition number of the residual covariance matrix equal to 2.435886?
weight: 5

Golden answer:
```python
import numpy as np
import pandas as pd
from numpy.linalg import det, eigvals
from statsmodels.tsa.api import VAR

# Load dataset
df = pd.read_csv("Quantum_Portfolio_Daily_Prices.csv", parse_dates=["Date"])
df = df.set_index("Date")
df = df.sort_index()

# 1) Transform data
log_prices = np.log(df)
log_returns = log_prices.diff().dropna()
diff_log_returns = log_returns.diff().dropna()

# 2) Select lag by AIC (max 10), fit VAR with no trend
model = VAR(diff_log_returns)
order_sel = model.select_order(10)
p = int(order_sel.selected_orders["aic"])
p = max(1, min(10, p))

res = model.fit(p, trend="n")


# Helpers for rounding
def f6(x):
    return float(np.round(float(x), 6))


def f3(x):
    return float(np.round(float(x), 3))


cols = list(diff_log_returns.columns)

# 1) FEVD at horizon 20
fevd = res.fevd(20)

fevd_out = {}
for i, var_i in enumerate(cols):
    vals = fevd.decomp[i, 19, :]  # horizon 20
    fevd_out[var_i] = {var_j: f6(vals[j]) for j, var_j in enumerate(cols)}

# 2) Dominant own-shock variable + IRF normalization
own_contrib = {var: fevd_out[var][var] for var in cols}
dominant_var = max(own_contrib, key=own_contrib.get)
dom_idx = cols.index(dominant_var)

irf = res.irf(30)
orth_irfs = irf.orth_irfs

max_abs = {}
for i, var in enumerate(cols):
    series = orth_irfs[:, i, dom_idx]
    max_abs[var] = float(np.max(np.abs(series)))

total = sum(max_abs.values())

irf_norm = {var: f3((val / total) * 100.0) for var, val in max_abs.items()}

# Sort from highest to lowest percentage
irf_norm = dict(
    sorted(
        irf_norm.items(),
        key=lambda kv: kv[1],
        reverse=True,
    )
)

# 3) Residual covariance diagnostics
Sigma = np.asarray(res.sigma_u)

eig = np.real(eigvals(Sigma))
cond_num = float(np.max(eig) / np.min(eig))

cov_diag = {
    "determinant": f6(det(Sigma)),
    "trace": f6(np.trace(Sigma)),
    "condition_number": f6(cond_num),
}

# Final output (only 3 keys)
output = {
    "FEVD": fevd_out,
    "Dominant_IRF_Normalized": irf_norm,
    "Covariance_Diagnostics": cov_diag,
}

print(output)

```
**Explanation:**
First take log of prices. Compute log returns as log(P_t) - log(P_{t-1}). Then take the first difference of those log returns. Fit a VAR model on this transformed data, choosing lag p from 1 to 10 using AIC and with no trend term. Using the fitted model, compute the orthogonalized FEVD at horizon 20. Identify the variable with the largest own-shock share. For that variable, compute orthogonalized impulse responses for horizons 0 to 30, take the maximum absolute response for each variable, and normalize these maxima so they sum to 100. Finally, compute the residual covariance determinant, trace, and condition number. Return the results in one Python dictionary.

**GTFA:**
{'FEVD': {'AlphaAsset': {'AlphaAsset': 0.975644, 'BetaAsset': 0.012352, 'GammaAsset': 0.012004}, 'BetaAsset': {'AlphaAsset': 0.130637, 'BetaAsset': 0.8616, 'GammaAsset': 0.007763}, 'GammaAsset': {'AlphaAsset': 0.05559, 'BetaAsset': 0.026247, 'GammaAsset': 0.918162}}, 'Dominant_IRF_Normalized': {'AlphaAsset': 61.389, 'BetaAsset': 23.951, 'GammaAsset': 14.66}, 'Covariance_Diagnostics': {'determinant': 0.0, 'trace': 0.001454, 'condition_number': 2.435886}}