private interval findIntervalForValue(double v, double intervalSize, List <interval> listOfIntervals) { foreach (interval i in listOfIntervals) { if (v >= i.lowerBound && v < i.upperBound) { i.marginalFrequency++; return(i); } } if (v < listOfIntervals[0].lowerBound) { while (true) { interval newLeft = new interval(); newLeft.upperBound = listOfIntervals[0].lowerBound; newLeft.lowerBound = newLeft.upperBound - intervalSize; newLeft.marginalFrequency = 0; listOfIntervals.Insert(0, newLeft); if (v >= newLeft.lowerBound && v < newLeft.upperBound) { newLeft.marginalFrequency++; return(newLeft); } } } else if (v >= listOfIntervals[listOfIntervals.Count() - 1].upperBound) { while (true) { interval newRight = new interval(); newRight.lowerBound = listOfIntervals[listOfIntervals.Count() - 1].upperBound; newRight.upperBound = newRight.lowerBound + intervalSize; newRight.marginalFrequency = 0; listOfIntervals.Add(newRight); if (v >= newRight.lowerBound && v < newRight.upperBound) { newRight.marginalFrequency++; return(newRight); } } } else { throw new Exception("not expected occurrence"); } }
private Dictionary <Tuple <interval, interval>, int> BivariateDistribution_DiscreteVariable(double IntervalSize_X, double IntervalSize_Y, double StartingEndPoint_X, double StartingEndPoint_Y, List <DataPoint> listOfObservations) { Dictionary <Tuple <interval, interval>, int> frequencies = new Dictionary <Tuple <interval, interval>, int>(); interval interval_X_0 = new interval(); interval_X_0.lowerBound = StartingEndPoint_X; interval_X_0.upperBound = StartingEndPoint_X + IntervalSize_X; interval_X_0.marginalFrequency = 0; listOfInterval_X = new List <interval>(); listOfInterval_X.Add(interval_X_0); interval interval_Y_0 = new interval(); interval_Y_0.lowerBound = StartingEndPoint_Y; interval_Y_0.upperBound = StartingEndPoint_Y + IntervalSize_Y; interval_Y_0.marginalFrequency = 0; listOfInterval_Y = new List <interval>(); listOfInterval_Y.Add(interval_Y_0); interval intervalFound_X; interval intervalFound_Y; foreach (DataPoint dp in listOfObservations) { intervalFound_X = null; intervalFound_Y = null; intervalFound_X = findIntervalForValue(dp.X, IntervalSize_X, listOfInterval_X); intervalFound_Y = findIntervalForValue(dp.Y, IntervalSize_Y, listOfInterval_Y); Tuple <interval, interval> foundTuple = new Tuple <interval, interval>(intervalFound_X, intervalFound_Y); if (frequencies.ContainsKey(foundTuple)) { frequencies[foundTuple] += 1; } else { frequencies.Add(foundTuple, 1); } } return(frequencies); }