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);
        }
    private static void dict_TryRemove(object sender, CountEventArgs e)
    {
        DictionaryCount <int, string> dict = sender as DictionaryCount <int, string>;

        Console.WriteLine(dict.Count);
        Console.WriteLine("Count less than 2!");
    }
        public override IModelLikelihood <int, int> GenerateModelLikelihood(IDataSet <int, int> training_set)
        {
            //TODO this asumes our label is finite and what not, maybe a bit silly
            double[] priors          = new double[training_set.DataContext.GetLabelDescriptor(0).ValueCount];
            int[]    instance_labels = training_set.GetLabelDataColumn(0);

            DictionaryCount <int> count_map = new DictionaryCount <int>(instance_labels);

            foreach (int key in count_map.Keys)
            {
                priors[key] = ((double)count_map.Get(key) / ((double)count_map.TotalCount));
            }


            IDictionary <int[], int[]> occurences = new DictionaryArrayKey <int, int[]>();

            for (int instance_index = 0; instance_index < training_set.InstanceCount; instance_index++)
            {
                int [] instance_features = training_set.GetInstanceFeatureData(instance_index);
                if (!occurences.ContainsKey(instance_features))
                {
                    occurences[instance_features] = new int[priors.Length + 1]; //index 0 is for total
                }
                occurences[instance_features][0]++;
                occurences[instance_features][instance_labels[instance_index] + 1]++;
            }
            return(new ModelLikelihoodNominalJointTable <int>(training_set.DataContext, priors, occurences));
        }
Ejemplo n.º 4
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);
            }
        }
Ejemplo n.º 5
0
 private BigIntegerPrimeFactorial(DictionaryCount <BigInteger> factor_counts)
 {
     d_factor_counts     = factor_counts;
     d_factorized_number = 1;
     foreach (BigInteger key in d_factor_counts.Keys)
     {
         int times = d_factor_counts[key];
         for (int times_index = 0; times_index < times; times_index++)
         {
             d_factorized_number = d_factorized_number * key;
         }
     }
 }
Ejemplo n.º 6
0
    static void Main(string[] args)
    {
        DictionaryCount <int, string> dict = new DictionaryCount <int, string>();

        dict.CountLessThan += dict_TryRemove;
        dict.CountToFireOn  = 1;
        dict.TryAdd(1, "hello");
        dict.TryAdd(2, "world");
        dict.TryAdd(3, "!");
        string outValue;

        dict.TryRemove(2, out outValue);
        dict.TryRemove(1, out outValue);
        Console.ReadKey(true);
    }
Ejemplo n.º 7
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.º 8
0
    //public bool NegTest1()
    //{
    //    bool retVal = true;

    //    TestLibrary.TestFramework.BeginScenario("NegTest1: ");

    //    try
    //    {
    //          //
    //          // Add your test logic here
    //          //
    //    }
    //    catch (Exception e)
    //    {
    //        TestLibrary.TestFramework.LogError("101", "Unexpected exception: " + e);
    //        TestLibrary.TestFramework.LogInformation(e.StackTrace);
    //        retVal = false;
    //    }

    //    return retVal;
    //}
    #endregion
    #endregion

    public static int Main()
    {
        DictionaryCount test = new DictionaryCount();

        TestLibrary.TestFramework.BeginTestCase("DictionaryCount");

        if (test.RunTests())
        {
            TestLibrary.TestFramework.EndTestCase();
            TestLibrary.TestFramework.LogInformation("PASS");
            return(100);
        }
        else
        {
            TestLibrary.TestFramework.EndTestCase();
            TestLibrary.TestFramework.LogInformation("FAIL");
            return(0);
        }
    }
Ejemplo n.º 9
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.º 10
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.º 11
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.º 12
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);
        }
        // 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.º 14
0
    //public bool NegTest1()
    //{
    //    bool retVal = true;

    //    TestLibrary.TestFramework.BeginScenario("NegTest1: ");

    //    try
    //    {
    //          //
    //          // Add your test logic here
    //          //
    //    }
    //    catch (Exception e)
    //    {
    //        TestLibrary.TestFramework.LogError("101", "Unexpected exception: " + e);
    //        TestLibrary.TestFramework.LogInformation(e.StackTrace);
    //        retVal = false;
    //    }

    //    return retVal;
    //}
    #endregion
    #endregion

    public static int Main()
    {
        DictionaryCount test = new DictionaryCount();

        TestLibrary.TestFramework.BeginTestCase("DictionaryCount");

        if (test.RunTests())
        {
            TestLibrary.TestFramework.EndTestCase();
            TestLibrary.TestFramework.LogInformation("PASS");
            return 100;
        }
        else
        {
            TestLibrary.TestFramework.EndTestCase();
            TestLibrary.TestFramework.LogInformation("FAIL");
            return 0;
        }
    }