Skip to content

Tensorflow

Experimental

This is currently very experimental. The current state consists of essentially having training data and data to analyze get processed by the model. However, that's about it.

To get started, you need to install the tensorflow dependencies:

pip install stocktracer[tensorflow]
poetry install --sync --extras "tensorflow" 

The idea with this analysis module is to leverage tensorflow to create a training model of stocks that match a particular criteria and ones that don't. For example, label a group of stocks as "good" and another group as "bad". Then use the tensorflow decision trees to categorize other unknown stocks as "good" or "bad" based on their attributes.

Example

PYTHONPATH=src poetry run python -m stocktracer analyze --final-year=2023 --final-quarter=1 --tickers aapl,msft,tmo,goog,googl,amzn,meta,acn,wm -a stocktracer.analysis.tensorflow > docs/.smoke-tests/tensorflow.txt
Use /tmp/tmptj3qtjte as temporary training directory
Reading training dataset...
Training dataset read in 0:00:07.895435. Found 50 examples.
Training model...
Model trained in 0:00:00.023778
Compiling model...
Model compiled.
Model: "random_forest_model"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
=================================================================
Total params: 1
Trainable params: 0
Non-trainable params: 1
_________________________________________________________________
Type: "RANDOM_FOREST"
Task: CLASSIFICATION
Label: "__LABEL"

Input Features (12):
    Assets
    AssetsCurrent
    CommonStockSharesIssued
    EarningsPerShareDiluted
    LiabilitiesCurrent
    NetCashProvidedByUsedInOperatingActivities
    OperatingIncomeLoss
    ROA
    current_ratio
    debt_to_assets
    delta_ROA
    net_income

No weights

Variable Importance: INV_MEAN_MIN_DEPTH:
    1.                                        "ROA"  0.567465 ################
    2.                                 "net_income"  0.549115 ############
    3.                        "OperatingIncomeLoss"  0.543807 ###########
    4. "NetCashProvidedByUsedInOperatingActivities"  0.528169 #######
    5.                              "AssetsCurrent"  0.523560 #######
    6.                    "EarningsPerShareDiluted"  0.498339 #
    7.                         "LiabilitiesCurrent"  0.492072 
    8.                                     "Assets"  0.489663 
    9.                              "current_ratio"  0.489130 

Variable Importance: NUM_AS_ROOT:
    1.                                        "ROA" 84.000000 ################
    2.                                 "net_income" 65.000000 ############
    3.                        "OperatingIncomeLoss" 63.000000 ###########
    4. "NetCashProvidedByUsedInOperatingActivities" 40.000000 #######
    5.                              "AssetsCurrent" 35.000000 ######
    6.                    "EarningsPerShareDiluted"  8.000000 #
    7.                         "LiabilitiesCurrent"  3.000000 
    8.                                     "Assets"  2.000000 

Variable Importance: NUM_NODES:
    1.                                        "ROA" 87.000000 ################
    2.                                 "net_income" 70.000000 ############
    3.                        "OperatingIncomeLoss" 63.000000 ###########
    4. "NetCashProvidedByUsedInOperatingActivities" 44.000000 #######
    5.                              "AssetsCurrent" 40.000000 #######
    6.                    "EarningsPerShareDiluted" 11.000000 #
    7.                         "LiabilitiesCurrent"  3.000000 
    8.                                     "Assets"  2.000000 
    9.                              "current_ratio"  2.000000 

Variable Importance: SUM_SCORE:
    1.                                        "ROA" 2787.145582 ################
    2.                                 "net_income" 2178.059286 ############
    3.                        "OperatingIncomeLoss" 2081.938386 ###########
    4. "NetCashProvidedByUsedInOperatingActivities" 1291.245434 #######
    5.                              "AssetsCurrent" 1151.498343 ######
    6.                    "EarningsPerShareDiluted" 227.371409 #
    7.                         "LiabilitiesCurrent" 84.581783 
    8.                                     "Assets" 68.147773 
    9.                              "current_ratio"  2.826852 



Winner takes all: true
Out-of-bag evaluation: accuracy:1 logloss:0.0355234
Number of trees: 300
Total number of nodes: 944

