๐ŸชŸSliding Window Problems

Practice:

  • Calculate average temperature in windows of 5 readings from a weather dataset.

  • Count frequency of words in batches of 10 tweets to find trending terms.

  • Check for abnormal jumps in a stock price by comparing windows of daily returns.

The key steps are:

  1. Define window size

  2. Perform operation on each window

  3. Slide window and repeat

  4. Analyze results

Calculate average temperature in windows of 5 readings from a weather dataset.

{25.5, 26.2, 24.8, 23.9, 25.1, 26.8, 27.3, 24.6, 23.5, 24.9, 25.7, 26.5};
public class AverageTemperature {
    public static void main(String[] args) {
        // Assuming temperatureReadings is an array containing the temperature readings
        double[] temperatureReadings = {25.5, 26.2, 24.8, 23.9, 25.1, 26.8, 27.3, 24.6, 23.5, 24.9, 25.7, 26.5};
    
    // Initialize variables
    int windowSize = 5;
    int totalWindows = temperatureReadings.length - windowSize + 1;
    double[] averageTemperatures = new double[totalWindows];
    
    // Calculate average temperatures for each window
    for (int i = 0; i < totalWindows; i++) {
        double sum = 0;
        
        // Calculate sum of temperatures in the window
        for (int j = i; j < i + windowSize; j++) {
            sum += temperatureReadings[j];
        }
        
        // Calculate average temperature for the window
        averageTemperatures[i] = sum / windowSize;
    }
    
    // Print the average temperatures for each window
    for (double averageTemperature : averageTemperatures) {
        System.out.println("Average Temperature: " + averageTemperature);
    }
}

Counting Frequency of Words in Batches of 10 Tweets

import java.util.HashMap;
import java.util.Map;

public class TrendingTerms {
    public static void main(String[] args) {
        // Assuming tweets is a list containing the tweets
        List<String> tweets = Arrays.asList("tweet1", "tweet2", "tweet3", ...);
        
        // Initialize variables
        int batchSize = 10;
        int totalBatches = tweets.size() / batchSize;
        Map<String, Integer> wordFrequency = new HashMap<>();
        
        // Count the frequency of words in each batch
        for (int i = 0; i < totalBatches; i++) {
            int startIndex = i * batchSize;
            int endIndex = startIndex + batchSize;
            
            // Process each tweet in the batch
            for (int j = startIndex; j < endIndex; j++) {
                String tweet = tweets.get(j);
                String[] words = tweet.split(" ");
                
                // Count the frequency of each word
                for (String word : words) {
                    wordFrequency.put(word, wordFrequency.getOrDefault(word, 0) + 1);
                }
            }
        }
        
        // Print the trending terms and their frequencies
        for (Map.Entry<String, Integer> entry : wordFrequency.entrySet()) {
            System.out.println("Word: " + entry.getKey() + ", Frequency: " + entry.getValue());
        }
    }
}

Checking for Abnormal Jumps in Stock Prices

public class AbnormalJumps {
    public static void main(String[] args) {
        // Assuming dailyReturns is an array containing the daily returns of a stock
        double[] dailyReturns = {0.02, -0.03, 0.05, 0.01, -0.02, 0.04, 0.07, -0.01};
        
        // Initialize variables
        int windowSize = 3;
        int totalWindows = dailyReturns.length - windowSize + 1;
        
        // Check for abnormal jumps in each window
        for (int i = 0; i < totalWindows; i++) {
            boolean abnormalJump = false;
            
            // Calculate the maximum and minimum returns in the window
            double maxReturn = Double.MIN_VALUE;
            double minReturn = Double.MAX_VALUE;
            
            for (int j = i; j < i + windowSize; j++) {
                double currentReturn = dailyReturns[j];
                
                if (currentReturn > maxReturn) {
                    maxReturn = currentReturn;
                }
                
                if (currentReturn < minReturn) {
                    minReturn = currentReturn;
                }
            }
            
            // Check if the difference between max and min returns exceeds a threshold
            double threshold = 0.03; // Assuming a 3% threshold for abnormal jumps
            if (maxReturn - minReturn > threshold) {
                abnormalJump = true;
            }
            
            // Print the result for the window
            System.out.println("Window: " + (i + 1) + ", Abnormal Jump: " + abnormalJump);
        }
    }
}

Last updated