Stock pattern similarity query system and method based on domain generalization
By optimizing the training of recurrent neural networks using domain generalization and feature selection algorithms RReliefF and Lasso combined with a greedy Gaussian segmentation algorithm, the data drift problem in stock data analysis was solved, the model accuracy was improved, and more reliable stock pattern similarity queries were achieved.
Patent Information
- Application Number
- CN202310116131.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-02-15
- Publication Date
- 2026-02-03
- Estimated Expiration
- 2043-02-15
AI Technical Summary
Existing deep learning methods suffer from data drift in stock data analysis, resulting in poor model performance in practical applications. Furthermore, existing feature selection methods are insufficient to effectively improve model accuracy.
A domain generalization method is adopted, combined with feature selection algorithms RReliefF and Lasso, and trained through a recurrent neural network. RReliefF is used for feature selection, Lasso is used for feature embedding selection, and a greedy Gaussian segmentation algorithm is combined for data segmentation to optimize the model training process.
It improves the data drift problem in stock data, enhances the accuracy of the model, and enables users to reliably select stock data with similar characteristics.
Smart Images

Figure CN116108379B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of deep learning technology, and in particular to a stock pattern similarity query system and method based on domain generalization. Background Technology
[0002] Quantitative trading refers to replacing subjective human judgment with advanced mathematical models and using computer technology to identify multiple high-probability events that generate excess returns from large amounts of historical data, thereby formulating corresponding strategies. As an important branch of quantitative trading, stock analysis plays a crucial role. The problem of stock pattern similarity lookup has consistently attracted attention. Although numerous solutions have emerged, the variability of stock data makes this problem a challenging task. Currently, methods for solving this problem mainly fall into three categories: statistical methods, traditional machine learning methods, and deep learning methods. Compared to the other two methods, deep learning methods have advantages such as strong adaptability, high accuracy, strong feature capture ability, and less human intervention. Therefore, deep learning methods have been adopted for research.
[0003] Deep learning, as a new research direction in the field of machine learning, brings machine learning closer to the original goal of achieving artificial intelligence. Its concept originates from artificial neural networks, mimicking the mechanisms of the human brain to interpret data. It combines low-level features to form high-level representations of attribute categories or features. Today, with the rise of deep learning, it is widely used in various fields, such as search technology, data mining, natural language processing, and image recognition. Its effects often surpass previous related technologies, and it can be used to solve many problems that traditional technologies could not solve. As the number of problems solved increases, deep learning continues to evolve. Today, there are many different types of deep learning technologies, with typical examples including recurrent neural networks, convolutional networks, autoencoders, and generative adversarial networks.
[0004] Although neural networks have demonstrated good performance, stock data often suffers from data drift, meaning that the statistical information of the training data is inconsistent with that of the actual data. This may lead to poor performance of the trained model in actual applications, and this phenomenon becomes more pronounced as the data time span increases. Therefore, this invention applies a domain generalization method to solve this problem.
[0005] Domain generalization is a very popular research direction in transfer learning in recent years. Transfer learning focuses on storing solutions to existing problems and applying them to other different but related problems. For example, knowledge (or models) used to identify cars can also be used to improve the ability to identify trucks. The solved problem is defined as the source domain, while the other different but related problems to be solved are defined as the target domain. Domain generalization differs from previous research directions in transfer learning in that the target domain data in previous research directions was visible, meaning the target domain data could be used to train deep learning models. In the definition of domain generalization, the target domain data is invisible, meaning it cannot be used to train the model. Domain generalization studies how to learn a model from numerous source domain data that can generalize to an invisible target domain.
[0006] Feature selection, also known as feature subset selection (FSS) or attribute selection, refers to choosing N features from M existing features to optimize a specific system metric. It's the process of selecting the most effective features from the original features to reduce the dimensionality of the dataset. It's an important means of improving the performance of learning algorithms and a key data preprocessing step in pattern recognition. For a learning algorithm, good training samples are crucial for training the model. Feature selection algorithms are mainly divided into three categories: filter methods, embedded methods, and wrapper methods. Filter methods: These methods start from the features of the dataset itself, examining the correlation between variables, etc., without considering the specific algorithm used after feature selection. Wrapper methods: These methods first select a subset of features, then use these features to run your specific algorithm (such as a decision tree), observe the performance, and repeat the above steps until the "best" feature subset is selected. Embedded methods: This type of method embeds feature selection into the algorithm, rather than isolating it. Considering the efficiency and accuracy of feature selection, this project will use a filter method (RReliefF) combined with an embedded method (Lasso) for feature selection. Summary of the Invention
[0007] To address the shortcomings of existing technologies, this invention provides a stock pattern similarity query system and method based on domain generalization. This invention will conduct research on the stock pattern similarity query problem, and based on existing data, obtain existing data features to find stocks with similar features.
[0008] A stock pattern similarity query system based on domain generalization includes a data layer, a matching model layer, and an analysis layer;
[0009] The data layer is responsible for data processing, specifically including a stock data module, a data preprocessing module, an RReliefF feature selection module, and a data segmentation module. The data acquisition module is used to acquire and store historical stock data; the data preprocessing module preprocesses the historical stock data; the RReliefF feature selection module performs RReliefF feature selection on the preprocessed data; and the data segmentation module is used to segment the data.
[0010] The matching model layer includes a stock classification model module and a stock classification model training module, which are used to establish a training model and train the training model, and to use the trained model, respectively.
[0011] The analysis layer includes a classification result analysis module and a classification result analysis report module, which are used to analyze the stock classification results output by the training model and generate analysis reports, respectively.
[0012] A stock pattern similarity query method based on domain generalization, implemented based on the aforementioned stock pattern similarity query system based on domain generalization, includes the following steps:
[0013] Step 1: Obtain historical data of the target stock to train the recurrent neural network;
[0014] Step 2: Preprocess the historical stock data obtained in Step 1;
[0015] Step 2.1: Impute missing values in historical stock data;
[0016] Step 2.2: Remove or replace outliers in historical stock data;
[0017] Step 2.3: Divide the data processed in Step 2.2 into training set, validation set, and test set;
[0018] Step 2.4: Normalize the training, validation, and test set data from Step 2.3, but do not normalize the label data of the validation and test sets. During normalization of the validation and test sets, the maximum and minimum values used for each feature are the maximum and minimum values of the training set, thereby optimizing the training quality of the recurrent neural network. The normalization formula is:
[0019]
[0020] Where y i The normalized value of the variable, x i Here, n represents the number of variables in the current feature being normalized, and i represents the i-th variable.
[0021] Step 3: Use the filter feature selection algorithm RReliefF to select features from the preprocessed training data in Step 2, filtering out inferior features that affect the accuracy of the recurrent neural network;
[0022] Step 3.1: Let the normalized training set above be dataset D, the number of sample extractions be m, the threshold for feature weights be δ, the number of original features be N, and the weights for different classifications be N. dC Different attribute weights N dA [A], Weights N for different categories and different attributes dC&dA [A];
[0023] Step 3.2: Set N dC N dA [A]、N dC&dA [A] and the weights W[A] of all features are set to 0;
[0024] Step 3.3: Randomly select a feature sample Ri from D;
[0025] Step 3.4: Find the k nearest neighbor features (I1, I2, ..., Ik) of Ri. k );
[0026] Step 3.5: Calculate N between Ri and its k nearest neighbors. dC The calculation formula is:
[0027] N dC =N dC +|f(R i )-f(I i )|*d(i,j)
[0028] In the formula, f(.) is the classification function.
[0029] Each time N is calculated dC All features need to be calculated N dA [A] and N dC&dA [A], the calculation formulas are as follows:
[0030] N dA [A] = N dA [A]+diff(A,R) i I j )*d(i,j)
[0031] N dC&dA [A] = N dC&dA [A]+|f(R i )-f(I i )|*diff(A, R i I j)*d(i,j)
[0032] In the formula, d(i,j) is R i with I j The distance between two features is calculated using the following formula:
[0033]
[0034] In the formula, k represents R i There are k neighboring features.
[0035] rank(R i I j ) represents I j In R i The distance ranking among the k nearest neighbor feature sequences, where σ is a user-defined parameter.
[0036] diff(A, R) i I j ) represents a computational instance R i With Example I j The difference between attributes, where A specifically refers to the attribute:
[0037]
[0038] d = |value(A, R) i )-value(A, I j | represents the distance between the attribute values of two instances, t eq It is the maximum distance between two attribute values and is always considered equal, t diff It is the minimum distance between attribute values and is always considered unequal;
[0039] Step 3.6: Repeat steps 3.3 to 3.5 for a total of m times;
[0040] Step 3.7: Calculate the total weight of all features using the following formula:
[0041]
[0042] Step 3.8: Sort W[A]. The larger the weight value, the more beneficial the feature is to the training of the learning model. After sorting, retain the top-ranked features according to the number of features to be retained as set by the user.
[0043] Step 3.9: Return all features that were finally selected.
[0044] Step 4: Use the greedy Gaussian segmentation algorithm to segment the training set data into segments with the greatest data differences.
[0045] Step 4.1: Assume the training set data is a multivariate time series. This multivariate time series has T timestamps, each of which is n-dimensional. Let the number of split points be K, the split point set P be empty, and the minimum batch window size be L.
[0046] Step 4.2: Add the starting and ending time indexes of the training set data sequence to P;
[0047] Step 4.3: After the data segments are divided by the split points in P, continue to find the optimal split points that can further divide the current data. Let b1 and b2 be the split points at the two ends of the first data segment. Find the optimal split point of the first data segment between the two ends. Then, continue in this way to find the optimal split point of all data segments. The optimal split point judgment formula is, that is, maximize the following formula value:
[0048]
[0049] In the formula, λ is the trade-off parameter, and b i Let P be an existing partition point, logdet() represents the estimate of the row and column logarithms of the matrix, Tr() represents the trace of the matrix, and S (i) Let S be the empirical covariance of the i-th segment. (i) The calculation formula is:
[0050]
[0051] In the formula μ (i) x is the empirical mean of the current data segment. t The value is the data vector within the current data segment.
[0052] Step 4.4: Add the optimal split point of this round to P;
[0053] Step 4.5: After adding a segmentation point, the historical segmentation point may not be in the optimal position. Therefore, it is necessary to optimize the position of the non-currently added segmentation points in P, that is, adjust the position of the historical segmentation point until all segmentation points reach the optimal segmentation position. The formula for judging whether the optimization process is optimal is the above evaluation formula.
[0054] Step 4.6: Repeat steps 4.1 to 4.5 for a total of K times, and return the split point set P.
[0055] Step 5: Input the segmented training set data and segmentation points into the recurrent neural network, and then use the embedded feature selection algorithm Lasso combined with the domain generalization method to train the recurrent neural network;
[0056] Step 5.1: Obtain the segmentation point set P and the training set data;
[0057] Step 5.2: Divide the training set data into batches based on the split points;
[0058] Step 5.3: Randomly select a batch of data for training the recurrent neural network;
[0059] Step 5.4: Calculate the distribution distance between all data segments in the current batch. The calculation method is to calculate the distance between each pair of data segments, then add them together and calculate the average. The calculation formula is:
[0060]
[0061] In the formula, λ is a hyperparameter, α is an importance parameter representing the distribution distance between two data segments, N is the number of segments, and since this invention uses a recurrent neural network, V is the number of layers in the recurrent neural network. The distance between the maximum mean differences is denoted as , where and These are the outputs of the i-th and j-th data segments in the t-th layer of the recurrent neural network, respectively. The specific calculation formula is as follows:
[0062]
[0063] In the formula h s with h t n refers to the output of a recurrent neural network. s With n t Represents h s with h t Each by n s With n t Composed of multiple multidimensional vectors, h si with h sj h respectively s The i-th and j-th vectors, h ti with h tj Similarly.
[0064] In the formula, k(.) is the kernel function. This invention uses the Gaussian kernel function, which has the following form:
[0065]
[0066] ||xy|| 2 Let σ be the Euclidean distance between vectors x and y, and σ be the standard deviation.
[0067] Step 5.5: Calculate the classification loss L c The calculation formula is:
[0068]
[0069] In the formula, mse(.) is the mean square error and N is the number of segments.
[0070] Step 5.6: Calculate the total loss function, which has the following form:
[0071] Loss = L mat +L dis +λ||θ||1
[0072] In the formula L mat For classification loss, L dis Let θ be the distribution distance, θ be a hyperparameter in the recurrent neural network, and ||.||1 be the L1 regular expression Lasso method.
[0073] Step 5.7: After the loss function is calculated, training of the current batch of the recurrent neural network can begin. Since the total loss function is composed of classification loss, distribution distance and Lasso feature selection algorithm, it can achieve the purpose of classification training, domain generalization and feature selection at the same time.
[0074] Step 5.8: Repeat steps 5.3 to 5.7 until all batches are trained.
[0075] Step 5.9: Repeat steps 5.3 to 5.8 until the classification accuracy of the recurrent neural network reaches the set threshold, at which point the training is complete;
[0076] Step 6: Analyze the classification accuracy of the recurrent neural network. If the accuracy is lower than a custom threshold, it is necessary to determine whether the recurrent neural network needs to be retrained, i.e., repeat steps 3 to 5. If the accuracy is greater than or equal to the custom threshold, the target stock data is preprocessed and then input into the already trained recurrent neural network for feature classification.
[0077] The beneficial effects of adopting the above technical solution are as follows:
[0078] This invention provides a stock pattern similarity query system and method based on domain generalization. The invention employs a domain generalization method to improve the data drift problem in stock data and uses a feature selection method to further enhance model accuracy, providing users with the most reliable basis possible when selecting stock data with similar characteristics. Attached Figure Description
[0079] Figure 1 This is the system framework of the present invention;
[0080] Figure 2 This is the flowchart of the stock data segmentation method of the present invention;
[0081] Figure 3 This describes the model training process for the present invention. Detailed Implementation
[0082] The specific embodiments of the present invention will be described in further detail below with reference to the accompanying drawings and examples. The following examples are for illustrative purposes only and are not intended to limit the scope of the invention.
[0083] A stock pattern similarity query system based on domain generalization, such as Figure 1 As shown, it includes a data layer, a matching model layer, and an analysis layer;
[0084] The data layer is responsible for data processing, specifically including a stock data module, a data preprocessing module, an RReliefF feature selection module, and a data segmentation module. The data acquisition module is used to acquire and store historical stock data; the data preprocessing module preprocesses the historical stock data; the RReliefF feature selection module performs RReliefF feature selection on the preprocessed data; and the data segmentation module is used to segment the data.
[0085] The matching model layer includes a stock classification model module and a stock classification model training module, which are used to establish a training model and train the training model, and to use the trained model, respectively.
[0086] The analysis layer includes a classification result analysis module and a classification result analysis report module, which are used to analyze the stock classification results output by the training model and generate analysis reports, respectively.
[0087] In this embodiment, as shown... Figure 1 As shown, firstly, the stock data acquisition module, i.e. Figure 1The stock data module in the diagram acquires historical stock data and stores it. Then, the historical stock data is preprocessed using the data preprocessing module shown in the diagram. The preprocessed data can be used to train a machine learning model. The machine learning model used in this invention is a recurrent neural network (RNN), but it is not limited to RNNs. A pre-trained machine learning model can also be used to find stocks with similar characteristics, as indicated by the two arrows pointing to the data preprocessing module in the diagram. Before the preprocessed data can be used for training, it must pass through the RreliefF feature selection module for feature selection and the data segmentation module for data segmentation. Only then can the training data be input into the training module, which is the stock classification model training module based on domain generalization and Lasso feature selection shown in the diagram. The trained classification model is input into the model usage module, corresponding to the stock classification model module in the diagram. While using the classification model, the classification results need to be analyzed in real time. The stock classification module then inputs the classification results into the classification result analysis module, which packages the results into an analysis report and sends it to the analysis report storage module, which corresponds to the classification result analysis report module in the diagram. The classification model training module continuously pulls reports from the classification result analysis report module. If the accuracy of the classification results in the report has dropped below a set threshold, the machine learning model needs to be retrained. In this case, the training module uses stock data stored in the stock data module to retrain the classification model. Once trained, the model is sent to the stock classification model module, replacing the model whose classification accuracy has decreased. The stock classification model in the diagram is a classification model based on recurrent neural networks. It's called a classification model because the process of querying similar stock pattern data is a classification process; similar stock data can be classified into one category. The above describes the workflow of the domain generalization-based stock pattern similarity query system.
[0088] A stock pattern similarity query method based on domain generalization, implemented based on the aforementioned stock pattern similarity query system based on domain generalization, includes the following steps:
[0089] Step 1: Obtain historical data of the target stock to train the recurrent neural network;
[0090] In this embodiment, the stock data is obtained from the database of the JoinQuant Quant Trading Platform, and the data is labeled manually.
[0091] Step 2: Preprocess the historical stock data obtained in Step 1; In this embodiment, the programming platform used in the data preprocessing process is Anaconda's Jupyter Notebook. Jupyter Notebook is essentially a web application that facilitates the creation and sharing of program documents, supports live code, mathematical equations and visualizations, and uses Python 3.7 as the programming language.
[0092] Step 2.1: Impute missing values in historical stock data;
[0093] Step 2.2: Remove or replace outliers in historical stock data;
[0094] Step 2.3: Divide the data processed in Step 2.2 into training set, validation set, and test set;
[0095] Step 2.4: Normalize the training, validation, and test set data from Step 2.3, but do not normalize the label data of the validation and test sets. During normalization of the validation and test sets, the maximum and minimum values used for each feature are the maximum and minimum values of the training set, thereby optimizing the training quality of the recurrent neural network. The normalization formula is:
[0096]
[0097] Where y i The normalized value of the variable, x i Here, n represents the number of variables in the current feature being normalized, and i represents the i-th variable.
[0098] Step 3: Use the filter feature selection algorithm RReliefF to select features from the preprocessed training data in Step 2, filtering out inferior features that affect the accuracy of the recurrent neural network; In this embodiment, the programming platform used for feature selection and subsequent steps is PyCharm. PyCharm is a Python integrated development environment with a complete set of tools to help users improve their efficiency when developing with the Python language. The programming language used is Python 3.7.
[0099] Step 3.1: Let the normalized training set above be dataset D, the number of sample extractions be m, the threshold for feature weights be δ, the number of original features be N, and the weights for different classifications be N. dC Different attribute weights N dA [A], Weights N for different categories and different attributes dC&dA [A];
[0100] Step 3.2: Set N dCN dA [A]、N dC&dA [A] and the weights W[A] of all features are set to 0;
[0101] Step 3.3: Randomly select a feature sample R from D. i ;
[0102] Step 3.4: Find R i The k nearest neighbor features (I1, I2, ..., I k );
[0103] Step 3.5: Calculate R i N with k neighboring features dC The calculation formula is:
[0104] N dC =N dC +|f(R i )-f(I i )|*d(i,j)
[0105] In the formula, f(.) is the classification function.
[0106] Each time N is calculated dC All features need to be calculated N dA [A] and N dC&dA [A], the calculation formulas are as follows:
[0107] N dA [A] = N dA [A]+diff(A,R) i I j )*d(i,j)
[0108] N dC&dA [A] = N dC&dA [A]+|f(R i )-f(I i )|*diff(A, R i ,Ij)*d(i,j)
[0109] In the formula, d(i,j) is R i with I j The distance between two features is calculated using the following formula:
[0110]
[0111] In the formula, k represents R i There are k neighboring features.
[0112] rank(R i I j ) represents I j In Ri The distance ranking among the k nearest neighbor feature sequences, where σ is a user-defined parameter.
[0113] diff(A, R) i I j ) represents a computational instance R i With Example I j The difference between attributes, where A specifically refers to the attribute:
[0114]
[0115] d = |value(A, R) i )-value(A, I j | represents the distance between the attribute values of two instances, t eq It is the maximum distance between two attribute values and is always considered equal, t diff It is the minimum distance between attribute values and is always considered unequal;
[0116] Step 3.6: Repeat steps 3.3 to 3.5 for a total of m times;
[0117] Step 3.7: Calculate the total weight of all features using the following formula:
[0118]
[0119] Step 3.8: Sort W[A]. The larger the weight value, the more beneficial the feature is to the training of the learning model. After sorting, retain the top-ranked features according to the number of features to be retained as set by the user.
[0120] Step 3.9: Return all features that were finally selected.
[0121] Step 4: Use the greedy Gaussian segmentation algorithm to segment the training set data into segments with the greatest data differences. The data segmentation process is as follows: Figure 2 As shown;
[0122] Step 4.1: Assume the training set data is a multivariate time series. This multivariate time series has T timestamps, each of which is n-dimensional. Let the number of split points be K, the split point set P be empty, and the minimum batch window size be L.
[0123] Step 4.2: Add the starting and ending time indexes of the training set data sequence to P;
[0124] Step 4.3: After the data segments are divided by the split points in P, continue to find the optimal split points that can further divide the current data. Let b1 and b2 be the split points at the two ends of the first data segment. Find the optimal split point of the first data segment between the two ends. Then, continue in this way to find the optimal split point of all data segments. The optimal split point judgment formula is, that is, maximize the following formula value:
[0125]
[0126] In the formula, λ is the trade-off parameter, and b i Let P be an existing partition point, logdet() represents the estimate of the row and column logarithms of the matrix, Tr() represents the trace of the matrix, and S (i) Let S be the empirical covariance of the i-th segment. (i) The calculation formula is:
[0127]
[0128] In the formula μ (i) x is the empirical mean of the current data segment. t The value is the data vector within the current data segment.
[0129] Step 4.4: Add the optimal split point of this round to P;
[0130] Step 4.5: After adding a segmentation point, the historical segmentation point may not be in the optimal position. Therefore, it is necessary to optimize the position of the non-currently added segmentation points in P, that is, adjust the position of the historical segmentation point until all segmentation points reach the optimal segmentation position. The formula for judging whether the optimization process is optimal is the above evaluation formula.
[0131] Step 4.6: Repeat steps 4.1 to 4.5 for a total of K times, and return the split point set P.
[0132] Step 5: Input the segmented training set data and segmentation points into the recurrent neural network, and then train the recurrent neural network using the Lasso embedded feature selection algorithm combined with the domain generalization method. The training process is as follows: Figure 3 As shown;
[0133] Step 5.1: Obtain the segmentation point set P and the training set data;
[0134] Step 5.2: Divide the training set data into batches based on the split points;
[0135] Step 5.3: Randomly select a batch of data for training the recurrent neural network;
[0136] Step 5.4: Calculate the distribution distance between all data segments in the current batch. The calculation method is to calculate the distance between each pair of data segments, then add them together and calculate the average. The calculation formula is:
[0137]
[0138] In the formula, λ is a hyperparameter, α is an importance parameter representing the distribution distance between two data segments, N is the number of segments, and since this invention uses a recurrent neural network, V is the number of layers in the recurrent neural network. The distance between the maximum mean differences is denoted as , where and These are the outputs of the i-th and j-th data segments in the t-th layer of the recurrent neural network, respectively. The specific calculation formula is as follows:
[0139]
[0140] In the formula h s with h t n refers to the output of a recurrent neural network. s With n t Represents h s with h t Each by n s With n t Composed of multiple multidimensional vectors, h si with h sj h respectively s The i-th and j-th vectors, h ti with h tj Similarly;
[0141] In the formula, k(.) is the kernel function. This invention uses the Gaussian kernel function, which has the following form:
[0142]
[0143] ||xy|| 2 Let σ be the Euclidean distance between vectors x and y, and σ be the standard deviation.
[0144] Step 5.5: Calculate the classification loss L c The calculation formula is:
[0145]
[0146] In the formula, mse(.) is the mean square error and N is the number of segments.
[0147] Step 5.6: Calculate the total loss function, which has the following form:
[0148] Loss = L mat +L dis +λ||θ||1
[0149] In the formula L mat For classification loss, L dis Let θ be the distribution distance, θ be a hyperparameter in the recurrent neural network, and ||.||1 be the L1 regular expression Lasso method.
[0150] Step 5.7: After the loss function is calculated, training of the current batch of the recurrent neural network can begin. Since the total loss function is composed of classification loss, distribution distance and Lasso feature selection algorithm, it can achieve the purpose of classification training, domain generalization and feature selection at the same time.
[0151] Step 5.8: Repeat steps 5.3 to 5.7 until all batches are trained.
[0152] Step 5.9: Repeat steps 5.3 to 5.8 until the classification accuracy of the recurrent neural network reaches the set threshold, at which point the training is complete;
[0153] Step 6: Analyze the classification accuracy of the recurrent neural network. If the accuracy is lower than a custom threshold, it is necessary to determine whether the recurrent neural network needs to be retrained, i.e., repeat steps 3 to 5. If the accuracy is greater than or equal to the custom threshold, the target stock data is preprocessed and then input into the already trained recurrent neural network for feature classification.
[0154] The above description is merely a preferred embodiment of this disclosure and an explanation of the technical principles employed. Those skilled in the art should understand that the scope of the invention involved in the embodiments of this disclosure is not limited to technical solutions formed by specific combinations of the above-described technical features, but should also cover other technical solutions formed by arbitrary combinations of the above-described technical features or their equivalents without departing from the above-described inventive concept. For example, technical solutions formed by substituting the above-described features with (but not limited to) technical features with similar functions disclosed in the embodiments of this disclosure.
Claims
1. A stock pattern similarity query system based on domain generalization, characterized in that, It includes a data layer, a matching model layer, and an analysis layer; The data layer is responsible for processing stock data. The matching model layer includes a stock classification model module and a stock classification model training module, which are used to build a training model and train the training model, and to use the trained model. The analysis layer includes a classification result analysis module and a classification result analysis report module, which are used to analyze the stock classification results output by the training model and generate analysis reports, respectively. The data layer specifically includes a stock data module, a data preprocessing module, an RReliefF feature selection module, and a data segmentation module. The data acquisition module is used to acquire and store historical stock data; the data preprocessing module performs preprocessing on the historical stock data; the RReliefF feature selection module performs RReliefF feature selection on the preprocessed data; and the data segmentation module is used to segment the data. The aforementioned stock pattern similarity query system based on domain generalization is used to implement a stock pattern similarity query method based on domain generalization, and includes the following steps: Step 1: Obtain historical data of the target stock to train the recurrent neural network; Step 2: Preprocess the historical stock data obtained in Step 1; Step 2 specifically includes: Step 2.1: Impute missing values in historical stock data; Step 2.2: Remove or replace outliers in historical stock data; Step 2.3: Divide the data processed in Step 2.2 into training set, validation set, and test set; Step 2.4: Normalize the training, validation, and test set data from Step 2.3, but do not normalize the label data of the validation and test sets. During normalization of the validation and test sets, the maximum and minimum values used for each feature are the maximum and minimum values of the training set, thereby optimizing the training quality of the recurrent neural network. The normalization formula is: Where y i The normalized value of the variable, x i Here, n represents the number of variables in the current feature being normalized, and i represents the i-th variable. Step 3: Use the filter feature selection algorithm RReliefF to select features from the preprocessed training data in Step 2, filtering out inferior features that affect the accuracy of the recurrent neural network; Step 3 specifically includes: Step 3.1: Let the normalized training set above be dataset D, the number of sample extractions be m, the threshold for feature weights be δ, the number of original features be N, and the weights for different classifications be N. dC Different attribute weights N dA [A], Weights N for different categories and different attributes dC&dA [A]; Step 3.2: Set N dC N dA [A]、N dC&dA [A] and the weights W[A] of all features are set to 0; Step 3.3: Randomly select a feature sample R from D. i ; Step 3.4: Find R i The k nearest neighbor features (I1, I2, ..., I k ); Step 3.5: Calculate R i N with k neighboring features dC The calculation formula is: N dC =N dC +|f(R i )-f(I i )|*d(i,j) In the formula, f(.) is the classification function; Each time N is calculated dC All features need to be calculated N dA [A] and N dC&dA [A], the calculation formulas are as follows: N dA [A]=N dA [A]+diff(A,R i ,I j )*d(i,j) N dC&dA [A]=N dC&dA [A]+|f(R i )-f(I i )|*diff(A,R i ,I j )*d(i,j) In the formula, d(i,j) is R i with I j The distance between two features is calculated using the following formula: In the formula, k represents R i There are k neighboring features; rank(R i ,I j ) represents I j In R i The distance ranking among the k nearest neighbor feature sequences, where σ is a user-defined parameter; diff(A,R i ,I j ) represents a computational instance R i With Example I j The difference between attributes, where A specifically refers to the attribute: d = |value(A,R) i )-value(A,I j | represents the distance between the attribute values of two instances, t eq It is the maximum distance between two attribute values and is always considered equal, t diff It is the minimum distance between attribute values and is always considered unequal; Step 3.6: Repeat steps 3.3 to 3.5 for a total of m times; Step 3.7: Calculate the total weight of all features using the following formula: Step 3.8: Sort W[A]. The larger the weight value, the more beneficial the feature is to the training of the learning model. After sorting, retain the top-ranked features according to the number of features to be retained as set by yourself. Step 3.9: Return all the features that were finally selected; Step 4: Use the greedy Gaussian segmentation algorithm to segment the training set data into segments with the greatest data differences. Step 4 specifically includes: Step 4.1: Assume the training set data is a multivariate time series. This multivariate time series has T timestamps, each of which is n-dimensional. Let the number of split points be K, the split point set P be empty, and the minimum batch window size be L. Step 4.2: Add the starting and ending time indexes of the training set data sequence to P; Step 4.3: After the data segments are divided by the split points in P, continue to find the optimal split points that can further divide the current data. Let b1 and b2 be the split points at the two ends of the first data segment. Find the optimal split point of the first data segment between the two ends. Then, continue in this way to find the optimal split point of all data segments. The optimal split point judgment formula is, that is, maximize the following formula value: In the formula, λ is the trade-off parameter, and b i Let P be an existing partition point, logdet() represents the estimate of the row and column logarithms of the matrix, Tr() represents the trace of the matrix, and S (i) Let S be the empirical covariance of the i-th segment. (i) The calculation formula is: In the formula μ (i) x is the empirical mean of the current data segment. t The value is the data vector within the current data segment; Step 4.4: Add the optimal split point of this round to P; Step 4.5: After adding a segmentation point, the historical segmentation point may not be in the optimal position. Therefore, it is necessary to optimize the position of the non-currently added segmentation points in P, that is, adjust the position of the historical segmentation point until all segmentation points reach the optimal segmentation position. The formula for judging whether the optimization process is optimal is the above evaluation formula. Step 4.6: Repeat steps 4.1 to 4.5 for a total of K times, and return the split point set P; Step 5: Input the segmented training set data and segmentation points into the recurrent neural network, and then use the embedded feature selection algorithm Lasso combined with the domain generalization method to train the recurrent neural network; Step 5 specifically includes: Step 5.1: Obtain the segmentation point set P and the training set data; Step 5.2: Divide the training set data into batches based on the split points; Step 5.3: Randomly select a batch of data for training the recurrent neural network; Step 5.4: Calculate the distribution distance between all data segments in the current batch. The calculation method is to calculate the distance between each pair of data segments, then add them together and calculate the average. The calculation formula is: In the formula, λ is a hyperparameter, α is an importance parameter representing the distribution distance between two data segments, N is the number of segments, a recurrent neural network is used, and V is the number of layers in the recurrent neural network. The distance between the maximum mean differences is denoted as , where and These are the outputs of the i-th and j-th data segments in the t-th layer of the recurrent neural network, respectively. The specific calculation formula is as follows: In the formula h s with h t n refers to the output of a recurrent neural network. s With n t Represents h s with h t Each by n s With n t Composed of multiple multidimensional vectors, h si with h sj h respectively s The i-th and j-th vectors, h ti with h tj h respectively t The i-th and j-th vectors; In the formula, k(.) is the kernel function, and a Gaussian kernel function is chosen, which has the following form: ||xy|| 2 Let σ be the Euclidean distance between vectors x and y, and σ be the standard deviation. Step 5.5: Calculate the classification loss L c The calculation formula is: In the formula, mse(.) is the mean square error, and N is the number of segments; Step 5.6: Calculate the total loss function; The total loss function takes the following form: Loss=L mat +L dis +λ||θ||1 In the formula L mat For classification loss, L dis Let θ be the distribution distance, θ be a hyperparameter in the recurrent neural network, and ||.||1 be the L1 regular expression Lasso method; Step 5.7: After the loss function is calculated, training of the current batch of the recurrent neural network can begin. Since the total loss function is composed of classification loss, distribution distance and Lasso feature selection algorithm, it can achieve the purpose of classification training, domain generalization and feature selection at the same time. Step 5.8: Repeat steps 5.3 to 5.7 until all batches are trained. Step 5.9: Repeat steps 5.3 to 5.8 until the classification accuracy of the recurrent neural network reaches the set threshold, at which point the training is complete; Step 6: Analyze the classification accuracy of the recurrent neural network. If the accuracy is lower than the custom threshold, it is necessary to determine whether the recurrent neural network needs to be retrained, that is, to repeat steps 3 to 5. If the accuracy is greater than or equal to the custom threshold, the target stock data is preprocessed and then input into the already trained recurrent neural network for feature classification.