Results 1 to 2 of 2
  1. #1
    apacheomega is offline Member
    Join Date
    May 2015
    Posts
    5
    Rep Power
    0

    Default Help with implementing chi Square algorithm

    I haven't touched Java in ages and I'm trying to get the code I have to work But I keep getting and error. What am I doing wrong here?

    Java Code:
    package chisquare;
    
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     *
     * @author geekmonster
     */
    public class ChiSquare {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            
            public static boolean isRandom(int[] randomNums, int r)
    	{
    		
    		if (randomNums.length <= 10 * r)
    			return false;
    
    		
    		Map<Integer,Integer> ht = getFrequencies(randomNums);
    
    		
    		double n_r = (double)randomNums.length / r;
    		double chiSquare = 0;
    
                    chiSquare = ht.values().stream().map((v) -> v - n_r).map((f) -> f * f).reduce(chiSquare, (accumulator, _item) -> accumulator + _item);
    		chiSquare /= n_r;
    
    		
    		return Math.abs(chiSquare - r) <= 2 * Math.sqrt(r);
    	}
    
    	
    	private static Map<Integer,Integer> getFrequencies(int[] nums)
    	{
    		Map<Integer,Integer> freqs = new HashMap<>();
    
    		for (int x : nums)
    		{
    			if (freqs.containsKey(x))
    				freqs.put(x, freqs.get(x) + 1);
    			else
    				freqs.put(x, 1);
    		}
    	
    		return freqs;
    	}
        }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Help with implementing chi Square algorithm

    You can't define methods inside other methods (e.g. isRandom( ... ) in the main( ... ) method). The compiler protests against it ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Paint a Square`
    By nester999 in forum New To Java
    Replies: 1
    Last Post: 12-01-2011, 10:55 PM
  2. Replies: 0
    Last Post: 10-03-2011, 01:27 PM
  3. Replies: 0
    Last Post: 03-20-2011, 02:17 AM
  4. Magic Square!!!... :D
    By joms999 in forum New To Java
    Replies: 4
    Last Post: 02-25-2010, 08:55 AM
  5. How can I square(^2) the pic in the grid
    By racewithferrari in forum New To Java
    Replies: 2
    Last Post: 11-03-2009, 06:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •