From 22a999e5028c9c120ac67c744b244fa21c2c5d88 Mon Sep 17 00:00:00 2001 From: Daniel Tomlinson Date: Fri, 12 Jul 2019 02:00:12 +0100 Subject: [PATCH] updating scripts --- markov/markov.sublime-workspace | 942 ++++++++++++++++++++++++++++++++ markov/markov_test.py | 209 +++++++ markov/test.py | 36 ++ 3 files changed, 1187 insertions(+) create mode 100644 markov/markov.sublime-workspace create mode 100644 markov/markov_test.py create mode 100644 markov/test.py diff --git a/markov/markov.sublime-workspace b/markov/markov.sublime-workspace new file mode 100644 index 0000000..f0b61f4 --- /dev/null +++ b/markov/markov.sublime-workspace @@ -0,0 +1,942 @@ +{ + "auto_complete": + { + "selected_items": + [ + [ + "mea", + "mean_absolute_error\tfunction" + ], + [ + "roc", + "roc_auc_score\tfunction" + ], + [ + "mean", + "mean_squared_error\tfunction" + ], + [ + "r", + "r2_score\tfunction" + ], + [ + "prec", + "precision_score\tfunction" + ], + [ + "re", + "recall_score\tfunction" + ], + [ + "lin", + "lin_reg\tstatement" + ], + [ + "X", + "X_test\tstatement" + ], + [ + "dec", + "dec_tree\tstatement" + ], + [ + "yt", + "y_train\tstatement" + ], + [ + "y_", + "y_train\tstatement" + ], + [ + "ran", + "ran_for\tstatement" + ], + [ + "Random", + "RandomForestRegressor\tclass" + ], + [ + "Dec", + "DecisionTreeRegressor\tclass" + ], + [ + "instant", + "instaniated_models\tstatement" + ], + [ + "i", + "i" + ], + [ + "train", + "train_test_split\tfunction" + ], + [ + "tes", + "train_test_split\tfunction" + ], + [ + "model", + "model_selection\tmodule" + ], + [ + "min", + "min_samples_leaf\tparam" + ], + [ + "max", + "max_depth\tparam" + ], + [ + "accur", + "accuracy_score\tfunction" + ], + [ + "y", + "y_pred\tstatement" + ], + [ + "reg", + "reg_coef\tstatement" + ], + [ + "V", + "Var_Y" + ], + [ + "Var", + "Var_X" + ], + [ + "li", + "bmi_life_model" + ], + [ + "cla", + "classmethod\tclass" + ], + [ + "tester", + "tester1" + ], + [ + "clas", + "class\tNew Class" + ], + [ + "int", + "int\tclass" + ], + [ + "set", + "setattr\tfunction" + ], + [ + "st", + "Standalone" + ], + [ + "M", + "MyClass\tclass" + ], + [ + "m", + "my_printer\tstatement" + ], + [ + "from", + "fromBirthYear\tfunction" + ], + [ + "class", + "classmethod\tclass" + ], + [ + "par", + "partialmethod\tclass" + ], + [ + "db", + "initialize_db" + ], + [ + "get", + "getattr\tfunction" + ], + [ + "name", + "__name__" + ], + [ + "__get", + "__getattribute__\tfunction" + ], + [ + "kwargs", + "kwargs" + ], + [ + "func", + "functools\tmodule" + ], + [ + "wra", + "wrapper_slow_down\tfunction" + ], + [ + "slo", + "slowDownDecorator\tclass" + ], + [ + "slow", + "slowDown\tclass" + ], + [ + "init", + "init" + ], + [ + "call", + "callable" + ], + [ + "Slo", + "Slow_Down_Decorator\tclass" + ], + [ + "Slow", + "Slow_Down\tclass" + ], + [ + "partial", + "partial\tclass" + ], + [ + "coun", + "count_down\tfunction" + ], + [ + "num", + "num_calls\tstatement" + ], + [ + "sum", + "sum_squares" + ], + [ + "wrapper", + "wrapper_count_calls\tfunction" + ], + [ + "n", + "num_calls" + ], + [ + "str", + "str\tclass" + ], + [ + "repea", + "repeat_partial\tfunction" + ], + [ + "parti", + "repeat_partial_wrapper" + ], + [ + "repeat", + "repeatN\tfunction" + ], + [ + "wrapper_rep", + "wrapper_repeatN\tfunction" + ], + [ + "de", + "decorator_repeat\tfunction" + ], + [ + "Call", + "callable\tfunction" + ], + [ + "R", + "ReferenceError\tclass" + ], + [ + "gree", + "greeter_func\tstatement" + ], + [ + "wrap", + "wrapper_debug\tfunction" + ], + [ + "__", + "__name__" + ], + [ + "kw", + "kwargs_repr\tstatement" + ], + [ + "args", + "args_repr" + ], + [ + "ge", + "get_type\tfunction" + ], + [ + "no", + "not_during_the_night\tfunction" + ], + [ + "ex", + "executable\tinstance" + ], + [ + "script", + "script_file\tstatement" + ], + [ + "sc", + "script_file\tstatement" + ], + [ + "Ke", + "KeyError\tclass" + ], + [ + "folder", + "folder" + ], + [ + "ar", + "arguments\tstatement" + ], + [ + "local", + "locals_val\tstatement" + ], + [ + "arguments", + "arguments" + ], + [ + "sctr", + "script2_main\tfunction" + ], + [ + "f", + "f" + ], + [ + "read_", + "read_excel\tfunction" + ], + [ + "read", + "read_csv\tfunction" + ], + [ + "is", + "is_tasty" + ], + [ + "prin", + "print_sound" + ], + [ + "w", + "wheels" + ], + [ + "pr", + "print_sound" + ], + [ + "date", + "datetime\tclass" + ], + [ + "p", + "print_wheels\tfunction" + ], + [ + "File", + "FileNotFoundError\tclass" + ], + [ + "borg", + "borg" + ], + [ + "log", + "log" + ], + [ + "conver", + "convert_to_uppercase\tfunction" + ], + [ + "hell", + "hellow_world\tstatement" + ], + [ + "__m", + "__name__\tinstance" + ], + [ + "fu", + "func\tparam" + ], + [ + "fun", + "functools\tmodule" + ], + [ + "funct", + "function_call\tfunction" + ], + [ + "d", + "double_volume\tfunction" + ], + [ + "s", + "surface_area\tfunction" + ], + [ + "new", + "new_length\tparam" + ], + [ + "Cube", + "Cube" + ], + [ + "area", + "area_2\tfunction" + ], + [ + "tri", + "tri_area\tfunction" + ], + [ + "triangle", + "triangle_area\tstatement" + ], + [ + "bas", + "base_area\tstatement" + ], + [ + "sl", + "slant_height\tparam" + ], + [ + "heigh", + "height_tripled\tfunction" + ], + [ + "h", + "height_doubled\tfunction" + ], + [ + "__in", + "__init__\tfunction" + ], + [ + "ba", + "base_area" + ], + [ + "garb", + "garbage_check\tfunction" + ], + [ + "len", + "length\tparam" + ], + [ + "__init", + "__init_subclass__\tfunction" + ], + [ + "Square", + "Square\tclass" + ], + [ + "se", + "self" + ], + [ + "list", + "list_of_ips\tstatement" + ], + [ + "su", + "sub_list\tstatement" + ], + [ + "def", + "def\tFunction" + ] + ] + }, + "buffers": + [ + ], + "build_system": "", + "build_system_choices": + [ + ], + "build_varint": "", + "command_palette": + { + "height": 0.0, + "last_filter": "", + "selected_items": + [ + [ + "replv", + "SublimeREPL: Python - virtualenv" + ], + [ + "ac", + "Virtualenv: Activate" + ], + [ + "activ", + "Virtualenv: Activate" + ], + [ + "repl v", + "SublimeREPL: Python - virtualenv" + ], + [ + "virtualen", + "Virtualenv: Activate" + ], + [ + "repl p", + "SublimeREPL: Python - virtualenv" + ], + [ + "repl py", + "SublimeREPL: Python - virtualenv" + ], + [ + "repel p", + "SublimeREPL: Python - virtualenv" + ], + [ + "error", + "SublimeLinter: Show All Errors" + ], + [ + "show", + "SublimeLinter: Show All Errors" + ], + [ + "rep", + "SublimeREPL: Python - virtualenv" + ], + [ + "python", + "SublimeREPL: Python - virtualenv" + ], + [ + "virtual", + "Virtualenv: Activate" + ], + [ + "python vi", + "SublimeREPL: Python - virtualenv" + ], + [ + "repl python", + "SublimeREPL: Python - virtualenv" + ], + [ + "virtua", + "Virtualenv: Activate" + ], + [ + "sublimerepl", + "Virtualenv: SublimeREPL - Python" + ], + [ + "install", + "Package Control: Install Package" + ], + [ + "virt", + "Virtualenv: Activate" + ], + [ + "virtu", + "Virtualenv: Activate" + ], + [ + "packa", + "Package Control: Install Package" + ], + [ + "vir", + "Virtualenv: New (venv)" + ], + [ + "install pa", + "Package Control: Install Package" + ], + [ + "insta", + "Package Control: Install Package" + ], + [ + "ins", + "Package Control: Install Package" + ], + [ + "brow", + "Preferences: Browse Packages" + ], + [ + "browse", + "Preferences: Browse Packages" + ], + [ + "remove", + "Package Control: Remove Package" + ], + [ + "Snippet: ", + "Snippet: For Loop" + ], + [ + "remo", + "Package Control: Remove Channel" + ], + [ + "show all", + "SublimeLinter: Show All Errors" + ], + [ + "prefs", + "Preferences: SublimeLinter Settings" + ], + [ + "package in", + "Package Control: Install Package" + ], + [ + "install pack", + "Package Control: Install Package" + ], + [ + "ayu: Activate theme", + "ayu: Activate theme" + ], + [ + "Browse Pack", + "Preferences: Browse Packages" + ], + [ + "Package Control: insta", + "Package Control: Install Package" + ] + ], + "width": 0.0 + }, + "console": + { + "height": 250.0, + "history": + [ + "sublime.cache_path()", + "import urllib.request,os,hashlib; h = '6f4c264a24d933ce70df5dedcf1dcaee' + 'ebe013ee18cced0ef93d5f746d80ef60'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); by = urllib.request.urlopen( 'http://packagecontrol.io/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); print('Error validating download (got %s instead of %s), please try manual install' % (dh, h)) if dh != h else open(os.path.join( ipp, pf), 'wb' ).write(by)" + ] + }, + "distraction_free": + { + "menu_visible": true, + "show_minimap": false, + "show_open_files": false, + "show_tabs": false, + "side_bar_visible": false, + "status_bar_visible": false + }, + "expanded_folders": + [ + ], + "file_history": + [ + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Model Evaluation Metrics/Regression Metrics/regression.py", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Model Evaluation Metrics/Regression Metrics/tests2.py", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Model Evaluation Metrics/3. Testing your models/tests.py", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Model Evaluation Metrics/3. Testing your models/quiz.py", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Model Evaluation Metrics/3. Testing your models/classification_metrics.py", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Model Evaluation Metrics/3. Testing your models/data.csv", + "/home/dtomlinson/git-repos/sublime/packages", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Ensemble Methods/spam_ensembles.py", + "/home/dtomlinson/git-repos/sublime/licesne", + "/home/dtomlinson/git-repos/sublime/.config/sublime-text-3/Packages/User/SublimeLinter.sublime-settings", + "/home/dtomlinson/git-repos/sublime/Projects/base.sublime-project", + "/home/dtomlinson/sublime/base.sublime-project", + "/home/dtomlinson/git-repos/sublime/.config/sublime-text-3/Packages/Preferences.sublime-settings", + "/home/dtomlinson/git-repos/sublime/Preferences.sublime-settings", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/.gitignore", + "/home/dtomlinson/projects/temp/test.py", + "/home/dtomlinson/projects/markov/markov.py", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Support Vector Machines/data.csv", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Support Vector Machines/quiz.py", + "/home/dtomlinson/docker-commands/wordpress", + "/home/dtomlinson/projects/.gitignore", + "/home/dtomlinson/projects/README.md", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/README.md", + "/home/dtomlinson/projects/reddit/comment_scrape.py", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Decision Trees/19. Titanic Exploration/titanic.py", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Naive Bayes/Bayesian_Inference.py", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Decision Trees/19. Titanic Exploration/titanic_solution.py", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Decision Trees/18. Decision Trees in sklearn/titanic.py", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Decision Trees/18. Decision Trees in sklearn/data.csv", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Decision Trees/18. Decision Trees in sklearn/quiz.py", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Decision Trees/Calculating information gain on a dataset.py", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Perceptron Algoritm/9. Perceptron Algorithm/data.csv", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Perceptron Algoritm/9. Perceptron Algorithm/perceptron.py", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Perceptron Algoritm/7 Perceptrons as Logical Operators/perceptron_not.py", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Perceptron Algoritm/7 Perceptrons as Logical Operators/perceptron_and.py", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Linear Regression/28. Feature Scaling/feature_scaling.py", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Linear Regression/28. Feature Scaling/data.csv", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Linear Regression/27. Regularization/regularization.py", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Linear Regression/27. Regularization/data.csv", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Linear Regression/25. Polynomial Regression/data.csv", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Linear Regression/25. Polynomial Regression/regression.py", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Linear Regression/25. Polynomial Regression/solution.py", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Linear Regression/20. Multiple Linear Regression/quiz.py", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Linear Regression/18. Linear Regression in scikit-learn/data.csv", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Linear Regression/18. Linear Regression in scikit-learn/regression.py", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Linear Regression/16 Quiz: Mini-Batch Gradient Descent/solution.py", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Linear Regression/16 Quiz: Mini-Batch Gradient Descent/data.csv", + "/home/dtomlinson/courses/Udacity Machine Learning - Introduction Nanodegree Program/python/Supervised Learning/Linear Regression/16 Quiz: Mini-Batch Gradient Descent/batch_graddesc.py", + "/home/dtomlinson/projects/learning/property/property.py", + "/home/dtomlinson/projects/learning/descriptors/test.py", + "/home/dtomlinson/projects/learning/decorators/decorator.py", + "/home/dtomlinson/projects/learning/decorators/classes_with_args.py", + "/home/dtomlinson/projects/learning/decorators/test.py", + "/tmp/mozilla_dtomlinson0/borg-docker-plex-do.sh", + "/home/dtomlinson/.config/sublime-text-3/Packages/User/SublimeREPL.sublime-settings", + "/home/dtomlinson/projects/learning/learning.sublime-project", + "/home/dtomlinson/projects/boto3/blocking_suggestions/.history.txt", + "/home/dtomlinson/.bash_profile", + "/home/dtomlinson/projects/learning/singletons/client_a.py", + "/home/dtomlinson/projects/learning/singletons/client_b.py", + "/home/dtomlinson/projects/learning/singletons/db.py", + "/home/dtomlinson/projects/learning/decorators/function_decorators.py", + "/home/dtomlinson/projects/learning/decorators/classes_decorater.py", + "/home/dtomlinson/.config/sublime-text-3/Packages/SublimeREPL/SublimeREPL.sublime-settings", + "/home/dtomlinson/projects/learning/decorators/script1.py", + "/home/dtomlinson/projects/learning/decorators/decorators.py", + "/home/dtomlinson/projects/learning/decorators/__init__.py", + "/home/dtomlinson/projects/learning/decorators/pathfix.py", + "/home/dtomlinson/projects/learning/import-args/script1.py", + "/home/dtomlinson/projects/learning/super/class_1.py", + "/home/dtomlinson/projects/learning/super/temp.py", + "/home/dtomlinson/projects/learning/import-args/script2.py", + "/home/dtomlinson/projects/learning/import-args/script3.py", + "/home/dtomlinson/projects/bayes-learning/lesson1.py", + "/home/dtomlinson/.virtualenvs/bayes-learning/lib/python3.7/site-packages/pandas/core/series.py", + "/home/dtomlinson/.virtualenvs/bayes-learning/lib/python3.7/site-packages/pandas/io/api.py", + "/home/dtomlinson/.virtualenvs/bayes-learning/lib/python3.7/site-packages/pandas/io/excel.py", + "/home/dtomlinson/docker-commands/bookstack-temp.txt", + "/home/dtomlinson/projects/learning/super/triangle.py", + "/home/dtomlinson/projects/learning/super/rectangle-square.py", + "/home/dtomlinson/projects/learning/class-and-object-orianted-python/lesson1.py", + "/usr/lib64/python3.7/datetime.py", + "/home/dtomlinson/projects/bayes-learning/seaborn.py", + "/home/dtomlinson/.virtualenvs/bayes-learning/lib/python3.7/site-packages/pandas/core/frame.py", + "/home/dtomlinson/Downloads/borg-backup-plex-do.sh", + "/home/dtomlinson/Downloads/borg-docker-plex-do.sh", + "/home/dtomlinson/Downloads/tmp/borg-docker-plex-do.sh", + "/home/dtomlinson/Downloads/tmp/borg-backup-plex-do.sh", + "/home/dtomlinson/Downloads/borg-docker-plex-server.sh", + "/home/dtomlinson/Downloads/borg-docker-plex-download.sh", + "/home/dtomlinson/Downloads/borg-backup-plex-download.sh", + "/home/dtomlinson/Downloads/borg-backup-plex-server.sh", + "/home/dtomlinson/Downloads/credentials.csv", + "/home/dtomlinson/requirements.txt", + "/home/dtomlinson/.config/sublime-text-3/Packages/Anaconda/Anaconda.sublime-settings", + "/home/dtomlinson/.config/sublime-text-3/Packages/User/Anaconda.sublime-settings", + "/home/dtomlinson/projects/learning/test.py", + "/home/dtomlinson/projects/temp/temp_1.py", + "/home/dtomlinson/projects/learning/class-and-object-orianted-python/links.txt", + "/home/dtomlinson/projects/base/base.sublime-project", + "/home/dtomlinson/projects/temp/temp_new.sublime-project", + "/home/dtomlinson/projects/temp/temp.sublime-project", + "/home/dtomlinson/.config/sublime-text-3/Packages/User/ayu-mirage.sublime-theme", + "/home/dtomlinson/.config/sublime-text-3/Packages/User/ayu-dark.sublime-theme", + "/home/dtomlinson/.config/sublime-text-3/Packages/SideBarEnhancements/Side Bar.sublime-settings", + "/home/dtomlinson/.config/sublime-text-3/Packages/User/Python.sublime-settings" + ], + "find": + { + "height": 29.0 + }, + "find_in_files": + { + "height": 0.0, + "where_history": + [ + ] + }, + "find_state": + { + "case_sensitive": false, + "find_history": + [ + "r2", + "import", + ",", + "= ", + " =", + "= ", + " i", + "i", + "f1_score", + "recall_score", + "precision_score", + "accuracy_score", + "fit", + "- ", + "movie", + "movie is", + "r", + ",", + "0", + "__x", + "x", + "arg", + "fibonacci", + "self", + "decorator", + "self" + ], + "highlight": true, + "in_selection": false, + "preserve_case": false, + "regex": false, + "replace_history": + [ + ], + "reverse": false, + "show_context": true, + "use_buffer2": true, + "whole_word": true, + "wrap": false + }, + "groups": + [ + { + "sheets": + [ + ] + } + ], + "incremental_find": + { + "height": 29.0 + }, + "input": + { + "height": 51.0 + }, + "layout": + { + "cells": + [ + [ + 0, + 0, + 1, + 1 + ] + ], + "cols": + [ + 0.0, + 1.0 + ], + "rows": + [ + 0.0, + 1.0 + ] + }, + "menu_visible": true, + "output.SublimeLinter": + { + "height": 0.0 + }, + "output.exec": + { + "height": 132.0 + }, + "output.find_results": + { + "height": 0.0 + }, + "pinned_build_system": "Packages/Virtualenv/Python + Virtualenv.sublime-build", + "project": "/home/dtomlinson/sublime/base.sublime-project", + "replace": + { + "height": 54.0 + }, + "save_all_on_build": true, + "select_file": + { + "height": 0.0, + "last_filter": "", + "selected_items": + [ + ], + "width": 0.0 + }, + "select_project": + { + "height": 500.0, + "last_filter": "", + "selected_items": + [ + [ + "", + "~/projects/bayes-learning/seaborn.sublime-workspace" + ] + ], + "width": 380.0 + }, + "select_symbol": + { + "height": 0.0, + "last_filter": "", + "selected_items": + [ + ], + "width": 0.0 + }, + "selected_group": 0, + "settings": + { + }, + "show_minimap": true, + "show_open_files": true, + "show_tabs": true, + "side_bar_visible": true, + "side_bar_width": 276.0, + "status_bar_visible": true, + "template_settings": + { + "max_columns": 2 + } +} diff --git a/markov/markov_test.py b/markov/markov_test.py new file mode 100644 index 0000000..d2e2114 --- /dev/null +++ b/markov/markov_test.py @@ -0,0 +1,209 @@ +import numpy as np +import itertools +import functools + + +""" Set our parameters """ +# Should we seed the results? +setSeed = False +seedNum = 27 + +""" Simulation parameters """ +# Should we simulate more than once? +setSim = True +simNum = 1 + +""" Define our data """ +# The Statespace +states = np.array(['L', 'w', 'W']) + +# Possible sequences of events +transitionName = np.array([['LL', 'Lw', 'LW'], + ['wL', 'ww', 'wW'], + ['WL', 'Ww', 'WW']]) + +# Probabilities Matrix (transition matrix) +transitionMatrix = np.array([[0.6, 0.1, 0.3], + [0.1, 0.7, 0.2], + [0.2, 0.2, 0.6]]) + +# Starting state +startingState = 'w' +initial_dist = np.array([0.3, 0.3, 0.4]) + +# Steps to run +stepTime = 2 +# End state you want to find probabilites of +endState = 'W' + +# Get P_steps +p_steps = False + +# Get Stationary Dist +stat_dist = False + + +# A class that implements the Markov chain to forecast the state/mood: +class markov(object): + """simulates a markov chain given its states, current state and + transition matrix. + + Parameters: + states: 1-d array containing all the possible states + transitionName: 2-d array containing a list + of the all possible state directions + transitionMatrix: 2-d array containing all + the probabilites of moving to each state + currentState: a string indicating the starting state + steps: an integer determining how many steps (or times) to simulate""" + + def __init__(self, states: np.array, transitionName: np.array, + transitionMatrix: np.array, currentState: str, + steps: int): + super(markov, self).__init__() + self.states = states + self.list = list + self.transitionName = transitionName + self.transitionMatrix = transitionMatrix + self.currentState = currentState + self.steps = steps + + @staticmethod + def setSeed(num: int): + return np.random.seed(num) + + @staticmethod + def p_steps(transitionMatrix, initial_dist, steps): + for _ in itertools.repeat(None, steps): + initial_dist = transitionMatrix.T.dot(initial_dist) + return initial_dist + + @staticmethod + def stationary_dist(transitionMatrix, initial_dist, steps): + for _ in itertools.repeat(None, steps): + initial_dist = transitionMatrix.T.dot(initial_dist) + return initial_dist + + @functools.lru_cache(maxsize=128) + def forecast(self): + print(f'Start state: {self.currentState}') + # Shall store the sequence of states taken + self.stateList = [self.currentState] + i = 0 + # To calculate the probability of the stateList + self.prob = 1 + while i != self.steps: + if self.currentState == 'L': + self.change = np.random.choice(self.transitionName[0], + replace=True, + p=transitionMatrix[0]) + if self.change == 'LL': + self.prob = self.prob * 0.8 + self.stateList.append('L') + pass + elif self.change == 'Lw': + self.prob = self.prob * 0.15 + self.currentState = 'w' + self.stateList.append('w') + else: + self.prob = self.prob * 0.05 + self.currentState = "W" + self.stateList.append("W") + elif self.currentState == "w": + self.change = np.random.choice(self.transitionName[1], + replace=True, + p=transitionMatrix[1]) + if self.change == "ww": + self.prob = self.prob * 0.15 + self.stateList.append("w") + pass + elif self.change == "wL": + self.prob = self.prob * 0.8 + self.currentState = "L" + self.stateList.append("L") + else: + self.prob = self.prob * 0.05 + self.currentState = "W" + self.stateList.append("W") + elif self.currentState == "W": + self.change = np.random.choice(self.transitionName[2], + replace=True, + p=transitionMatrix[2]) + if self.change == "WW": + self.prob = self.prob * 0.05 + self.stateList.append("W") + pass + elif self.change == "WL": + self.prob = self.prob * 0.8 + self.currentState = "L" + self.stateList.append("L") + else: + self.prob = self.prob * 0.15 + self.currentState = "w" + self.stateList.append("w") + i += 1 + print(f'Possible states: {self.stateList}') + print(f'End state after {self.steps} steps: {self.currentState}') + print(f'Probability of all the possible sequence of states:' + f' {self.prob}\n') + return self.stateList + + +def main(*args, **kwargs): + global startingState + try: + simNum = kwargs['simNum'] + except KeyError: + pass + sumTotal = 0 + # Check validity of transitionMatrix + for i in range(len(transitionMatrix)): + sumTotal += sum(transitionMatrix[i]) + if i != len(states) and i == len(transitionMatrix): + raise ValueError('Probabilities should add to 1') + # Set the seed so we can repeat with the same results + if setSeed: + markov.setSeed(seedNum) + # Save our simulations: + list_state = [] + count = 0 + # Simulate Multiple Times + if setSim: + if initial_dist is not None: + startingState = np.random.choice(states, p=initial_dist) + for _ in itertools.repeat(None, simNum): + markovChain = markov(states, transitionName, + transitionMatrix, startingState, + stepTime) + list_state.append(markovChain.forecast()) + startingState = np.random.choice(states, p=initial_dist) + else: + for _ in itertools.repeat(None, simNum): + markovChain = markov(states, transitionName, + transitionMatrix, startingState, + stepTime) + list_state.append(markovChain.forecast()) + else: + for _ in range(1, 2): + list_state.append(markov(states, transitionName, + transitionMatrix, startingState, + stepTime).forecast()) + for list in list_state: + if(list[-1] == f'{endState!s}'): + print(True, list) + count += 1 + else: + print(False, list) + if setSim is False: + simNum = 1 + print(f'\nThe probability of starting in {startingState} and finishing' + f' in {endState} after {stepTime} steps is {(count / simNum):.2%}') + if p_steps: + print(f'P_{stepTime} is ' + f'{markov.p_steps(transitionMatrix, initial_dist, stepTime)}') + if stat_dist: + print(f'Stat dist is {markov.stationary_dist(transitionMatrix,initial_dist, stepTime)}') + + +if __name__ == '__main__': + main(simNum=simNum) diff --git a/markov/test.py b/markov/test.py new file mode 100644 index 0000000..def450e --- /dev/null +++ b/markov/test.py @@ -0,0 +1,36 @@ +import numpy as np + +np.random.seed(1234) + +n_states = 5 +n_steps = 50 +tolerance = 1e-5 + +# Random transition matrix and state vector +transitionMatrix = np.array([[0.6, 0.1, 0.3], + [0.1, 0.7, 0.2], + [0.2, 0.2, 0.6]]) + +# initial dist = [L, w, W] +intital_dist = np.array([1, 0, 0]) + +# Normalize rows in transitionMatrix +# transitionMatrix /= transitionMatrix.sum(axis=1)[:, np.newaxis] + +# # Normalize p +# p /= p.sum() + +# Take steps +for k in range(n_steps): + intital_dist = transitionMatrix.T.dot(intital_dist) + +p_50 = intital_dist +print(f'p {p_50}') + +# Compute stationary state +w, v = np.linalg.eig(transitionMatrix.T) + +j_stationary = np.argmin(abs(w - 1.0)) +p_stationary = v[:, j_stationary].real +p_stationary /= p_stationary.sum() +print(p_stationary)