Speculation Engine: Predicting Stock Price Trends
This challenge asks you to build a simplified "speculation engine" in JavaScript that analyzes historical stock price data and attempts to predict future trends. The engine will take a series of historical prices as input and output a prediction for the next price, along with a confidence level. This is a simplified model, focusing on core logic rather than complex statistical analysis.
Problem Description
You are tasked with creating a JavaScript function called predictNextPrice that takes an array of historical stock prices as input and returns an object containing the predicted next price and a confidence level (between 0 and 1, inclusive). The prediction should be based on a simple moving average calculation.
What needs to be achieved:
- Calculate the moving average of the last
nprices in the input array. - Use the moving average as the predicted next price.
- Calculate a confidence level based on the volatility of the historical prices. Higher volatility should result in a lower confidence level.
Key Requirements:
- The function must accept an array of numbers representing historical stock prices.
- The function must return an object with two properties:
predictedPrice(a number) andconfidence(a number between 0 and 1). - The moving average window size (
n) should be configurable (defaulting to 5). - Volatility should be calculated as the standard deviation of the historical prices.
- The confidence level should be inversely proportional to the volatility.
Expected Behavior:
The function should handle various input scenarios gracefully, including:
- Empty input array.
- Input array with fewer than
nelements. - Input array with valid numerical data.
Edge Cases to Consider:
- What should happen if the input array is empty? Return a default prediction (e.g., 0) and a low confidence.
- What should happen if the input array has fewer than
nelements? Use all available data for the moving average. - How should you handle non-numerical values in the input array? (Assume they are invalid and should be ignored or throw an error - your choice, but document it).
- How to ensure the confidence level stays within the 0-1 range.
Examples
Example 1:
Input: [10, 12, 15, 13, 16]
Output: { predictedPrice: 13.8, confidence: 0.7 }
Explanation: Moving average (n=5) = (10 + 12 + 15 + 13 + 16) / 5 = 13.8. Volatility is calculated, and confidence is adjusted accordingly.
Example 2:
Input: [20, 22, 25, 23, 26, 28, 27, 30]
Output: { predictedPrice: 27.14285714285714, confidence: 0.6 }
Explanation: Moving average (n=5) = (23 + 26 + 28 + 27 + 30) / 5 = 27.14285714285714. Volatility is calculated, and confidence is adjusted accordingly.
Example 3:
Input: []
Output: { predictedPrice: 0, confidence: 0.1 }
Explanation: Empty input array. Returns a default prediction and low confidence.
Constraints
- The input array will contain numbers (integers or floats). Non-numerical values should be handled gracefully (either ignored or an error thrown - document your choice).
- The moving average window size (
n) defaults to 5, but should be configurable. - The confidence level must be a number between 0 and 1, inclusive.
- The standard deviation calculation should be reasonably accurate.
- The function should execute in a reasonable time (e.g., less than 100ms for typical input sizes).
Notes
- You can use built-in JavaScript functions for mathematical operations (e.g.,
Math.sqrt,Math.pow). - Consider using a helper function to calculate the standard deviation.
- The confidence level calculation is intentionally simplified. More sophisticated models would use more complex statistical techniques.
- Document your assumptions and choices regarding error handling and edge cases.
- Focus on clarity and readability of your code. Good variable names and comments are appreciated.
- The confidence level should decrease as volatility increases. A simple approach is to use
confidence = 1 - (volatility / max_volatility), wheremax_volatilityis a reasonable upper bound for volatility (e.g., 10). Adjust this as needed to ensure the confidence stays within the 0-1 range.