Ejemplo n.º 1
0
        public static double[] Ranks(IList <double> list)
        {
            // compute ranks: equal ranks are averaged, ranks start at 1 (for most thest this is requiered)
            DictionaryCount <double> values_counts = new DictionaryCount <double>();

            foreach (double item in list)
            {
                values_counts.Increment(item);
            }

            Dictionary <double, double> ranks_lookup = new Dictionary <double, double>();
            List <double> keys = new List <double>(values_counts.Keys);

            keys.Sort();
            double rank = 1;

            foreach (double key in keys)
            {
                int count = values_counts[key];
                ranks_lookup[key] = (rank + rank + count - 1) / 2.0;
                rank += count;
            }

            double[] ranks = new double[list.Count];
            for (int index = 0; index < list.Count; index++)
            {
                ranks[index] = ranks_lookup[list[index]];
            }
            return(ranks);
        }
Ejemplo n.º 2
0
        public BigIntegerPrimeFactorial(BigInteger to_factor)
        {
            d_factorized_number = to_factor;
            d_factor_counts     = new DictionaryCount <BigInteger>();
            List <BigInteger> factors = ToolsMathBigInteger.factorize(to_factor);

            foreach (BigInteger factor in factors)
            {
                d_factor_counts.Increment(factor);
            }
        }
        // based on first bigger than second means positive
        public static double ComputeRankSumStatistic(IList <double> sample_0, IList <double> sample_1)
        {
            // compute ranks
            DictionaryCount <double> counts = new DictionaryCount <double>();

            foreach (double item in sample_0)
            {
                counts.Increment(item);
            }

            foreach (double item in sample_1)
            {
                counts.Increment(item);
            }
            Dictionary <double, double> ranks = new Dictionary <double, double>();
            List <double> keys = new List <double>(counts.Keys);

            keys.Sort();
            double rank = 1;

            foreach (double key in keys)
            {
                int count = counts[key];
                ranks[key] = (rank + rank + count - 1) / 2.0;
                rank      += count;
            }

            List <double> rank_list = new List <double>();

            foreach (double item in sample_0)
            {
                rank_list.Add(ranks[item]);
            }

            return(ToolsMathCollection.Sum(rank_list));
        }
Ejemplo n.º 4
0
        public static BigIntegerPrimeFactorial get_common_basis(List <BigIntegerPrimeFactorial> prime_factorial_list)
        {
            HashSet <BigInteger> prime_set  = new HashSet <BigInteger>();
            HashSet <BigInteger> factor_set = new HashSet <BigInteger>();

            foreach (BigIntegerPrimeFactorial prime_factorial in prime_factorial_list)
            {
                if (prime_factorial.is_prime())
                {
                    prime_set.Add(prime_factorial.get_factorized_number());
                }
                else
                {
                    foreach (BigInteger key in prime_factorial.d_factor_counts.Keys)
                    {
                        factor_set.Add(key);
                    }
                }
            }

            DictionaryCount <BigInteger> factor_counts = new DictionaryCount <BigInteger>();

            // build count map
            foreach (BigInteger key in factor_set)
            {
                int max_count = 1;
                foreach (BigIntegerPrimeFactorial prime_factorial in prime_factorial_list)
                {
                    max_count = Math.Max(max_count, prime_factorial.d_factor_counts[key]);
                }
                factor_counts.Add(key, max_count);
            }

            // build add primes
            foreach (BigInteger prime in prime_set)
            {
                if (!factor_set.Contains(prime))
                {
                    factor_counts.Increment(prime);
                }
            }

            return(new BigIntegerPrimeFactorial(factor_counts));
        }