Number of nodes by tree:
Count: 300 Average: 3.14667 StdDev: 0.521366
Min: 3 Max: 5 Ignored: 0
----------------------------------------------
[ 3, 4) 278  92.67%  92.67% ##########
[ 4, 5)   0   0.00%  92.67%
[ 5, 5]  22   7.33% 100.00% #

Depth by leafs:
Count: 622 Average: 1.07074 StdDev: 0.256389
Min: 1 Max: 2 Ignored: 0
----------------------------------------------
[ 1, 2) 578  92.93%  92.93% ##########
[ 2, 2]  44   7.07% 100.00% #

Number of training obs by leaf:
Count: 622 Average: 24.1158 StdDev: 7.05829
Min: 5 Max: 38 Ignored: 0
----------------------------------------------
[  5,  6) 18   2.89%   2.89% ##
[  6,  8)  4   0.64%   3.54% #
[  8, 10)  0   0.00%   3.54%
[ 10, 11)  2   0.32%   3.86%
[ 11, 13)  3   0.48%   4.34%
[ 13, 15) 17   2.73%   7.07% ##
[ 15, 16) 13   2.09%   9.16% ##
[ 16, 18) 42   6.75%  15.92% ######
[ 18, 20) 65  10.45%  26.37% #########
[ 20, 22) 74  11.90%  38.26% ##########
[ 22, 23) 27   4.34%  42.60% ####
[ 23, 25) 54   8.68%  51.29% #######
[ 25, 27) 41   6.59%  57.88% ######
[ 27, 28) 30   4.82%  62.70% ####
[ 28, 30) 69  11.09%  73.79% #########
[ 30, 32) 65  10.45%  84.24% #########
[ 32, 33) 30   4.82%  89.07% ####
[ 33, 35) 38   6.11%  95.18% #####
[ 35, 37) 23   3.70%  98.87% ###
[ 37, 38]  7   1.13% 100.00% #

Attribute in nodes:
    87 : ROA [NUMERICAL]
    70 : net_income [NUMERICAL]
    63 : OperatingIncomeLoss [NUMERICAL]
    44 : NetCashProvidedByUsedInOperatingActivities [NUMERICAL]
    40 : AssetsCurrent [NUMERICAL]
    11 : EarningsPerShareDiluted [NUMERICAL]
    3 : LiabilitiesCurrent [NUMERICAL]
    2 : current_ratio [NUMERICAL]
    2 : Assets [NUMERICAL]

Attribute in nodes with depth <= 0:
    84 : ROA [NUMERICAL]
    65 : net_income [NUMERICAL]
    63 : OperatingIncomeLoss [NUMERICAL]
    40 : NetCashProvidedByUsedInOperatingActivities [NUMERICAL]
    35 : AssetsCurrent [NUMERICAL]
    8 : EarningsPerShareDiluted [NUMERICAL]
    3 : LiabilitiesCurrent [NUMERICAL]
    2 : Assets [NUMERICAL]

Attribute in nodes with depth <= 1:
    87 : ROA [NUMERICAL]
    70 : net_income [NUMERICAL]
    63 : OperatingIncomeLoss [NUMERICAL]
    44 : NetCashProvidedByUsedInOperatingActivities [NUMERICAL]
    40 : AssetsCurrent [NUMERICAL]
    11 : EarningsPerShareDiluted [NUMERICAL]
    3 : LiabilitiesCurrent [NUMERICAL]
    2 : current_ratio [NUMERICAL]
    2 : Assets [NUMERICAL]

Attribute in nodes with depth <= 2:
    87 : ROA [NUMERICAL]
    70 : net_income [NUMERICAL]
    63 : OperatingIncomeLoss [NUMERICAL]
    44 : NetCashProvidedByUsedInOperatingActivities [NUMERICAL]
    40 : AssetsCurrent [NUMERICAL]
    11 : EarningsPerShareDiluted [NUMERICAL]
    3 : LiabilitiesCurrent [NUMERICAL]
    2 : current_ratio [NUMERICAL]
    2 : Assets [NUMERICAL]

Attribute in nodes with depth <= 3:
    87 : ROA [NUMERICAL]
    70 : net_income [NUMERICAL]
    63 : OperatingIncomeLoss [NUMERICAL]
    44 : NetCashProvidedByUsedInOperatingActivities [NUMERICAL]
    40 : AssetsCurrent [NUMERICAL]
    11 : EarningsPerShareDiluted [NUMERICAL]
    3 : LiabilitiesCurrent [NUMERICAL]
    2 : current_ratio [NUMERICAL]
    2 : Assets [NUMERICAL]

Attribute in nodes with depth <= 5:
    87 : ROA [NUMERICAL]
    70 : net_income [NUMERICAL]
    63 : OperatingIncomeLoss [NUMERICAL]
    44 : NetCashProvidedByUsedInOperatingActivities [NUMERICAL]
    40 : AssetsCurrent [NUMERICAL]
    11 : EarningsPerShareDiluted [NUMERICAL]
    3 : LiabilitiesCurrent [NUMERICAL]
    2 : current_ratio [NUMERICAL]
    2 : Assets [NUMERICAL]

Condition type in nodes:
    322 : HigherCondition
Condition type in nodes with depth <= 0:
    300 : HigherCondition
Condition type in nodes with depth <= 1:
    322 : HigherCondition
Condition type in nodes with depth <= 2:
    322 : HigherCondition
Condition type in nodes with depth <= 3:
    322 : HigherCondition
Condition type in nodes with depth <= 5:
    322 : HigherCondition
Node format: NOT_SET

Training OOB:
    trees: 1, Out-of-bag evaluation: accuracy:0.933333 logloss:2.40291
    trees: 11, Out-of-bag evaluation: accuracy:0.98 logloss:0.743573
    trees: 21, Out-of-bag evaluation: accuracy:0.98 logloss:0.040613
    trees: 31, Out-of-bag evaluation: accuracy:1 logloss:0.0275049
    trees: 41, Out-of-bag evaluation: accuracy:1 logloss:0.0319809
    trees: 51, Out-of-bag evaluation: accuracy:1 logloss:0.0373954
    trees: 61, Out-of-bag evaluation: accuracy:1 logloss:0.0403551
    trees: 71, Out-of-bag evaluation: accuracy:1 logloss:0.0429027
    trees: 81, Out-of-bag evaluation: accuracy:1 logloss:0.044798
    trees: 91, Out-of-bag evaluation: accuracy:1 logloss:0.0443501
    trees: 101, Out-of-bag evaluation: accuracy:1 logloss:0.0409493
    trees: 111, Out-of-bag evaluation: accuracy:1 logloss:0.039155
    trees: 121, Out-of-bag evaluation: accuracy:1 logloss:0.0401516
    trees: 131, Out-of-bag evaluation: accuracy:1 logloss:0.0380973
    trees: 141, Out-of-bag evaluation: accuracy:1 logloss:0.0368898
    trees: 151, Out-of-bag evaluation: accuracy:1 logloss:0.037123
    trees: 161, Out-of-bag evaluation: accuracy:1 logloss:0.0354772
    trees: 171, Out-of-bag evaluation: accuracy:1 logloss:0.0355343
    trees: 181, Out-of-bag evaluation: accuracy:1 logloss:0.0351609
    trees: 191, Out-of-bag evaluation: accuracy:1 logloss:0.0362425
    trees: 201, Out-of-bag evaluation: accuracy:1 logloss:0.0357231
    trees: 211, Out-of-bag evaluation: accuracy:1 logloss:0.0355152
    trees: 221, Out-of-bag evaluation: accuracy:1 logloss:0.0347273
    trees: 231, Out-of-bag evaluation: accuracy:1 logloss:0.0333816
    trees: 241, Out-of-bag evaluation: accuracy:1 logloss:0.0335076
    trees: 251, Out-of-bag evaluation: accuracy:1 logloss:0.0337454
    trees: 261, Out-of-bag evaluation: accuracy:1 logloss:0.0338705
    trees: 271, Out-of-bag evaluation: accuracy:1 logloss:0.0352747
    trees: 281, Out-of-bag evaluation: accuracy:1 logloss:0.0351027
    trees: 291, Out-of-bag evaluation: accuracy:1 logloss:0.0346112
    trees: 300, Out-of-bag evaluation: accuracy:1 logloss:0.0355234


1/1 [==============================] - ETA: 0s - loss: 0.0000e+00
1/1 [==============================] - 0s 449ms/step - loss: 0.0000e+00
Empty DataFrame
Columns: []
Index: []

Last update: June 3, 2023