3

使用Google OR-Tools分析过去20年中国金融资产最佳配置组合 - 车骑

 1 year ago
source link: https://www.cnblogs.com/royzshare/p/16978919.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

使用Google OR-Tools分析过去20年中国金融资产最佳配置组合

前两天,在朋友圈里看到一张截至2022年Q2的金融资产历年收益图如下,图中列举了国内从2005年到2022年近20年主要的金融资产历年收益率,随产生想法分析和验证下面几个问题:

  • 过去20年,基于怎样的资产配置才能让收益最大化?
  • 如果完全拒绝风险,是否可以理财,收益率会是多少?
  • 是否有风险小,收益高的资产配置组合?
  • 抛开择时和运气,资产配置的最佳持有时长是多少年?

分析方法
使用工具:Google OR-Tools,OR-Tools是谷歌用于组合优化的软件工具,可以从大量可能的解决方案中找到问题的最佳解决方案。比如本例中,假如2005年初我手上有100元钱,怎么把这100元钱分到不同的金融资产上有太多方案。但基本上只会有一种组合让最终收益最大化,也基本只会有一种组合让每年本金不出现亏损的前提下实现收益尽可能最大化。这些都会借助于这个工具进行分析和验证。
一些计算条件:

  • 由于图中的信托和房地产门槛比较高,不适于一般理财者,所以刨除掉。只保留银行理财、货币基金、债卷基金、股票基金、股票等5种金融资产。
  • 假定只是2005年初投入100元本金,中间不增加本金,也不减少账户资金。
  • 投资组合是固定的,比如银行理财占比x%,股票占比y%等,这个比例会保持不变。每年年初会基于上一年剩余本金进行比例的动态平衡调整,调整的目的是让投资组合依然保持这个比例。
  • 无风险是相对于每年年初的剩余资金。举例:2005年初投入100元,那么2005年末剩余资金必须大于等于100元。如果2005年获得了10元收益也就是说2006年年初的账户资金是110元(100元本金+10元收益),那么2006年末剩余资金必须大于等于年初资金110元。

分析结果
分析结果请参考:文章

代码

