コード例 #1
0
        static double[] doLSQ(List <Tuple <float, float, float> > data, Action <double[], double[], object> fitalgo, double[] x)
        {
            double epsg   = 0.00000001;
            double epsf   = 0;
            double epsx   = 0;
            int    maxits = 0;

            alglib.minlmstate  state;
            alglib.minlmreport rep;

            alglib.minlmcreatev(data.Count, x, 100, out state);
            alglib.minlmsetcond(state, epsg, epsf, epsx, maxits);

            var t1 = new alglib.ndimensional_fvec(fitalgo);

            alglib.minlmoptimize(state, t1, null, data);

            alglib.minlmresults(state, out x, out rep);

            log.InfoFormat("passes {0}", rep.iterationscount);
            log.InfoFormat("term type {0}", rep.terminationtype);
            log.InfoFormat("ans {0}", alglib.ap.format(x, 4));

            return(x);
        }
コード例 #2
0
ファイル: MagCalib.cs プロジェクト: kuzzh/UAV-Station
        static double[] doLSQ(List <Tuple <float, float, float> > data, Action <double[], double[], object> fitalgo,
                              double[] x)
        {
            double epsg   = 0.00000001;
            double epsf   = 0;
            double epsx   = 0;
            int    maxits = 0;

            alglib.minlmstate  state;
            alglib.minlmreport rep;

            alglib.minlmcreatev(data.Count, x, 100, out state);
            alglib.minlmsetcond(state, epsg, epsf, epsx, maxits);

            var t1 = new alglib.ndimensional_fvec(fitalgo);

            alglib.minlmoptimize(state, t1, null, data);

            alglib.minlmresults(state, out x, out rep);

            log.InfoFormat("passes {0}", rep.iterationscount);
            log.InfoFormat("term type {0}", rep.terminationtype);
            log.InfoFormat("njac {0}", rep.njac);
            log.InfoFormat("ncholesky {0}", rep.ncholesky);
            log.InfoFormat("nfunc{0}", rep.nfunc);
            log.InfoFormat("ngrad {0}", rep.ngrad);
            log.InfoFormat("ans {0}", alglib.ap.format(x, 4));

            if (data == datacompass1)
            {
                error = 0;

                foreach (var item in state.fi)
                {
                    error += item;
                }

                error = Math.Round(Math.Sqrt(Math.Abs(error)), 2);
            }


            if (data == datacompass2)
            {
                error2 = 0;

                foreach (var item in state.fi)
                {
                    error2 += item;
                }

                error2 = Math.Round(Math.Sqrt(Math.Abs(error2)), 2);
            }

            return(x);
        }
コード例 #3
0
        static double[] doLSQ(List<Tuple<float, float, float>> data, Action<double[], double[], object> fitalgo,
            double[] x)
        {
            double epsg = 0.00000001;
            double epsf = 0;
            double epsx = 0;
            int maxits = 0;

            alglib.minlmstate state;
            alglib.minlmreport rep;

            alglib.minlmcreatev(data.Count, x, 100, out state);
            alglib.minlmsetcond(state, epsg, epsf, epsx, maxits);

            var t1 = new alglib.ndimensional_fvec(fitalgo);

            alglib.minlmoptimize(state, t1, null, data);

            alglib.minlmresults(state, out x, out rep);

            log.InfoFormat("passes {0}", rep.iterationscount);
            log.InfoFormat("term type {0}", rep.terminationtype);
            log.InfoFormat("njac {0}", rep.njac);
            log.InfoFormat("ncholesky {0}", rep.ncholesky);
            log.InfoFormat("nfunc{0}", rep.nfunc);
            log.InfoFormat("ngrad {0}", rep.ngrad);
            log.InfoFormat("ans {0}", alglib.ap.format(x, 4));

            if (data == datacompass1)
            {
                error = 0;

                foreach (var item in state.fi)
                {
                    error += item;
                }

                error = Math.Round(Math.Sqrt(Math.Abs(error)), 2);
            }


            if (data == datacompass2)
            {
                error2 = 0;

                foreach (var item in state.fi)
                {
                    error2 += item;
                }

                error2 = Math.Round(Math.Sqrt(Math.Abs(error2)), 2);
            }

            return x;
        }
