← Back to Problems
Minimum Variance Direction
Easy standard Linear Algebra Portfolio Optimization
Time limit: 2 s per test  ·  Memory: 256 MB

You are given the daily returns of n assets over T days as a T x n matrix R, where R[t][i] is the return of asset i on day t.

A weight vector w with w_1^2 + ... + w_n^2 = 1 defines the portfolio return series p_t = R[t][1] w_1 + ... + R[t][n] w_n. Find the w for which the sample variance of p_1, ..., p_T is smallest.

To fix the sign, output the w whose component of largest absolute value is positive.

Input

The first line contains T and n. Each of the next T lines contains n real numbers, the returns of that day, each with at most 6 digits after the decimal point.

Output

n real numbers w_1, ..., w_n, each with absolute or relative error at most 1e-9.

Constraints

2 <= n <= 20

n + 1 <= T <= 400

It is guaranteed that the minimizing direction is unique.

Scoring

This is a standard problem: a submission scores full points if it produces the correct output for every test case, and zero otherwise.

Numeric answers are accepted if they are within an absolute tolerance of 1e-09 or a relative tolerance of 1e-09 of the expected value.

Examples

input
6 2
-0.011124 0.000445
0.018100 -0.010024
-0.023667 -0.001467
0.031836 -0.002040
-0.046964 -0.012130
-0.014521 -0.012268
output
-0.06141763299817512 0.9981121551994552
input
30 4
0.017833 -0.006588 -0.020016 0.018444
-0.003903 0.014853 0.034964 0.013398
0.021261 -0.001811 0.004596 -0.008254
-0.001396 -0.003081 0.005057 -0.011317
0.022948 -0.023261 0.005904 -0.005468
0.005359 0.028984 -0.000215 -0.002594
0.036346 0.019880 -0.032758 0.000278
-0.028406 -0.015031 0.026713 -0.009070
0.014319 -0.030345 0.007413 0.020697
0.001872 0.003964 -0.016865 -0.003254
-0.023080 0.030619 0.001388 0.007729
0.010194 0.015921 -0.002412 -0.009755
0.006679 -0.009638 0.023173 -0.009211
-0.000398 -0.003802 0.015664 0.015092
0.028165 0.027106 0.009716 -0.005942
-0.014228 0.033975 -0.009043 -0.003681
-0.013508 -0.033361 -0.014826 0.017948
0.004739 -0.038965 -0.044204 0.000487
-0.012034 0.035392 -0.004542 0.020504
0.043090 -0.002481 0.022983 0.001128
-0.019112 -0.015831 0.013556 0.019389
-0.006429 -0.003918 -0.009243 -0.004996
0.010174 -0.017671 0.009504 0.010397
-0.013375 -0.020296 0.009360 0.002876
-0.018550 -0.026943 -0.063568 -0.018978
-0.012189 -0.004616 0.008808 0.013261
0.001670 -0.042639 -0.008760 0.013746
-0.003211 -0.000477 -0.008774 0.015583
0.014868 0.005865 0.051483 0.007773
-0.017663 -0.016382 -0.009257 -0.002155
output
0.09942063951195336 0.09232502966663914 -0.15930603028539678 0.9778615515760893
Python 3.13 i Execution environment Isolated microVM · 1 vCPU · no internet access Time and memory limits are set per problem Available packages numpy 2.5.0scipy 1.18.0pandas 3.0.0scikit-learn 1.9.0statsmodels 0.15.0cvxpy 1.9.2