//定义单年最大允许亏损比例。(比如:0.2代表单年最大允许亏损比例为20%;1代表无限制;0代表不允许亏损)
            float allowableMaximumLossRatio = 1f; //无限制
            //float allowableMaximumLossRatio = 0.2f; //单年最大允许亏损比例为20%
            //float allowableMaximumLossRatio = 0f; //不允许亏损

            //待处理数据,此处全部转换为整数处理
            (String year, long[] values)[] data = new[]
            {
                ("2005", new long[]{ 10273, 10236, 10912, 10140, 8848}),
                ("2006", new long[]{ 10280, 10150, 11494, 22263, 21190}),
                ("2007", new long[]{ 10360, 10336, 11822, 22833, 26621}),
                ("2008", new long[]{ 11542, 10356, 10646, 4858, 3708}),
                ("2009", new long[]{ 10425, 10142, 10504, 17117, 20547}),
                ("2010", new long[]{ 10392, 10181, 10690, 9972, 9312}),
                ("2011", new long[]{ 10463, 10355, 9711, 7547, 7759}),
                ("2012", new long[]{ 10588, 10397, 10622, 10545, 10468}),
                ("2013", new long[]{ 10482,10395,10061,11013,10544}),
                ("2014", new long[]{ 10597,10460,11848,12939,15244}),
                ("2015", new long[]{ 10556,10362,10993,13467,13850}),
                ("2016", new long[]{ 10471,10261,9965,8969,8709}),
                ("2017", new long[]{ 10422,10384,10165,11063,10493}),
                ("2018", new long[]{ 10496,10375,10543,7683,7175}),
                ("2019", new long[]{ 10446,10266,10422,14109,13302}),
                ("2020", new long[]{ 10414,10213,10315,14454,12343}),
                ("2021", new long[]{ 10310, 10228, 10393, 10587, 10917}),
                ("2022", new long[]{ 10350, 10101, 10090, 8928, 9047}),
            };

            // 创建CP模型.
            CpModel model = new CpModel();

            //定义变量:各类资产配置比例
            IntVar a = model.NewIntVar(0, 100, "a"); //银行理财
            IntVar b = model.NewIntVar(0, 100, "b"); //货币基金
            IntVar c = model.NewIntVar(0, 100, "c"); //债卷基金
            IntVar d = model.NewIntVar(0, 100, "d"); //股票基金
            IntVar e = model.NewIntVar(0, 100, "e"); //股票

            //创建约束条件:配置比例总和为100%
            model.Add(a + b + c + d + e <= 100);
            model.Add(a + b + c + d + e >= 100);

            //创建约束条件:限定低风险配置比例
            //model.Add(a >= 40);
            //model.Add(d + e <= 40);

            //定义变量数组:单年年末资金
            IntVar[] yearResults = new IntVar[data.Length];
            //定义变量数组:单年收益率
            IntVar[] yearRatios = new IntVar[data.Length];

            for (int i = 0; i<data.Length; i++)
            {
                var yearItem = data[i];

                //定义变量:当前年度收益率
                IntVar ratio = model.NewIntVar(0, 100 * 10000 * 3, $"ratio{i}");
                model.Add(ratio == a * yearItem.values[0] + b * yearItem.values[1] + c * yearItem.values[2] + d * yearItem.values[3] + e * yearItem.values[4]);
                yearRatios[i] = ratio;

                //创建约束条件:单年最大允许亏损比例
                model.Add(ratio >= Convert.ToInt32(100 * (1 - allowableMaximumLossRatio)) * 10000);

                //定义变量:当前年末资金
                IntVar resultA = model.NewIntVar(0, 100 * 100 * 10000 * Convert.ToInt64(Math.Pow(3, i+1)), $"resultA{i}");
                model.AddMultiplicationEquality(resultA, i==0? model.NewConstant(100) : yearResults[i-1], ratio);

                //定义变量:由于原生数据的收益率和配置比例是使用转换后的整数计算的,所以这里使用当前年末资金除以100*10000
                IntVar result = model.NewIntVar(0, 100 * Convert.ToInt64(Math.Pow(3, i+1)), $"result{i}");
                model.AddDivisionEquality(result, resultA, model.NewConstant(100 * 10000));
                yearResults[i] = result;
            }

            //设定求解目标为最终资金最大
            model.Maximize(yearResults[data.Length -1]);

            //求解
            CpSolver solver = new CpSolver();
            CpSolverStatus status = solver.Solve(model);

            //输出求解结果
            if (status == CpSolverStatus.Optimal || status == CpSolverStatus.Feasible)
            {
                Console.WriteLine("银行理财配置比: " + solver.Value(a)+"%");
                Console.WriteLine("货币基金配置比: " + solver.Value(b)+"%");
                Console.WriteLine("债卷基金配置比: " + solver.Value(c)+"%");
                Console.WriteLine("股票基金配置比: " + solver.Value(d)+"%");
                Console.WriteLine("股票配置比: " + solver.Value(e)+"%");

                for (int i = 0; i<data.Length; i++)
                {
                    Console.WriteLine($"{data[i].year} 年末资金:{solver.Value(yearResults[i])} 收益率:{String.Format("{0:P}", solver.Value(yearRatios[i]) / 1000000.00 - 1)}");
                }

                Console.WriteLine($"最终资金: {solver.ObjectiveValue}");
                Console.WriteLine($"年化收益率: {String.Format("{0:P}", Math.Pow((solver.ObjectiveValue - 100)/100, 1.00/data.Length)-1)}");
            }
            else
            {
                Console.WriteLine("求解失败,未找到合适结果.");
            }

            Console.WriteLine($"求解耗时: {solver.WallTime()}s");

Github地址:代码


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK