# Thread: Help with implementing chi Square algorithm

1. Member
Join Date
May 2015
Posts
5
Rep Power
0

## 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. ## 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

#### Posting Permissions

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