コード例 #4
0
ファイル: MagCalib.cs プロジェクト: jank3/MissionPlanner
        static double[] doLSQ(List<Tuple<float, float, float>> data, Action<double[], double[], object> fitalgo, double[] x)
        {
            double epsg = 0.00000001;
            double epsf = 0;
            double epsx = 0;
            int maxits = 0;

            alglib.minlmstate state;
            alglib.minlmreport rep;

            alglib.minlmcreatev(data.Count, x, 100, out state);
            alglib.minlmsetcond(state, epsg, epsf, epsx, maxits);

            var t1 = new alglib.ndimensional_fvec(fitalgo);

            alglib.minlmoptimize(state, t1, null, data);

            alglib.minlmresults(state, out x, out rep);

            log.InfoFormat("passes {0}", rep.iterationscount);
            log.InfoFormat("term type {0}", rep.terminationtype);
            log.InfoFormat("ans {0}", alglib.ap.format(x, 4));

            return x;
        }
コード例 #5
0
        /*
         * static Func<StateForUtilityMaximization, alglib.ndimensional_fvec> maximize_utility_func = (state) =>
         * {
         *  alglib.ndimensional_fvec d = (xs, fi, obj) =>
         *  {
         *      var valuation = ValuePortfolio(state, xs);
         *      fi[0] = -valuation.Utility;
         *      fi[1] = Enumerable.Sum(xs) - 1;
         *
         *  };
         *  return d;
         * };
         *
         * static Func<StateForPortfolioTargeting, alglib.ndimensional_fvec> target_portfolio_func = (state) =>
         * {
         *  alglib.ndimensional_fvec f = (xs, fi, obj) =>
         *  {
         *      var valuation = ValuePortfolio(state, xs);
         *      fi[0] = valuation.Error;
         *      fi[1] = Enumerable.Sum(xs) - 1;
         *  };
         *  return f;
         * };
         */
        public static ValuationResult Optimize(OptimizationParameters state, double[] initialValues2)
        {
            int n       = state.CurrentStocksAllocation.Length;
            var weights = VectorOp.createWith(n, 1.0 / n);

            double[] initialValues = VectorOp.DotDivision(VectorOp.multiplication(weights, state.StocksValue()), state.MarketData.AvgPrices);
            alglib.ndimensional_fvec functionToOptimize = null;
            int equalities   = 0;
            int inequalities = 0;
            var stateType    = state.GetType();

            if (stateType == typeof(ReturnTargetingParameters))
            {
                functionToOptimize = target_return_func((ReturnTargetingParameters)state);
                equalities         = 5;
                inequalities       = initialValues.Length;
            }
            else if (false)//stateType == typeof(StateForUtilityMaximization))
            {
                //functionToOptimize = target_return_func((StateForReturnTargeting)state);
                //equalities = 1;
                //inequalities = 0;
            }
            else if (false) //stateType == typeof(StateForPortfolioTargeting))
            {
                //functionToOptimize = target_portfolio_func((StateForPortfolioTargeting)state);
                //equalities = 1;
                //inequalities = 0;
            }

            double[] s = new double[n];
            for (int i = 0; i < n; i++)
            {
                s[i] = 1;// state.AvgPrices.Average() / state.AvgPrices[i];
            }
            double epsx     = 0.00001;
            double diffstep = 0.1;
            double radius   = 1;
            double rho      = 100000;
            int    maxits   = 0;

            alglib.minnsstate  optimizationState;
            alglib.minnsreport rep;
            double[]           newStocksAllocation;

            alglib.minnscreatef(n, initialValues, diffstep, out optimizationState);
            alglib.minnssetalgoags(optimizationState, radius, rho);
            alglib.minnssetcond(optimizationState, epsx, maxits);
            alglib.minnssetscale(optimizationState, s);
            alglib.minnssetnlc(optimizationState, equalities, inequalities);
            alglib.minnsoptimize(optimizationState, functionToOptimize, null, null);
            alglib.minnsresults(optimizationState, out newStocksAllocation, out rep);
            var valuation = RebalancingValuation.ValuePortfolio(state.MarketData, state.CurrentStocksAllocation, newStocksAllocation);

            Console.WriteLine("{0}", alglib.ap.format(newStocksAllocation, 3));
            Console.WriteLine("Expected return: {0}", valuation.ExpectedReturn);
            Console.WriteLine("StdDev: {0}", valuation.StdDev);
            Console.WriteLine("buysell: {0}", valuation.RebalancingCost.SharesBuySell);
            //Console.WriteLine("Error: {0}", state.targ);
            return(valuation);
        }