Ejemplo n.º 5
0
        /***
         * Lowest value has lowest rank
         * Shared ranks are averaged
         *
         * @param array
         * @return
         */
        public static float[][] ConvertToAccendingRanks(
            float[][] array)
        {
            DictionaryCount <float> value_counts = new DictionaryCount <float>();

            foreach (float[] inner in array)
            {
                foreach (float value in inner)
                {
                    value_counts.Increment(value);
                }
            }

            Dictionary <float, float> rank_map = new Dictionary <float, float>();
            List <float> value_list            = new List <float>(value_counts.Keys);

            value_list.Sort();

            float rank_level = 1.0f;

            for (int value_index = 0; value_index < value_list.Count; value_index++)
            {
                float value       = value_list[value_index];
                float value_count = value_counts[value];
                float rank        = (rank_level + rank_level + value_count - 1) / 2.0f;
                rank_map[value] = rank;
                rank_level      = rank_level + value_count;
            }

            float[][] ranks = new float[array.Length][];
            for (int outer_index = 0; outer_index < array.Length; outer_index++)
            {
                ranks[outer_index] = new float[array[outer_index].Length];
                for (int inner_index = 0; inner_index < array[outer_index].Length; inner_index++)
                {
                    ranks[outer_index][inner_index] = rank_map[array[outer_index][inner_index]];
                }
            }

            return(ranks);
        }
Ejemplo n.º 6
0
        /***
         * Lowest value has lowest rank
         * Shared ranks are averaged
         *
         * @param array
         * @return
         */
        public static double[][] ConvertToAccendingRanks(
            IList <IList <double> > list_list)
        {
            DictionaryCount <double> value_counts = new DictionaryCount <double>();

            foreach (IList <double> list in list_list)
            {
                foreach (double value in list)
                {
                    value_counts.Increment(value);
                }
            }

            Dictionary <double, double> rank_map = new Dictionary <double, double>();
            List <double> value_list             = new List <double>(value_counts.Keys);

            value_list.Sort();

            double rank_level = 1.0f;

            for (int value_index = 0; value_index < value_list.Count; value_index++)
            {
                double value       = value_list[value_index];
                double value_count = value_counts[value];
                double rank        = (rank_level + rank_level + value_count - 1) / 2.0f;
                rank_map[value] = rank;
                rank_level      = rank_level + value_count;
            }

            double[][] ranks = new double[list_list.Count][];
            for (int outer_index = 0; outer_index < list_list.Count; outer_index++)
            {
                ranks[outer_index] = new double[list_list[outer_index].Count];
                for (int inner_index = 0; inner_index < list_list[outer_index].Count; inner_index++)
                {
                    ranks[outer_index][inner_index] = rank_map[list_list[outer_index][inner_index]];
                }
            }
            return(ranks);
        }
Ejemplo n.º 7
0
        public void DoExperiment()
        {
            IReadOnlyList <PriceCandle> candles = ToolsPrice.GetPriceCandles(ToolsPrice.DefaultSymbolUSDEUR, TimeScale.Second1);
            DictionaryCount <int>       dic     = new DictionaryCount <int>();

            foreach (PriceCandle candle in candles)
            {
                dic.Increment((int)Math.Round((candle.OpenAsk - candle.OpenBid) / TradingConstants.POINT));
            }
            List <int> keys = new List <int>(dic.Keys);

            keys.Sort();

            string[,] spread_table = new string[keys.Count, 2];
            for (int i = 0; i < keys.Count; i++)
            {
                Console.WriteLine(keys[i] + " " + dic[keys[i]]);
                spread_table[i, 0] = keys[i].ToString(CultureInfo.InvariantCulture);
                spread_table[i, 1] = dic[keys[i]].ToString(CultureInfo.InvariantCulture);
            }
            ToolsIOCSV.WriteCSVFile(@"D:\GoogleDrive\TestData\Trading\Spreads.csv", spread_table);
        }
Ejemplo n.º 8
0
        // based on first bigger than second means positive
        public static double ComputeSingedRankPairedStatistic(IList <double> sample_0, IList <double> sample_1)
        {
            double[] absolute_difference = ToolsMathCollection.AbsoluteDifference(sample_0, sample_1);

            // compute ranks
            DictionaryCount <double> counts = new DictionaryCount <double>();

            foreach (double item in absolute_difference)
            {
                counts.Increment(item);
            }

            Dictionary <double, double> ranks = new Dictionary <double, double>();
            List <double> keys = new List <double>(counts.Keys);

            keys.Sort();
            double rank = 1;

            foreach (double key in keys)
            {
                int count = counts[key];
                ranks[key] = (rank + rank + count - 1) / 2.0;
                rank      += count;
            }

            double statistic = 0;

            for (int index = 0; index < sample_0.Count; index++)
            {
                if (sample_1[index] < sample_0[index])
                {
                    statistic += ranks[absolute_difference[index]];
                }
            }
            return(statistic);
        }