<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Tianchi YU(于天池)&apos;s Blog</title><description>my blog</description><link>https://tianchiyu.me/</link><item><title>Performance Benefits From Underutilized CPU</title><link>https://tianchiyu.me/blog/2023-08-18-performance-benefits-from-underutilized-cpu/</link><guid isPermaLink="true">https://tianchiyu.me/blog/2023-08-18-performance-benefits-from-underutilized-cpu/</guid><description>Notes on CPU utilization and performance.</description><pubDate>Fri, 18 Aug 2023 00:00:00 GMT</pubDate><content:encoded/></item><item><title>How to Write A CORRECT LLVM Pass As A Plugin</title><link>https://tianchiyu.me/blog/2023-08-06-how-to-write-a-correct-llvm-pass-as-a-plugin/</link><guid isPermaLink="true">https://tianchiyu.me/blog/2023-08-06-how-to-write-a-correct-llvm-pass-as-a-plugin/</guid><description>Notes on writing an LLVM pass as a plugin.</description><pubDate>Sun, 06 Aug 2023 00:00:00 GMT</pubDate><content:encoded/></item><item><title>What is Three Address Code</title><link>https://tianchiyu.me/blog/2023-08-02-what-is-three-address-code/</link><guid isPermaLink="true">https://tianchiyu.me/blog/2023-08-02-what-is-three-address-code/</guid><description>An introduction to three-address code, its structure, and its role in compiler optimization.</description><pubDate>Wed, 02 Aug 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;Three Address Code (TAC) is an intermediate code generation technique used by compilers to represent high-level language constructs in a lower-level, more manageable form. TAC is called &quot;three address code&quot; because each instruction in this intermediate representation typically involves at most three addresses or operands. These addresses can represent variables, constants, or temporary results. TAC is an essential part of the compilation process as it simplifies the optimization and code generation phases.&lt;/p&gt;
&lt;h2&gt;Characteristics of Three Address Code&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Simplicity&lt;/strong&gt;: TAC uses a simple and limited set of instructions, making it easier to analyze and optimize.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Efficiency&lt;/strong&gt;: TAC strikes a balance between high-level language constructs and low-level machine instructions, allowing for efficient code generation.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Flexibility&lt;/strong&gt;: TAC can represent a wide range of programming constructs, including arithmetic operations, control flow statements, and function calls.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Ease of Optimization&lt;/strong&gt;: The simplicity of TAC makes it easier for compilers to apply various optimization techniques to improve the performance of the generated code.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;Basic Structure of TAC&lt;/h2&gt;
&lt;p&gt;A typical TAC instruction consists of at most three operands, which can be variables, constants, or temporary values. The general format of a TAC instruction is as follows:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;result = operand1 operator operand2
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here, &lt;code&gt;result&lt;/code&gt; is the variable or temporary location where the result of the operation is stored, &lt;code&gt;operand1&lt;/code&gt; and &lt;code&gt;operand2&lt;/code&gt; are the input operands, and &lt;code&gt;operator&lt;/code&gt; is the operation to be performed (e.g., +, -, *, /).&lt;/p&gt;
&lt;h2&gt;Example&lt;/h2&gt;
&lt;p&gt;Consider the following high-level code snippet:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;a = b + c * d;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The corresponding TAC representation might look like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;t1 = c * d
a = b + t1
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this example, &lt;code&gt;t1&lt;/code&gt; is a temporary variable used to store the result of the multiplication before it is added to &lt;code&gt;b&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;Three Address Code is a valuable intermediate representation used by compilers to bridge the gap between high-level programming languages and low-level machine code. Its simplicity, efficiency, and flexibility make it an ideal choice for optimizing and generating code for a wide range of programming constructs. Understanding TAC is essential for anyone interested in compiler design and implementation.&lt;/p&gt;
</content:encoded></item><item><title>Lexical Analysis, Syntax Analysis, and Semantic Analysis</title><link>https://tianchiyu.me/blog/2023-08-01-lexical-analysis-syntax-analysis-and-semantic-analysis-copy/</link><guid isPermaLink="true">https://tianchiyu.me/blog/2023-08-01-lexical-analysis-syntax-analysis-and-semantic-analysis-copy/</guid><description>Notes on lexical analysis, syntax analysis, and semantic analysis in compilers.</description><pubDate>Tue, 01 Aug 2023 00:00:00 GMT</pubDate><content:encoded/></item><item><title>Rust Basics 1(Quick Review)</title><link>https://tianchiyu.me/blog/2023-07-01-rust-basics-1quick-review/</link><guid isPermaLink="true">https://tianchiyu.me/blog/2023-07-01-rust-basics-1quick-review/</guid><description>A quick review of Rust programs, compilation, variables, and basic language concepts.</description><pubDate>Sat, 01 Jul 2023 00:00:00 GMT</pubDate><content:encoded>&lt;h1&gt;Rust Basics(1)&lt;/h1&gt;
&lt;h2&gt;Anatomy of a Rust Program&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;indent with four spaces, not a tab&lt;/li&gt;
&lt;li&gt;&lt;code&gt;println!&lt;/code&gt; calls a Rust marco&lt;/li&gt;
&lt;li&gt;end the line with a semicolon(;)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Compiling and Running Are Seperate Steps&lt;/h2&gt;
&lt;p&gt;Rust is an &lt;em&gt;ahead-of-time&lt;/em&gt; compiled language.&lt;/p&gt;
&lt;h2&gt;Cargo&lt;/h2&gt;
&lt;p&gt;Cargo creats a project in a new directory and initializes a new Git repository along with a &lt;code&gt;.gitignore&lt;/code&gt;
file.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Note: Git is a common version control system. You can change cargo new to use a different version control 
system or no version control system by using the --vcs flag. Run cargo new --help to see the available 
options.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Cargo expects sources files to live inside the &lt;code&gt;src&lt;/code&gt; directory and puts the binary in a directory named
&lt;code&gt;debug&lt;/code&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;cargo new&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cargo build&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cargo run&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;cargo check&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;TOML(Tom&apos;s Obvious, Minimal Language)&lt;/h3&gt;
&lt;p&gt;It is Cargo&apos;s configuration format.&lt;/p&gt;
&lt;h3&gt;Building for Release&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;cargo build --release&lt;/code&gt; compile the project with optimizations.&lt;/p&gt;
&lt;h2&gt;Crate&lt;/h2&gt;
&lt;p&gt;A &lt;code&gt;crate&lt;/code&gt; is a collection of Rust source code files. For example, the &lt;code&gt;rand&lt;/code&gt; crate is a &lt;em&gt;library&lt;/em&gt; crate,
which contains code that is intended to be used in other programs.&lt;/p&gt;
&lt;h3&gt;external crates&lt;/h3&gt;
&lt;p&gt;The external crates are added into projects as dependencies. Cargo fetches the latest versions of everything
that dependency needs from the &lt;em&gt;registry&lt;/em&gt;, which is a copy of data from &lt;code&gt;Crates.io&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;Cargo.lock&lt;/h3&gt;
&lt;p&gt;If &lt;code&gt;Cargo.lock&lt;/code&gt; exists, Cargo will use the versions specified there rather than doing all the work of
figuring out versions again, which lets us have a reproducible build automatically.&lt;/p&gt;
&lt;h3&gt;update&lt;/h3&gt;
&lt;pre&gt;&lt;code&gt;cargo update
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;helps you to update a crate.&lt;/p&gt;
&lt;h1&gt;Common Programming Concepts&lt;/h1&gt;
&lt;h2&gt;Variables and Mutability&lt;/h2&gt;
&lt;p&gt;By default, variables in Rust are immutable.&lt;/p&gt;
&lt;p&gt;You would get compile-time errors when we attempt to change a value that is designated as immutable.&lt;/p&gt;
&lt;h3&gt;Constants&lt;/h3&gt;
&lt;p&gt;Like immutable variables, but constants:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;don&apos;t accept mut syntax.&lt;/li&gt;
&lt;li&gt;use const keyword&lt;/li&gt;
&lt;li&gt;the type of the value must be annotated&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Shadowing&lt;/h3&gt;
&lt;pre&gt;&lt;code&gt;fn main() {
    let x = 5;

    let x = x + 1;

    {
        let x = x * 2;
        println!(&quot;The value of x in the inner scope is: {x}&quot;);
    }

    println!(&quot;The value of x is: {x}&quot;);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When you run it:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ cargo run
   Compiling variables v0.1.0 (file:///projects/variables)
    Finished dev [unoptimized + debuginfo] target(s) in 0.31s
     Running `target/debug/variables`
The value of x in the inner scope is: 12
The value of x is: 6
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But shadowing:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;we create a new variable&lt;/li&gt;
&lt;li&gt;we can change the type of the value&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Data Types&lt;/h2&gt;
&lt;p&gt;Rust is a statically typed language, it must know the types of all variables at compile time.&lt;/p&gt;
&lt;h3&gt;Scalar Types&lt;/h3&gt;
&lt;p&gt;Four primary scalar types : integers, floating-point numbers, Booleans and characters.&lt;/p&gt;
&lt;h4&gt;Integer Types&lt;/h4&gt;
&lt;p&gt;Integer Literals in Rust&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Number literals	Example
Decimal	98_222
Hex	0xff
Octal	0o77
Binary	0b1111_0000
Byte (u8 only)	b&apos;A&apos;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Integer Overflow&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;In debug mode, Rust includes checks for integer overflow and cause the program to &lt;code&gt;panic&lt;/code&gt; at runtime.&lt;/li&gt;
&lt;li&gt;In release mode, Rust performs &lt;em&gt;two&apos;s complement wrapping&lt;/em&gt; for the value, and the program won&apos;t panic.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;Floating-point Types&lt;/h4&gt;
&lt;p&gt;Two primitive types : numbers with decimal points f32 and f64.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;fn main() {
    let x = 2.0; // f64

    let y: f32 = 3.0; // f32
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Boolean Type&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;true&lt;/code&gt; and &lt;code&gt;false&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;Character Type&lt;/h3&gt;
&lt;p&gt;Four bytes in size and Unicode Scalar Value.&lt;/p&gt;
&lt;h3&gt;Compound Types&lt;/h3&gt;
&lt;h4&gt;Tuple&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;a variety fo types&lt;/li&gt;
&lt;li&gt;a fixed length&lt;/li&gt;
&lt;li&gt;comma-separated list&lt;/li&gt;
&lt;li&gt;access a tuple element by using a period (.) followed by the index&lt;/li&gt;
&lt;li&gt;tuple without any values has a special name, &lt;em&gt;unit&lt;/em&gt; : an empty value ()&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code&gt;fn main() {
    let tup: (i32, f64, u8) = (500, 6.4, 1);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;fn main() {
    let tup = (500, 6.4, 1);

    let (x, y, z) = tup;

    println!(&quot;The value of y is: {y}&quot;);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;Array&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;same type&lt;/li&gt;
&lt;li&gt;a fixed lenght&lt;/li&gt;
&lt;li&gt;comma-separated list&lt;/li&gt;
&lt;li&gt;access elements by using indexing&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code&gt;fn main() {
    let a = [1, 2, 3, 4, 5];
    
    let a: [i32; 5] = [1, 2, 3, 4, 5];

    let a = [3; 5]; // equal to [3, 3, 3, 3, 3];

    let first = a[0];
    let second = a[1];

}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Arrays are useful when you want your data allocated on the stack rather than the heap.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Runtime error : invalid array element access:&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;use std::io;

fn main() {
    let a = [1, 2, 3, 4, 5];

    println!(&quot;Please enter an array index.&quot;);

    let mut index = String::new();

    io::stdin()
        .read_line(&amp;amp;mut index)
        .expect(&quot;Failed to read line&quot;);

    let index: usize = index
        .trim()
        .parse()
        .expect(&quot;Index entered was not a number&quot;);

    let element = a[index];

    println!(&quot;The value of the element at index {index} is: {element}&quot;);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The program resulted in a runtime error at the point of using an invalid value in the indexing operation.
This check has to happen at runtime, especially in this case, because the compiler can’t possibly know
what value a user will enter when they run the code later.&lt;/p&gt;
&lt;h2&gt;Functions&lt;/h2&gt;
&lt;p&gt;Functions are prevalent in Rust code.&lt;/p&gt;
&lt;p&gt;Rust code uses &lt;em&gt;snake case&lt;/em&gt; as the conventional style for function and variable names.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;fn main() {
    println!(&quot;Hello, world!&quot;);

    another_function();
}

fn another_function() {
    println!(&quot;Another function.&quot;);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In function signatures, you must declare the type of each parameter. &lt;strong&gt;Requiring type annotations in
function definitions means the compiler almost never needs you to use them elsewhere in the code to
figure out what type you mean&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;Statements and Expressions&lt;/h3&gt;
&lt;p&gt;Function bodies are made up of a series of statements optionally ending in an expression.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Statements are instructions that perform some action and do not return a value.&lt;/li&gt;
&lt;li&gt;Expressions evaluate to a resultant value.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Expressions do not include ending semicolons. If you add a semicolon to the end of an expression, you
turn it into a statement, and it will then not return a value.&lt;/p&gt;
&lt;h3&gt;Functions with Return Values&lt;/h3&gt;
&lt;p&gt;We must declare the return type after an arrow (-&amp;gt;).&lt;/p&gt;
&lt;p&gt;We need to have an expression as the return value. If not, there might be mismatched types error.&lt;/p&gt;
&lt;h2&gt;Control Flow&lt;/h2&gt;
&lt;h3&gt;if Expressions&lt;/h3&gt;
&lt;pre&gt;&lt;code&gt;if condition {

} else {

}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Boolean is expected after &lt;code&gt;if&lt;/code&gt;, Rust cannot automatically convert non-Boolean to Boolean types.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;if condition {

} else if condition {

} ... {

} else {

}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Using too many &lt;code&gt;else if&lt;/code&gt; expressions can clutter your code, so if you have more that one, you might
wnat to refactor your code. For example by &lt;code&gt;match&lt;/code&gt;.&lt;/p&gt;
&lt;h4&gt;Using if in a let Statement&lt;/h4&gt;
&lt;p&gt;Because &lt;code&gt;if&lt;/code&gt; is an expression, it could be used on the right side of a &lt;code&gt;let&lt;/code&gt; statement.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;fn main() {
    let condition = true;
    let number = if condition { 5 } else { 6 };

    println!(&quot;The value of number is: {number}&quot;);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Repetition with Loops&lt;/h3&gt;
&lt;p&gt;Loop is an expression.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;fn main() {
    loop {
        println!(&quot;again!&quot;);
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;Returning Values from Loops&lt;/h4&gt;
&lt;pre&gt;&lt;code&gt;fn main() {
    let mut counter = 0;

    let result = loop {
        counter += 1;

        if counter == 10 {
            break counter * 2;
        }
    };

    println!(&quot;The result is {result}&quot;);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;Loop Labels to Disambiguate Between Multiple Loops&lt;/h4&gt;
&lt;p&gt;We could specify a loop label so that we use with &lt;code&gt;break&lt;/code&gt; or &lt;code&gt;continue&lt;/code&gt; to specify that those keywords
apply to the labeled loop instead of the innermost loop.&lt;/p&gt;
&lt;p&gt;Note that, the &lt;code&gt;break&lt;/code&gt; keyword cannot be followed by both a return value and a loop label at the same
time. The &lt;code&gt;break&lt;/code&gt; statement is used to exit a loop or a block early, and it can be optionally followed
by an expression to provide a return value for the enclosing function or block. However, it cannot
be combined with a loop label.&lt;/p&gt;
&lt;p&gt;The labels should be in format like : &lt;code&gt;&apos;lable&lt;/code&gt;.&lt;/p&gt;
&lt;h4&gt;Conditional Loops with while&lt;/h4&gt;
&lt;pre&gt;&lt;code&gt;fn main() {
    let mut number = 3;

    while number != 0 {
        println!(&quot;{number}!&quot;);

        number -= 1;
    }

    println!(&quot;LIFTOFF!!!&quot;);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;Looping Through a Collection with for&lt;/h4&gt;
&lt;pre&gt;&lt;code&gt;fn main() {
    let a = [10, 20, 30, 40, 50];

    for element in a {
        println!(&quot;the value is: {element}&quot;);
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;fn main() {
    for number in (1..4).rev() {
        println!(&quot;{number}!&quot;);
    }
    println!(&quot;LIFTOFF!!!&quot;);
}
&lt;/code&gt;&lt;/pre&gt;
</content:encoded></item><item><title>DIS - Classification</title><link>https://tianchiyu.me/blog/2020-06-11-dis---classification/</link><guid isPermaLink="true">https://tianchiyu.me/blog/2020-06-11-dis---classification/</guid><description>Notes on classification, clustering, and decision trees in distributed information systems.</description><pubDate>Thu, 11 Jun 2020 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&amp;lt;head&amp;gt;
&amp;lt;script src=&quot;https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML&quot; type=&quot;text/javascript&quot;&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script type=&quot;text/x-mathjax-config&quot;&amp;gt;
MathJax.Hub.Config({
tex2jax: {
skipTags: [&apos;script&apos;, &apos;noscript&apos;, &apos;style&apos;, &apos;textarea&apos;, &apos;pre&apos;],
inlineMath: [[&apos;$&apos;,&apos;$&apos;]]
}
});
&amp;lt;/script&amp;gt;
&amp;lt;/head&amp;gt;&lt;/p&gt;
&lt;h1&gt;DIS - Classification&lt;/h1&gt;
&lt;h2&gt;Clustering and Classificatioin&lt;/h2&gt;
&lt;p&gt;For inferring global models of data collections there exist two types of approaches: descriptive and predictive modeling. We illustrate the difference among them by an example.&lt;/p&gt;
&lt;p&gt;We assume that a set of data items (or objects) with two attributes a1 and a2 is given. Assume the global model we are interested in is a classification (or as often said labeling) of the data items.
In descriptive modeling we just know the data items, as indicated by the points in the 2-dimensional grid. A descriptive modeling technique, such as clustering, produces classes, which are not known in advance. For doing this it relies on some criteria that specify when two data items probably belong to the same class. Such a criteria is usually based on a similarity measure.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;A predictive modeling technique, such as classification, starts from a given classification (or labeling) of data items. Using that classification of the dataset the classification method infers conditions on the properties of the data objects, that allow to predict the membership to a specific class. For example, the prediction could be based on a partitioning of the attribute values along each dimension, as shown in the figure on the right. There, first attribute a1 is partitioned into two intervals, and for each of the intervals a different partitioning of the attribute a2 is used to determine the regions corresponding to classes. Misclassifications may occur as seen in the example.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Given a dataset of objects described by attributes, build a model that assigns objects to a class (or label)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;no class info -&amp;gt; descriptive model(clustering) -&amp;gt; describe classes based on similarity of attribute values&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Given a dataset of objects described by attributes, build a model that assigns objects to a class&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;class info -&amp;gt; predictive model(classification) -&amp;gt; predict classes based on known attribute values&lt;/p&gt;
&lt;h2&gt;Classification Problem&lt;/h2&gt;
&lt;p&gt;Classification creates a &lt;strong&gt;global model&lt;/strong&gt;, that is used for predicting the class label of unknown data. Since the classification function is learned from existing data, this approach is also called a &lt;strong&gt;supervised learning approach&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Classification is clearly useful in many decision problems, where for a given data item a decision is to be made (which depends on the class to which the data item belongs). Classification is also often called &lt;strong&gt;predictive analytics&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Input: set of objects with &lt;strong&gt;categorical/numerical attributes&lt;/strong&gt; and one &lt;strong&gt;class label&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Output: A model that returns the class label given the object attributes&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Model is a function represented as rules, decision trees, formulae, neural networks&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Classification belongs to supervised ML&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Objects have class information&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Basic Approach&lt;/h2&gt;
&lt;p&gt;Model is learnt from a set of objects with known labels: &lt;strong&gt;training set&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The quality of the model is evaluated by comparing the predicted class labels with those from a set of objects with known labels: &lt;strong&gt;test set&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Test set is independent of training set, otherwise over-fitting will occur&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The model is applied to data with unknown labels: &lt;strong&gt;prediction&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;Example of Classification&lt;/h3&gt;
&lt;p&gt;&lt;/p&gt;
&lt;h4&gt;Model test and Usage&lt;/h4&gt;
&lt;p&gt;&lt;/p&gt;
&lt;h2&gt;Problem formulation&lt;/h2&gt;
&lt;p&gt;Problem:
Given a database D with n data items described by d categorical/numerical attributes and one categorical attribute (class label C)&lt;/p&gt;
&lt;p&gt;Find:(rules, decision tree,...)&lt;/p&gt;
&lt;p&gt;A function f: $X^d -&amp;gt; C$&lt;/p&gt;
&lt;p&gt;Such that:
classifies accurately the items in the training set
generalises well for the (unknown) items in the test set&lt;/p&gt;
&lt;h2&gt;Characteristics of Classification Methods&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Predictive accuracy&lt;/li&gt;
&lt;li&gt;Speed and scalability
&lt;ul&gt;
&lt;li&gt;Time to build the model(usually more expensive)&lt;/li&gt;
&lt;li&gt;Time to use the model&lt;/li&gt;
&lt;li&gt;In memory vs. on disk proceesing&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Robustness
&lt;ul&gt;
&lt;li&gt;Handling noise, outliers and missing values&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Interpretability [可解释性]
&lt;ul&gt;
&lt;li&gt;Understanding the model and its decisions(black box) vs. white box&lt;/li&gt;
&lt;li&gt;Compactness of the model [简洁性]&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As for clustering methods, we can also identify for classification methods a range of criteria to assess and compare the properties of different approaches.&lt;/p&gt;
&lt;p&gt;Predictive accuracy is the natural main objective to optimize for a classifier. It characterizes how well the classifier performs its job. Often  we encounter a trade off between predictive accuracy and the speed and scalability of the method. Methods that achieve high accuracy tend also to be more expensive. As for clustering, also for classification noise and outliers can pose additional problems affecting accuracy. Finally, a very important criterion is the interpretability of the model. In many concrete applications, it is critical that humans are able to understand based on which criteria a classifier takes a decision, e.g. for accountability. Imagine a case, where an assurance policy is refused based on the decision taken by a classifier, and the client would oppose in court. Only with  human-interpretable methods the decision could be argued for. However, the most powerful classifiers today tend to produce models that are very hard to interpret for humans, as they represent very complex functions.&lt;/p&gt;
&lt;h2&gt;Q&amp;amp;A&lt;/h2&gt;
&lt;p&gt;If a classifier has 75% accuracy, it means that ...&lt;/p&gt;
&lt;p&gt;A. It correctly classifies 75% of the data items in the training set&lt;/p&gt;
&lt;p&gt;B. It correctly classifies 100% of the data items in the training set but only 75% in the test set&lt;/p&gt;
&lt;p&gt;C. It correctly classifies 75% of the data items in the test set&lt;/p&gt;
&lt;p&gt;D. It correctly classifies 75% of the unknown data items&lt;/p&gt;
&lt;p&gt;Answer: [&amp;lt;font color: white&amp;gt; C &amp;lt;/font&amp;gt;]&lt;/p&gt;
&lt;h2&gt;Decision Trees&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Nodes&lt;/strong&gt; are tests on a single attribute&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Branches&lt;/strong&gt; are attribute values&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Leaves&lt;/strong&gt; are marked with class labels&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;A standard type of classification function is a decision tree. In a basic decision tree, &lt;strong&gt;at each level one of the available attributes is used to partition the data set based on the different attribute values&lt;/strong&gt;. At the leaf level of the decision tree, the values of the class label attribute are found. Thus, for a given data item with unknown class label attribute, by traversing the tree from the root to the leaf according to its data values, its class can be determined by choosing the class label found at the leaf level. Note that in different branches of the tree, different attributes may be used for classification.&lt;/p&gt;
&lt;p&gt;A decision tree is constructed in a top-down manner, by recursively splitting the training set using conditions on the attributes. How these conditions are determined is one of the key questions for decision tree induction. After the decision tree construction, it may occur that at the leaf level the granularity is too fine, i.e., many leaves correspond to outliers in the data. Thus, in a second phase such leaves are identified and eliminated.&lt;/p&gt;
&lt;p&gt;The key problem in constructing a decision tree is thus to determine the attributes that are used to partition the data set at each level of the decision tree.&lt;/p&gt;
&lt;h3&gt;Decision Tree Induction: Algorithm&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Tree construction (top-down divide-and-conquer strategy)&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;At the beginning, all training samples belong to the root&lt;/li&gt;
&lt;li&gt;Examples are partitioned recursively based on a selected “most discriminative” attribute&lt;/li&gt;
&lt;li&gt;Discriminative power determined based on &lt;strong&gt;information gain (ID3/C4.5)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Partitioning stops if&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;All samples belong to the same class → assign the class label to the leaf&lt;/li&gt;
&lt;li&gt;There are no attributes left → majority voting to assign the class label to the leaf&lt;/li&gt;
&lt;li&gt;There are no samples left&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The basic algorithm for decision tree induction proceeds in a greedy manner. First the set of all data objects are associated with the root. Among all attributes one is chosen to partition the set. The criterion that is applied to select the attribute is based on measuring the &lt;strong&gt;information gain&lt;/strong&gt; that can be achieved, or how much uncertainty on the classification of the data objects is removed by the partitioning.&lt;/p&gt;
&lt;p&gt;Three conditions can occur such that no further partitions can be performed:&lt;/p&gt;
&lt;p&gt;(1) all data objects are in the same class, therefore further splitting makes no sense,&lt;/p&gt;
&lt;p&gt;(2) no attributes are left which can be used to split. Still data objects from different classes can be in the leaf, then majority voting is applied.&lt;/p&gt;
&lt;p&gt;(3) no data objects are left.&lt;/p&gt;
&lt;h3&gt;Example&lt;/h3&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Based on this approach for attribute selection, we can now illustrate the induction of the decision tree. In a first step, age is chosen for a split. The partition 31..40 contains after the split only instances from one class, the positive class, thus for this branch of the tree the induction terminates.&lt;/p&gt;
&lt;p&gt;For the partition &amp;lt;= 30 we find that the student attribute is the best to be chosen for further splitting. Further splitting makes no more sense, as the two resulting partitions, after splitting by the student attribute, are consisting of either positive or negative instances only.&lt;/p&gt;
&lt;p&gt;Similarly, for the partition &amp;gt;40 we find that credit rating gives the largest information gain. As before, further splitting is no more needed, as the resulting partitions contain only positive respectively negative instances.&lt;/p&gt;
&lt;h2&gt;Attribute Selection&lt;/h2&gt;
&lt;h3&gt;Entropy&lt;/h3&gt;
&lt;p&gt;At a given branch in the tree, the set of samples S to be classified has P positive and N negative instances&lt;/p&gt;
&lt;p&gt;The entropy of the set S is&lt;/p&gt;
&lt;p&gt;$$
H(P,N) = -\frac{P}{P+N}log_2\frac{P}{P+N} - \frac{N}{P+N}log_2\frac{N}{P+N}
$$&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Note:&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If P = 0 or N = 0, then H(P,N) = 0 -&amp;gt; no uncertainty&lt;/p&gt;
&lt;p&gt;If P = N, then H(P,N) = 1 -&amp;gt; maximal uncertainty&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The approach is based on an information-theoretic argument. Assuming that we have a binary category, i.e., two classes P and N to which objects in S have to be assigned, we can compute the amount of information required to determine the class, by H(P, N), the standard entropy measure, where P and N denote the cardinalities of the classes P and N. Given an attribute A that can be used for partitioning the data collection, we can calculate the amount of information needed to classify the data after the split according to attribute A has been performed. This value is obtained by calculating H(P, N) for each of the partitions and weighting these values by the probability that a data item belongs to the respective partition.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h4&gt;Example&lt;/h4&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;We illustrate the attribute selection process now for our running example. Initially the data contains P = 9 positive instances and N = 5 negative instances. This results in an entropy of 0.94, i.e. 0.94 bits are required to decide the class of one instance. We compute next the entropies of all partitions that result from splitting all attributes. For example, if we split for Age, we obtain 3 partitions, each with a different distribution of positive and negative instances; and thus with different entropies.&lt;/p&gt;
&lt;h3&gt;Information Gain&lt;/h3&gt;
&lt;p&gt;The information gained by a split can thus be determined as the difference of the amount of information needed for correct classification before and after the split. Thus we calculate the reduction in uncertainty that is obtained by splitting according to attribute A and select among all possible attributes the one that leads to the highest reduction. For our example we can conclude that it is best to split on attribute age.&lt;/p&gt;
&lt;p&gt;Next we compute the weighted sum of all entropies of the partitions in a split. The weights correspond to the probability of an instance falling into an element of the partition. Computing this for all attributes shows, that the attribute H results in the lowest entropy, i.e., leaves the lowest remaining uncertainty about the class membership of instances after the split.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Computation&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Attribute A partitions S into $S_1, S_2, ... S_v$
Entropy of attribute A is
$$
H(A) = \sum_{i=1}^v\frac{P_i+N_i}{P+N}H(P_i,N_i)
$$&lt;/p&gt;
&lt;p&gt;The information gain obtained by splitting S using A is
$$
Gain(A) = H(P,N) - H(A)
$$
Choose the biggest information gain as the splited one.&lt;/p&gt;
&lt;h3&gt;Q &amp;amp; A&lt;/h3&gt;
&lt;p&gt;Given the distribution of positive and negative samples for attributes A1 and A2, which is the best attribute for splitting?&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;A1&lt;/th&gt;
&lt;th&gt;P&lt;/th&gt;
&lt;th&gt;N&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;a&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;b&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;A2&lt;/th&gt;
&lt;th&gt;P&lt;/th&gt;
&lt;th&gt;N&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;x&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;y&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Answer:[&amp;lt;font color:white&amp;gt;A1&amp;lt;/font&amp;gt;]&lt;/p&gt;
&lt;h2&gt;Pruning [修剪]&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;A common problem in classification is that a classifier may &lt;strong&gt;overspecialize and capture noise and outliers in the data&lt;/strong&gt;[噪声和离群值对数据的影响], rather than &lt;strong&gt;general properties&lt;/strong&gt;. One possibility to limit overspecialization would be to &lt;strong&gt;stop the partitioning of tree nodes when some specific criteria is met&lt;/strong&gt; (e.g., number of samples assigned to the leaf node). A possible criterion is to &lt;strong&gt;stop partitioning when the majority of remaining samples falls into one class&lt;/strong&gt;. However, in general it is difficult to specify a suitable criterion a priori (e.g. choosing the right value of epsilon).&lt;/p&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;Another alternative is to &lt;strong&gt;first build the complete classification tree&lt;/strong&gt;, and then, in a second phase, &lt;strong&gt;prune subtrees that do not contribute to an efficient classification&lt;/strong&gt;. Different approaches can be applied to that end: &lt;strong&gt;heuristic approaches[启发式方法] can identify subtrees that do not contribute to the classification accuracy, and eliminate those&lt;/strong&gt;. A more principled approach is &lt;strong&gt;the use of the minimum description length principle&lt;/strong&gt;. (MDL).&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The construction phase does not filter out noise → &lt;strong&gt;overfitting&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Pruning strategies&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Stop partitioning a node when large majority of samples is positive or negative, i.e., 𝑁/(𝑁+𝑃) or 𝑃/(𝑁+𝑃)&amp;gt;1−𝜀&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Build the full tree, then replace nodes with leaves labelled with the majority class, if classification accuracy does not change&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Apply Minimum Description Length (MDL) principle&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;分类算法：
https://blog.csdn.net/china1000/article/details/48597469&lt;/p&gt;
&lt;h3&gt;Minimum Description Length Pruning&lt;/h3&gt;
&lt;p&gt;The MDL is based on the following consideration: if the effort in order to specify a class (the implicit description of the class extension through a decision tree) exceeds the effort to enumerate all class members (the explicit description of the class by enumerating its extension), then the subtree is over classifying and non-optimal.  To measure the description cost a suitable metrics for the encoding cost, both for trees and data sets is required. For trees this can be done by suitably counting the various structural elements needed to encode the tree (#nodes, #test predicates, # arcs), whereas for explicit classification, it is sufficient to count the number of misclassifications that occur in a tree node.&lt;/p&gt;
&lt;p&gt;补充：
剪枝的目的是为了通过删除部分节点和子树，以避免“过度合适”/“过学习”，因为过学习将导致我们作出的假设的泛化能力过差。最小描述长度（MDL）准则表示，解释一组数据最好的理论，应该使下面这两项之和最下：&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;描述理论所需要的比特长度；&lt;/li&gt;
&lt;li&gt;在理论的协助下，对数据编码所需要的比特长度。&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;而决策树中的MDL应当是使得训练样本的大多数数据符合这棵树，其他的样本作为例外编码，使得下面这两项最小：&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;编码决策树所需要的比特，它代表了猜想&lt;/li&gt;
&lt;li&gt;编码例外实例所需要的比特&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Let M1, M2, ..., Mn be a list of candidate models (i.e., trees). The best model is the one that minimizes
$$
L(M) + L(D|M)
$$
where&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;L(M) is the length of the description of the model in bits (#nodes, #leaves, #arcs ...)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;L(D|M) is the is the length of the description of the data when encoded with the model in bits (#misclassifications)&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Continuous Attributes&lt;/h2&gt;
&lt;p&gt;With continuous attributes it does not make sense to create a separate path in the decision tree for every possible attribute value. Instead, in such a case, a binary decision tree is constructed. Binary decisions can be specified both for continuous and categorical attributes. For continuous attributes, the binary split is performed by selecting a threshold that separates the instances in those that have a larger and a smaller value than the threshold. For categorical attributes, a subset of attribute values can be chosen that distinguishes the instances in two subsets.&lt;/p&gt;
&lt;p&gt;With continuous attributes we cannot have a separate branch for each value&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;use binary decision trees&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Binary decision trees&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;For continuous attributes A a split is defined by val(A) &amp;lt; X&lt;/li&gt;
&lt;li&gt;For categorical attributes A a split is defined by a subset X  $\subseteq$ domain(A)&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Example&lt;/h3&gt;
&lt;p&gt;This example shows a dataset with both categorical and continuous attributes and a possible binary decision tree for such a dataset. The class label in the example is Risk.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;h3&gt;Splitting Continuous Attributes&lt;/h3&gt;
&lt;p&gt;Approach&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Sort the data according to attribute value&lt;/li&gt;
&lt;li&gt;Determine the value of X which maximizes information gain by scanning through the data items&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Only if the class label changes, a relevant decision point exists&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;When splitting the dataset using a continuous attribute, we need to determine which is the optimal value to split the dataset based on this attribute. To that end, first the set of attribute values is sorted. Then the class labels are traversed, and whenever it changes a possible split point is found (it can be shown that splitting where class labels do not change is provably sub-optimal). At these points the information gain needs to be computed.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h4&gt;Scalability of Continuous Attributes&lt;/h4&gt;
&lt;p&gt;Naive implementation&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;At each step the data set is split in subsets that are associated with a tree node&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Problem&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;For evaluating which continuous attribute to split, data needs to be sorted according to these attributes&lt;/li&gt;
&lt;li&gt;Becomes dominating cost&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In a naïve implementation of the splitting process, we would keep all data in a single table. This would imply that we would have for traversing the attributes in order to resort that table every time an attribute is investigated. Therefore, a more efficient approach is needed.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;To avoid repeated resorting of data, for every attribute a separate and presorted table is kept. Once a split is chosen, we find two different situation. For the table that keeps the attribute that was used in the split, the table needs just to be partitioned into two subtables, maintaining the order. For the other attributes we have to select the subtables corresponding to the instances of the two partitions that have been formed. To that end a temporary hash table is constructed that allows to associate to each dataitem its partition. Then the attribute table is scanned and partitioned using the hashtable to decide for each entry to which partition it belongs. Note that in this approach, for continuous attributes, the resulting tables are again sorted as the order is preserved from the original table.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;Idea: Presorting of data and maintaining order throughout tree construction&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Requires separate sorted attribute tables for each attribute&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Updating attribute tables&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Attribute used for split: splitting attribute table straightforward&lt;/li&gt;
&lt;li&gt;Other attributes
&lt;ul&gt;
&lt;li&gt;Build Hash Table once associating tuple identifiers (TIDs) of data items with partitions&lt;/li&gt;
&lt;li&gt;Select data from other attribute tables by scanning and probing the hash table&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Example&lt;/h3&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;In this example we demonstrate of how attribute tables are split, when a decision node is introduced in the decision tree. Since the split is based on attribute Age, the table for Age can simply be split into two subtables at the threshold value. For the Car Type table we use the temporary hash table indicating partition membership to separate it into two subtables.&lt;/p&gt;
&lt;h3&gt;Q &amp;amp; A&lt;/h3&gt;
&lt;p&gt;When splitting a continuous attribute, its values need to be sorted …&lt;/p&gt;
&lt;p&gt;A. to avoid overfitting&lt;/p&gt;
&lt;p&gt;B. to prune the data&lt;/p&gt;
&lt;p&gt;C. to define a binary split condition&lt;/p&gt;
&lt;p&gt;D. to accelerate tree induction&lt;/p&gt;
&lt;p&gt;Answer:[&amp;lt;font color:white&amp;gt;C&amp;lt;/font&amp;gt;]&lt;/p&gt;
&lt;h2&gt;Characteristics of Decision Tree Induction&lt;/h2&gt;
&lt;p&gt;We summarize here some of the major strengths and weaknesses of standard decision tree induction.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Decision trees advantages&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The information theoretic criteria used to select the most discriminative attribute is an embedded feature selection&lt;/li&gt;
&lt;li&gt;No data preparation is needed, such as normalisation of data&lt;/li&gt;
&lt;li&gt;The best aspect of using trees for analytics is that they are easy to interpret and explain, while more sophisticated ML algorithms (ANN, SVM) are seen as black-boxes that do not “explain” the classification decisions they make&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Decision trees drawbacks&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;They can be extremely sensitive to small perturbations in the data: a slight change can result in a drastically different tree.&lt;/li&gt;
&lt;li&gt;They can easily overfit. This can be compensated by validation methods and pruning, but remains a problem.&lt;/li&gt;
&lt;li&gt;They are not incremental. If new data is available, the existing tree cannot be incrementally modified, but the whole tree must be reconstructed from scratch&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;p&gt;Strengths&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Automatic feature selection&lt;/li&gt;
&lt;li&gt;Minimal data preparation&lt;/li&gt;
&lt;li&gt;Non-linear model&lt;/li&gt;
&lt;li&gt;Easy to interpret and explain&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Weaknesses&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Sensitive to small perturbation in the data&lt;/li&gt;
&lt;li&gt;Tend to overfit&lt;/li&gt;
&lt;li&gt;No incremental updates&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;h3&gt;Decision Tree Induction: Properties&lt;/h3&gt;
&lt;p&gt;Model: flow-chart like tree structure&lt;/p&gt;
&lt;p&gt;Score function: classification accuracy&lt;/p&gt;
&lt;p&gt;Optimisation: top-down tree construction + pruning&lt;/p&gt;
&lt;p&gt;Data Management: avoiding sorting during splits&lt;/p&gt;
&lt;h2&gt;Classification Algorithms&lt;/h2&gt;
&lt;p&gt;Decision trees is one of the best known and historically first examples of a classification approach. Many other methods have been devised in studied over tie. These include basic methods (we will see some examples later), ensemble methods (discussed in the following), support vector machines (a paradigm based on splitting the space through hyper-planes), and neural networks (which are attracting recently significant attention and are nowadays among the best performing classifiers if very large training sets are available).&lt;/p&gt;
&lt;p&gt;Decision tree induction is a (well-known) example of a classification algorithm&lt;/p&gt;
&lt;p&gt;Alternatives&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Basic methods: Naïve Bayes, kNN, logistic regression, ..&lt;/li&gt;
&lt;li&gt;Ensemble methods: random forest, gradient boosting, …&lt;/li&gt;
&lt;li&gt;Support vector machines&lt;/li&gt;
&lt;li&gt;Neural networks: CNN, rNN, LSTM, …&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Ensemble Methods&lt;/h3&gt;
&lt;p&gt;One important development in decision trees was the introduction of the idea of ensemble methods. The basic principle is simple: instead of constructing a single model, many different models are constructed independently. Even if each model is not very expressive (weak learners) their combination can be powerful (strong learner). Different ensemble methods are distinguished by the type of approach they are based on. Ensemble methods, which we will discuss in the following, learn several models in parallel, and combine then their predictions by voting or averaging. Stacking methods use more sophisticated techniques to combine model outputs, based themselves on learning methods. Finally, boosting learn models in sequence. In each step the samples of the training data are reweighted depending on whether they have been correctly classified.&lt;/p&gt;
&lt;p&gt;Idea&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Take a collection of simple or weak learners&lt;/li&gt;
&lt;li&gt;Combine their results to make a single, strong learner
Types&lt;/li&gt;
&lt;li&gt;Bagging: train learners in parallel on different samples of the data, then combine outputs through voting or averaging&lt;/li&gt;
&lt;li&gt;Stacking: combine model outputs using a second-stage learner like linear regression[线性回归]&lt;/li&gt;
&lt;li&gt;Boosting: train learners on the filtered output of other learners&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Random Forests: Algorithm&lt;/h3&gt;
&lt;p&gt;Random forests are an ensemble method based on bagging. The principle is very simple: K different decision trees are learnt in parallel from different (independent) samples of the data, and the classification is derived from a majority vote of the predictions.&lt;/p&gt;
&lt;p&gt;Learn K different decision trees from independent samples of the data (bagging); vote between different learners, so models should not be too similar&lt;/p&gt;
&lt;p&gt;Aggregate output: majority vote&lt;/p&gt;
&lt;h4&gt;Why do Ensemble Methods Work?&lt;/h4&gt;
&lt;p&gt;Here we give the argument why ensemble methods work: even if the individual classifiers are not very good (e.g. make 35% errors in prediction) their aggregate will be very strong. For example, if we have 25 classifiers, the probability that a majority of them, namely at least 13, make a wrong prediction is very small, namely 6%. In general, ensemble methods work well, if the individual models are better than random guessing. The figure illustrates the relation between the classification errors of individual classifiers and the aggregate classification accuracy.&lt;/p&gt;
&lt;h4&gt;Sampling Strategies&lt;/h4&gt;
&lt;p&gt;Two sampling strategies&lt;/p&gt;
&lt;p&gt;Sampling data&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;select a subset of the data → Each tree is trained on different data&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Sampling attributes&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;select a subset of attributes → corresponding nodes in different trees (usually) don’t use the same feature to split&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For random forests the main issue is the choice of the sampling strategy, i.e., the generation of samples that are used for learning the individual models. Specifically, it consists of two different sampling strategy. The first, sampling data selects from the original dataset a sample.  Thus each decision tree is trained on a different sample of data. The second, sampling attributes selects from the attributes available to take a decision a random subset. Thus even if a tree would have been constructed in the same way up to a level, the continuation might become different due to attribute sampling (e.g. the optimal attribute in one tree is not available for splitting in the other tree).&lt;/p&gt;
&lt;h4&gt;Algorithm context&lt;/h4&gt;
&lt;ol&gt;
&lt;li&gt;Draw K bootstrap samples of size N from original dataset, with replacement (bootstrapping)&lt;/li&gt;
&lt;li&gt;While constructing the decision tree, select a random set of m attributes out of the p attributes available to infer split (feature bagging)&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Typical parameters&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;m ≈ sqrt(p), or smaller&lt;/li&gt;
&lt;li&gt;K ≈ 500&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;Illustration of Random Forests&lt;/h4&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Random forests allow to learn much more complex functions than basic decision trees. This fact is illustrated in this visualization. For the same training data set different numbers of decision trees are constructed (rCART is a variant of decision trees). We observe that with increasing numbers of trees the decision boundaries become increasingly more complex and smoother, and thus a better separation among different classes can be achieved.&lt;/p&gt;
&lt;h3&gt;Q &amp;amp; A&lt;/h3&gt;
&lt;p&gt;The computational cost for constructing a RF with K as compared to constructing K decision trees on the same data&lt;/p&gt;
&lt;p&gt;A. is identical&lt;/p&gt;
&lt;p&gt;B. is on average larger&lt;/p&gt;
&lt;p&gt;C. is on average smaller&lt;/p&gt;
&lt;p&gt;Answer:[&amp;lt;font color:white&amp;gt;C&amp;lt;/font&amp;gt;]&lt;/p&gt;
&lt;h3&gt;Characteristics of Random Forests&lt;/h3&gt;
&lt;p&gt;Random forests are a very popular method for classification due to the many advantages they offer. They are considered as the method of choice in cases where the data is dense, which means that the number of features is relatively low (in the thousands). Sparse data would be, for example, vector space representation of documents with very large vocabularies. In such cases, before applying a method such as random forests, a dimensionality reduction would have to be applied. This could be accomplished in the case of documents by creating a word embedding.&lt;/p&gt;
&lt;p&gt;Strengths&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Ensembles can model extremely complex decision boundaries &lt;strong&gt;without overfitting&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Probably the most popular classifier for &lt;strong&gt;dense data&lt;/strong&gt; (≤ a few thousand features)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Easy to implement&lt;/strong&gt; (train a lot of trees)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Parallelizes easily, good match for MapReduce&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;More recently, in cases where large training sets are available or number of features is very large, deep neural networks exhibit better performance than random forests.&lt;/p&gt;
&lt;p&gt;Weaknesses&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Deep Neural Networks generally do better&lt;/li&gt;
&lt;li&gt;Needs many passes over the data – at least the max depth of the trees&lt;/li&gt;
&lt;li&gt;Relatively easy to overfit – hard to balance accuracy/fit tradeoff&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;references&lt;/h2&gt;
&lt;p&gt;Textbook&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Jiawei Han, Data Mining: concepts and techniques, Morgan Kaufman, 2000, ISBN 1-55860-489-8
References&lt;/li&gt;
&lt;li&gt;Leo Breiman (2001) “Random Forests” Machine Learning, 45, 5-32.&lt;/li&gt;
&lt;li&gt;Shafer, John, Rakesh Agrawal, and Manish Mehta. &quot;SPRINT: A scalable parallel classifier for data mining.&quot; Proc. 1996 Int. Conf. Very Large Data Bases. 1996.&lt;/li&gt;
&lt;/ul&gt;
</content:encoded></item><item><title>Software Security 1 (Basic Principles)</title><link>https://tianchiyu.me/blog/2020-02-19-software-security-1-basic-principles/</link><guid isPermaLink="true">https://tianchiyu.me/blog/2020-02-19-software-security-1-basic-principles/</guid><description>Course notes on software security principles, threats, attacks, and defenses.</description><pubDate>Wed, 19 Feb 2020 00:00:00 GMT</pubDate><content:encoded>&lt;h1&gt;Software Security 1 (Basic Principles)&lt;/h1&gt;
&lt;p&gt;&lt;strong&gt;Mathias Payer&lt;/strong&gt; EPFL Spring 2020&lt;/p&gt;
&lt;p&gt;Allow intended use of software, prevent unintended use that may cause harm.&lt;/p&gt;
&lt;h2&gt;Security principles / CIA security triad&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Confidentiality&lt;/strong&gt;: an attacker cannot recover protected data&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Integrity&lt;/strong&gt;: an attacker cannot modify protected data&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Availability&lt;/strong&gt;: an attacker cannot stop/hinder computation&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Accountability/non-repudiation may be used as fourth fundamental concept. It prevents denial of message transmission or receipt.&lt;/p&gt;
&lt;h2&gt;Attacks and Defenses&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Attack (threat) models
&lt;ul&gt;
&lt;li&gt;A class of attacks that you want to stop&lt;/li&gt;
&lt;li&gt;What is the attacker’s capability?&lt;/li&gt;
&lt;li&gt;What is impact of an attack?&lt;/li&gt;
&lt;li&gt;What attacks are out-of-scope?&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Defenses address a certain attack/threat model
&lt;ul&gt;
&lt;li&gt;General: e.g., stop memory corruptions&lt;/li&gt;
&lt;li&gt;Very specific: e.g., stop overwriting a return address&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Threat model&lt;/h3&gt;
&lt;p&gt;The threat model defines the abilities and resources of the attacker. Threat models enable structured reasoning about the attack surface.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Awareness of entry points (and associated threats)&lt;/li&gt;
&lt;li&gt;Look at systems from an attacker’s perspective
&lt;ul&gt;
&lt;li&gt;Decompose application: identify structure&lt;/li&gt;
&lt;li&gt;Determine and rank threats&lt;/li&gt;
&lt;li&gt;Determine counter measures and mitigation&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Further reading: https://www.owasp.org/index.php/Application_Threat_Modeling&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Threat model for a safe/lockbox&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;You want to protect your valuables by locking them in a safe.&lt;/p&gt;
&lt;p&gt;In trust land, you don’t need to lock your safe.
An attacker may pick your lock.
An attacker may use a torch to open your safe.
An attacker may use advanced technology (x-ray) to open it.
An attacker may get access (or copy) your key.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Threat model: operating systems&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Malicious extension【恶意扩展】: attacker-controlled driver injected in OS;在操作系统中加入攻击者控制的驱动程序\
Bootkit: compromise the boot process (BIOS, boot sectors)危及boot进程;\
Memory corruption: software bugs such as spatial and temporal memory safety errors or hardware bugs such as rowhammer;\
Data leakage【数据泄露】: the OS accidentally returns confidential data
(e.g., randomization secrets);\
Concurrency bugs【并发问题】: unsynchronized【不同步】 reads across privilege levels result in TOCTTOU (time of check to time of use【检查时间到使用时间】) bugs;\
Side channels: indirect data leaks through shared resources such as hardware (e.g., caches), speculation (Spectre or Meltdown), or software (page deduplication);\
Malicious peripherals【恶意外围设备】: an attacker may connect malicious peripherals to an exposed bus;\
Resource depletion and deadlocks: stop legitimate computation【合法计算】 by exhausting【耗尽】 or blocking【锁死】 access to resources&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Cost of security&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There is no free lunch, security incurs overhead.
Security is. . .
expensive to develop,
expensive to maintain,
may have performance overhead,
may be inconvenient to users.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Fundamental security mechanisms&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;ol&gt;
&lt;li&gt;Isolation&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Isolate two components from each other. One component cannot access data/code of the other component except through a well-defined API.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Least privilege&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The principle of least privilege ensures that a component has the least privileges needed to function.&lt;/p&gt;
&lt;p&gt;最小特权原则是系统安全中最基本的原则之一。所谓最小特权(Least Privilege)，指的是&quot;在完成某种操作时所赋予网络中每个主体(用户或进程)必不可少的特权&quot;。最小特权原则，则是指&quot;应限定网络中每个主体所必须的最小特权，确保可能的事故、错误、网络部件的篡改等原因造成的损失最小&quot;。&lt;/p&gt;
&lt;p&gt;Any further removed privilege reduces functionality
Any added privilege will not increase functionality (according to the specification)
This property constrains an attacker in the obtainable privileges&lt;/p&gt;
&lt;p&gt;最小特权原则一方面给予主体&quot;必不可少&quot;的特权，这就保证了所有的主体都能在所赋予的特权之下完成所需要完成的任务或操作;另一方面，它只给予主体&quot;必不可少&quot;的特权，这就限制了每个主体所能进行的操作。
最小特权原则要求每个用户和程序在操作时应当使用尽可能少的特权，而角色允许主体以参与某特定工作所需要的最小特权去签入(Sign)系统。被授权拥有强力角色(Powerful Roles)的主体，不需要动辄运用到其所有的特权，只有在那些特权有实际需求时，主体才去运用它们。如此一来，将可减少由于不注意的错误或是侵入者假装合法主体所造成的损坏发生，限制了事故、错误或攻击带来的危害。它还减少了特权程序之间潜在的相互作用，从而使对特权无意的、没必要的或不适当的使用不太可能发生。这种想法还可以引申到程序内部：只有程序中需要那些特权的最小部分才拥有特权。&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Fault compartments 【故障舱】&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Separate individual components into smallest functional entity possible. These units contain faults to individual components. Allows abstraction and permission checks at boundaries.&lt;/p&gt;
&lt;p&gt;Note that this property builds on least privilege and isolation. Both properties are most effective in combination: many small components that are running and interacting with least privileges.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Example: mail server&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Mail Transfer Agents (MTA)&lt;/strong&gt; need to do a plethora of tasks:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Send/receive data from the network&lt;/p&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;Manage a pool of received/unsent messages&lt;/p&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;Provide access to stored messages for each user&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;-&amp;gt; Two approaches: sendmail and qmail&lt;/p&gt;
&lt;p&gt;Sendmail uses a typical Unix approach with a large monolithic【庞大的】 server and is known for the high complexity and previous security vulnerabilities【脆弱点】.&lt;/p&gt;
&lt;p&gt;QMail uses a modern least privilege approach with a set of
communicating processes.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;QMail&lt;/strong&gt; [详见ppt]&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;qmail is a mail transfer agent (MTA) that runs on Unix. It was written, starting December 1995, by Daniel J. Bernstein as a more secure replacement for the popular Sendmail program. Originally license-free software, qmail&apos;s source code was later dedicated in the public domain by the author.
When first published, qmail was the first security-aware mail transport agent; since then, other security-aware MTAs have been published. The most popular predecessor to qmail, Sendmail, was not designed with security as a goal, and as a result has been a perennial target for attackers. In contrast to sendmail, qmail has a modular architecture composed of mutually untrusting components; for instance, the SMTP listener component of qmail runs with different credentials from the queue manager or the SMTP sender. qmail was also implemented with a security-aware replacement to the C standard library, and as a result has not been vulnerable to stack and heap overflows, format string attacks, or temporary file race conditions.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Separate modules run under separate user IDs &lt;strong&gt;(isolation)&lt;/strong&gt;
Each user ID has only limited access to a subset of the resources &lt;strong&gt;(least privilege)&lt;/strong&gt;
Only one very small component runs as suid root
Only one very small component running as root&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;SUID (Set owner User ID up on execution) is a special type of file permissions given to a file. Normally in Linux/Unix when a program runs, it inherit’s access permissions from the logged in user. SUID is defined as giving temporary permissions to a user to run a program/file with the permissions of the file owner rather that the user who runs it. In simple words users will get file owner’s permissions as well as owner UID and GID when executing a file/program/command.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;ol&gt;
&lt;li&gt;Trust and correctness&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Specific components are assumed to be trusted or correct according to a specification&lt;/p&gt;
&lt;p&gt;Formal verification ensures that a component correctly implements a given specification and can therefore be trusted. &lt;strong&gt;Note that this property is an ideal property that cannot generally be achieved.&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;Hardware and software abstractions&lt;/h2&gt;
&lt;h3&gt;Operating System (OS) abstractions&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Provides process abstraction&lt;/li&gt;
&lt;li&gt;Well-defined API to access hardware resources&lt;/li&gt;
&lt;li&gt;Enforces mutual exclusion to resources&lt;/li&gt;
&lt;li&gt;Enforces access permissions for resources&lt;/li&gt;
&lt;li&gt;Restrictions based on user/group/ACL&lt;/li&gt;
&lt;li&gt;Restricts attacker&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;OS process isolation:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Memory protection :protect the memory (code and data such as heap, stack, or globals) of one process from other processes;&lt;/li&gt;
&lt;li&gt;Address space: working memory of one process&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Today’s system implement address spaces (virtual memory) through page tables with the help of an Memory Management Unit (MMU)&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Single domain OS (Hardware -&amp;gt; Application)&lt;/strong&gt;
&lt;ol&gt;
&lt;li&gt;A single layer, no isolation or compartmentalization&lt;/li&gt;
&lt;li&gt;All code runs in the same domain: the application can directly call into operating system drivers&lt;/li&gt;
&lt;li&gt;High performance, often used in embedded systems&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Monolithic OS(Hardware -&amp;gt;  -&amp;gt;several applications)&lt;/strong&gt;
&lt;ol&gt;
&lt;li&gt;Two layers: the operating system and applications&lt;/li&gt;
&lt;li&gt;The OS manages resources and orchestrates access&lt;/li&gt;
&lt;li&gt;Applications are unprivileged, must request access from the OS&lt;/li&gt;
&lt;li&gt;Linux fully and Windows mostly follows this approach for performance (isolating individual components is expensive)&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Micro-kernel&lt;/strong&gt;
&lt;ol&gt;
&lt;li&gt;Many layers: each component is a separate process&lt;/li&gt;
&lt;li&gt;Only essential parts are privileged
&lt;ol&gt;
&lt;li&gt;Process abstraction (address spaces)&lt;/li&gt;
&lt;li&gt;Process management (scheduling)&lt;/li&gt;
&lt;li&gt;Process communication (IPC)&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;Applications request access from different OS processes&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Library OS&lt;/strong&gt;
&lt;ol&gt;
&lt;li&gt;Few thin layers; flat structure&lt;/li&gt;
&lt;li&gt;Micro-kernel exposes bare OS services&lt;/li&gt;
&lt;li&gt;Each application brings all necessary OS components&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;Hardware abstractions&lt;/h3&gt;
&lt;p&gt;Virtual memory through MMU/OS&lt;/p&gt;
&lt;p&gt;Only OS has access to raw physical memory&lt;/p&gt;
&lt;p&gt;DMA for trusted devices&lt;/p&gt;
&lt;p&gt;ISA enforces privilege abstraction (ring 0/3 on x86)&lt;/p&gt;
&lt;p&gt;Hardware abstractions are fundamental for performance&lt;/p&gt;
&lt;h2&gt;Access control&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Authentication【证明】: Who are you (what you know, have, or are)?&lt;/li&gt;
&lt;li&gt;Authorization【授权】: Who has access to object?&lt;/li&gt;
&lt;li&gt;Audit/Provenance【审查，来源】: I’ll check what you did.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Authentication&lt;/h3&gt;
&lt;p&gt;There are three fundamental types of identification:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;What you know: username / password&lt;/li&gt;
&lt;li&gt;What you are: biometrics&lt;/li&gt;
&lt;li&gt;What you have: smartcard / phone&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Identification is stronger in combination:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;two factors are better than one&lt;/li&gt;
&lt;li&gt;Check for username/password and presence of a security token&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;Authorization&lt;/h3&gt;
&lt;p&gt;—— Information Flow Control (IFC)&lt;/p&gt;
&lt;p&gt;Who can access what information?&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Access policies are called &lt;strong&gt;access control models&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Access control models originally developed by the US military
&lt;ul&gt;
&lt;li&gt;Users with different clearance levels【许可级别】 on a single system&lt;/li&gt;
&lt;li&gt;Data was shared across different levels of clearance&lt;/li&gt;
&lt;li&gt;Therefore the name “multi-level security”&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;—— Types of access control&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Mandatory Access Control (MAC) 【强制/命令访问控制】
&lt;strong&gt;Idea: rule and lattice-based policy&lt;/strong&gt;\
&lt;ul&gt;
&lt;li&gt;Centrally controlled&lt;/li&gt;
&lt;li&gt;One entity controls what permissions are given&lt;/li&gt;
&lt;li&gt;Users cannot change policy themselves&lt;/li&gt;
&lt;/ul&gt;
&lt;blockquote&gt;
&lt;p&gt;Examples: Bell/LaPadula and Biba\&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Bell-LaPadula模型 (BLP)&lt;/strong&gt; 是一种状态机模型，用于在政府和军事应用中实施访问控制。它是由David Elliott Bell 和Leonard J. LaPadula在Roger R. Schell的指导下设计的，用于规范美国国防部 (DoD) 的多级安全 (MLS) 策略。该模型是计算机安全策略的形式状态转换模型，它利用访问主体和访问对象的安全等级来描述一系列访问控制规则。安全等级的范围从最敏感（如“最高机密”）到最不敏感（如“未分类”或“公开”）。Bell-LaPadula模型是保护与安全之间没有明显区别的模型之一。\
在存在受信任主体的情况下，Bell-LaPadula模型可能会产生从高机密文档到低机密文档的信息流动。受信任主体不受*属性的限制，但必须证明其在安全策略方面是值得信任的。该安全模型针对访问控制，并被描述为：“下读，上写”。\
在Bell-LaPadula模型中，用户只能在其自己的安全级别或更高的安全级别上创建内容（如，秘密研究人员可以创建秘密或绝密文件，但不能创建公共文件；不能下写）。相反，用户只能查看在其自己的安全级别或更低的安全级别的内容（如，秘密研究人员可以查看公共或秘密文件，但不能查看绝密文件；不能上读）。\&lt;/p&gt;
&lt;p&gt;Multi level security model that enforces information flow control;\
All security levels are monotonically ordered;\
-&amp;gt; Objects (files) have a clearance level【许可级别】\
A given clearance allows reading objects of lower or equal clearance and writing files of equal or higher clearance;\
Summarized as read-down, write-up;\
Bell/LaPadula enforces confidentiality;\&lt;/p&gt;
&lt;p&gt;注重机密性，此模型强调就是保证机密不能泄密。所以，机密程度越高，越保密，低的就不能读。写方面，高不能往低写，写的话机密的东西流到机密低的地方了。 所以他不允许数据由高级往低级流，但允许低级往高级流，机密性低的数据，跑到机密性高的地方，并不破坏机密性。
比如，1，2，3，4 ， 1级为最高机密，那么2级的人，不可以读1级数据，这将破坏保密原则。所以不能向上读。反之，1级的人，向2级区域写东西，则也会破坏机密性。&lt;/p&gt;
&lt;p&gt;If implemented naively, attacker may overwrite confidential files.
Mathias Payer&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Biba模型&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Multi level security model that enforces information flow control;\
All security levels are monotonically ordered;\
Objects (files) have an integrity level【完整性级别】\
A given clearance allows reading files of higher or equal
clearance and writing files of lower or equal clearance;\
Biba introduces notion of integrity, separate from secrecy;\
Summary: read-up, write-down;\
Biba enforces integrity;\&lt;/p&gt;
&lt;p&gt;完整性1，2，3，4级， 1级是最高完整性，比如是董事会最高决议，它并不需要保密，谁都能看，但任何人不得破坏其完整性，不能修改其决议，只有1级的人才能修改。所以2级的人不可以写1级的内容。但2级可以读1级的内容，来生成2级的信息。 此时，数据只能由高向低流，而禁止从低向高流。&lt;/p&gt;
&lt;p&gt;If implemented naively, attacker may leak privileged information.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;另，状态机模型的介绍另见笔记“有限状态机”。&lt;/em&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Discretionary【任意的，无条件的】 Access Control (DAC) 【自由访问控制】&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Idea: object owner specifies policy&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;MAC requires central control, empower【许可，准许】 the user!&lt;/li&gt;
&lt;li&gt;User has authority over her resources (introduce ownership)&lt;/li&gt;
&lt;li&gt;User sets permissions for her data if other users want access&lt;/li&gt;
&lt;li&gt;For example: Unix permissions&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Can become complex and may require per-file special casing.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Role-Based Access Control (RBAC) 【基于角色访问控制】&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;strong&gt;Policy defined in terms of roles (sets of permissions), individuals are assigned roles, roles are authorized for tasks.&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Access permission is broken into sets of roles.&lt;/li&gt;
&lt;li&gt;Users get assigned specific roles&lt;/li&gt;
&lt;li&gt;Administration privileges may be a role.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Roles simplify special casing but roles can become overly complex.&lt;/p&gt;
&lt;h2&gt;Access control matrix&lt;/h2&gt;
&lt;p&gt;map access rights for subjects to objects&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;foo&lt;/th&gt;
&lt;th&gt;bar&lt;/th&gt;
&lt;th&gt;baz&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;user&lt;/td&gt;
&lt;td&gt;rwx&lt;/td&gt;
&lt;td&gt;rw&lt;/td&gt;
&lt;td&gt;rw&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;group&lt;/td&gt;
&lt;td&gt;rx&lt;/td&gt;
&lt;td&gt;r&lt;/td&gt;
&lt;td&gt;r&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;other&lt;/td&gt;
&lt;td&gt;rx&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;r&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Used, e.g., for Unix/Linux file systems or Android/iOS/Java security model for privileged APIs.&lt;/p&gt;
&lt;p&gt;Introduced by Butler Lampson in 1971
http://research.microsoft.com/enus/um/people/blampson/08-Protection/Acrobat.pdf&lt;/p&gt;
&lt;h2&gt;Summary&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Software security goal: allow intended use of software, prevent
unintended use that may cause harm.&lt;/li&gt;
&lt;li&gt;Security triad, core principles: Confidentiality, Integrity, Availability.&lt;/li&gt;
&lt;li&gt;Security of a system depends on its threat model.&lt;/li&gt;
&lt;li&gt;Concepts: isolation, least privilege, fault compartments, trust.&lt;/li&gt;
&lt;li&gt;Security relies on abstractions to reduce complexity.&lt;/li&gt;
&lt;li&gt;Reading assignment: Butler Lampson, Protection
http://doi.acm.org/10.1145/775265.775268&lt;/li&gt;
&lt;/ul&gt;
</content:encoded></item></channel></rss>