{"id":17155162,"url":"https://github.com/davidrusu/ctw","last_synced_at":"2025-04-13T13:03:26.637Z","repository":{"id":57614555,"uuid":"466335811","full_name":"davidrusu/ctw","owner":"davidrusu","description":"An Implementation of the Context Tree Weighting (CTW) Sequence Prediction Algorithm","archived":false,"fork":false,"pushed_at":"2022-03-05T04:17:14.000Z","size":6,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-27T04:07:53.980Z","etag":null,"topics":["rust","sequence-prediction"],"latest_commit_sha":null,"homepage":"http://www.davidrusu.com/blog#ctw","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/davidrusu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-03-05T03:14:31.000Z","updated_at":"2024-03-21T21:14:59.000Z","dependencies_parsed_at":"2022-08-27T03:24:12.235Z","dependency_job_id":null,"html_url":"https://github.com/davidrusu/ctw","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidrusu%2Fctw","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidrusu%2Fctw/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidrusu%2Fctw/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidrusu%2Fctw/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/davidrusu","download_url":"https://codeload.github.com/davidrusu/ctw/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248717249,"owners_count":21150389,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["rust","sequence-prediction"],"created_at":"2024-10-14T21:50:49.626Z","updated_at":"2025-04-13T13:03:26.603Z","avatar_url":"https://github.com/davidrusu.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ca href=\"https://crates.io/crates/ctw\"\u003e\u003cimg src=\"https://img.shields.io/crates/v/ctw.svg\"\u003e\u003c/a\u003e\n\n# Context Tree Weighting (CTW)\n\nCTW is a lightweight, practical and well performing sequence prediction algorithm discovered by Frans Willems, Yuri Shtarkov and Tjalling Tjalkens (1995).\n\nIt has good query and update performance (linear in the context length).\n\n# Useage\n\nThe following example demonstrates CTW learning the altenating binary sequence 101010...\n\n```rust\nuse ctw::CtwTree;\nlet mut rng = rand::thread_rng();\nlet mut tree = CtwTree::new(8); // context length is 8\n\nlet pattern = [true, false].into_iter().cycle().take(100); // true, false, true, ..\ntree.update_batch(\u0026Vec::from_iter(pattern));\n\nlet mut predictions = Vec::new();\n\nfor _ in 0..10 {\n    let prediction = tree.sample(\u0026mut rng);\n    tree.update(prediction);\n\n    // convert bool to 1 and 0 for legibility\n    let bit = if prediction { 1 } else { 0 };\n    predictions.push(bit);\n}\n\nprintln!(\"predictions: {predictions:?}\"); // --\u003e \"prediction: [1, 0, 1, 0, 1, 0, 1, 0, 1, 0]\"\nassert_eq!(predictions, vec![1,0,1,0,1,0,1,0,1,0]);\n```\n\n# Resources\n\n1. http://www.hutter1.net/publ/sctw.pdf\n2. https://web.stanford.edu/class/ee378a/lecture-notes/lecture_9.pdf\n3. http://www.data-compression.info/Algorithms/CTW/\n4. https://www.cs.cmu.edu/~aarti/Class/10704_Spring15/CTW.pdf (original paper)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidrusu%2Fctw","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdavidrusu%2Fctw","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidrusu%2Fctw/lists"}