Initial commit
This commit is contained in:
parent
6396559547
commit
ac39b48f86
|
@ -0,0 +1,75 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "Lab-works-Rust"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"rand",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.2.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.149"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b"
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
version = "0.2.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.8.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"rand_chacha",
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_chacha"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
|
||||
dependencies = [
|
||||
"ppv-lite86",
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.11.0+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
|
|
@ -0,0 +1,28 @@
|
|||
[package]
|
||||
name = "Lab-works-Rust"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
default-run = "main"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[profile.dev.package."*"]
|
||||
opt-level = "z" # Optimize library for size
|
||||
|
||||
[profile.release]
|
||||
#opt-level = 'z' # Optimize for size
|
||||
opt-level = 3 # Optimize for speed
|
||||
lto = true # Enable link-time optimization
|
||||
codegen-units = 1 # Reduce number of codegen units to increase optimizations
|
||||
panic = 'abort' # Abort on panic
|
||||
strip = true # Strip symbols from binary*
|
||||
|
||||
[lints.clippy]
|
||||
pedantic = "warn"
|
||||
nursery = "warn"
|
||||
unwrap_used = "warn"
|
||||
expect_used = "warn"
|
||||
cargo = "warn"
|
||||
|
||||
[dependencies]
|
||||
rand = "0.8.5"
|
|
@ -0,0 +1,31 @@
|
|||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
|
||||
const BUFFER_SIZE: usize = 65536;
|
||||
|
||||
fn main() {
|
||||
let all_sums_quantity: i32 = 100_000_000;
|
||||
let mut above_200_sums_quantity: i32 = 0;
|
||||
|
||||
let mut f = File::open("random_source").unwrap();
|
||||
let mut buffer = [0u8; BUFFER_SIZE];
|
||||
|
||||
let total_cycles: i32 = all_sums_quantity / BUFFER_SIZE as i32;
|
||||
|
||||
for _i in 0..total_cycles {
|
||||
f.read_exact(&mut buffer).unwrap();
|
||||
for p in buffer.windows(2) {
|
||||
if p[0] as u16 + p[1] as u16 > 200 {
|
||||
above_200_sums_quantity += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let probability_of_a: f64 = (above_200_sums_quantity) as f64 / (all_sums_quantity) as f64;
|
||||
|
||||
println!("Кількість експериментів: {all_sums_quantity}");
|
||||
println!("Кількість сум двох байтів що більше за 200: {above_200_sums_quantity}");
|
||||
println!(
|
||||
"Статистична ймовірність того, що сума двох випадкових байтів є більша за 200: {probability_of_a:.10}"
|
||||
);
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
use rand::{thread_rng, Rng};
|
||||
|
||||
const BUFFER_SIZE: usize = 65536;
|
||||
|
||||
fn main() {
|
||||
let all_sums_quantity: i32 = 100_000_000;
|
||||
let mut above_200_sums_quantity: i32 = 0;
|
||||
|
||||
let mut buffer = [0u8; BUFFER_SIZE];
|
||||
|
||||
let total_cycles: i32 = all_sums_quantity / BUFFER_SIZE as i32;
|
||||
|
||||
for _ in 0..total_cycles {
|
||||
thread_rng().fill(&mut buffer[..]);
|
||||
for p in buffer.windows(2) {
|
||||
if p[0] as u16 + p[1] as u16 > 200 {
|
||||
above_200_sums_quantity += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let probability_of_a: f64 = (above_200_sums_quantity) as f64 / (all_sums_quantity) as f64;
|
||||
|
||||
println!("Кількість експериментів: {all_sums_quantity}");
|
||||
println!("Кількість сум двох байтів що більше за 200: {above_200_sums_quantity}");
|
||||
println!(
|
||||
"Статистична ймовірність того, що сума двох випадкових байтів є більша за 200: {probability_of_a:.10}"
|
||||
);
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
use rand::random;
|
||||
|
||||
const SAMPLE_SIZE: usize = 5_000;
|
||||
|
||||
fn simpson_from_uniform(m: i32, a: i32, r: f32) -> f32 {
|
||||
if r <= 0.5 {
|
||||
let d = (2f32 * r).sqrt() / 0.2;
|
||||
(m - a) as f32 + d
|
||||
} else {
|
||||
let d = (2f32 * (1f32 - r)).sqrt() / 0.2;
|
||||
(m + a) as f32 - d
|
||||
}
|
||||
}
|
||||
|
||||
fn mean(distribution: &[f32]) -> f32 {
|
||||
distribution.iter().sum::<f32>() / distribution.len() as f32
|
||||
}
|
||||
|
||||
fn dispersion(mean: f32, distribution: &[f32]) -> f32 {
|
||||
distribution
|
||||
.iter()
|
||||
.map(|x| (*x - mean).powi(2))
|
||||
.sum::<f32>()
|
||||
/ distribution.len() as f32
|
||||
}
|
||||
|
||||
fn standard_deviation(mean: f32, distribution: &[f32]) -> f32 {
|
||||
dispersion(mean, distribution).sqrt()
|
||||
}
|
||||
|
||||
fn probability_in_limits(lower: f32, upper: f32, distribution: &[f32]) -> f32 {
|
||||
distribution
|
||||
.iter()
|
||||
.filter(|x| **x > lower && **x < upper)
|
||||
.count() as f32
|
||||
/ distribution.len() as f32
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let set_m = 20;
|
||||
let set_a = 5;
|
||||
let simpson_numbers: Vec<f32> = (0..=SAMPLE_SIZE)
|
||||
.map(|_| simpson_from_uniform(set_m, set_a, random::<f32>()))
|
||||
.collect();
|
||||
|
||||
let m = mean(&simpson_numbers);
|
||||
let std_dev = standard_deviation(m, &simpson_numbers);
|
||||
let probability = probability_in_limits(17f32, 24f32, &simpson_numbers);
|
||||
|
||||
println!("Математичне очікування розподілу: {m}");
|
||||
println!("Середньоквадратичне відхилення розподілу: {std_dev}");
|
||||
println!("Ймовірність потрапляння у проміжок [17; 24]: {probability}");
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
use rand::random;
|
||||
|
||||
fn approximate_x(random_number: f32) -> f32 {
|
||||
let mut x: f32 = 0f32;
|
||||
while random_number
|
||||
> (2f32 / std::f32::consts::PI).mul_add(
|
||||
(x - 1f32).mul_add(
|
||||
(x - 1f32).mul_add(-(x - 1f32), 1f32).sqrt(),
|
||||
(x - 1f32).asin(),
|
||||
),
|
||||
1f32,
|
||||
)
|
||||
{
|
||||
x += 0.000_1;
|
||||
}
|
||||
x
|
||||
}
|
||||
|
||||
fn calculate_y(random_number: f32, x: &f32) -> f32 {
|
||||
random_number * 2f32 * (x - 1f32).mul_add(-(x - 1f32), 1f32).sqrt()
|
||||
- (x - 1f32).mul_add(-(x - 1f32), 1f32).sqrt()
|
||||
+ 1f32
|
||||
}
|
||||
|
||||
fn mean(distribution: &[f32]) -> f32 {
|
||||
distribution.iter().sum::<f32>() / SAMPLE_SIZE as f32
|
||||
}
|
||||
|
||||
fn dispersion(mean: f32, distribution: &[f32]) -> f32 {
|
||||
distribution
|
||||
.iter()
|
||||
.map(|x| (*x - mean).powi(2))
|
||||
.sum::<f32>()
|
||||
/ (SAMPLE_SIZE - 1) as f32
|
||||
}
|
||||
|
||||
fn standard_deviation(mean: f32, distribution: &[f32]) -> f32 {
|
||||
dispersion(mean, distribution).sqrt()
|
||||
}
|
||||
|
||||
fn covariance(distribution_x: &[f32], mean_x: f32, distribution_y: &[f32], mean_y: f32) -> f32 {
|
||||
let mut sum: f32 = 0f32;
|
||||
for i in 0..SAMPLE_SIZE {
|
||||
sum += (distribution_x[i] - mean_x) * (distribution_y[i] - mean_y);
|
||||
}
|
||||
sum / (SAMPLE_SIZE - 1) as f32
|
||||
}
|
||||
|
||||
const SAMPLE_SIZE: usize = 5_000;
|
||||
|
||||
fn main() {
|
||||
let x: Vec<f32> = (0..SAMPLE_SIZE)
|
||||
.map(|_| approximate_x(random::<f32>()))
|
||||
.collect();
|
||||
let mut y: Vec<f32> = Vec::new();
|
||||
|
||||
x.iter()
|
||||
.for_each(|x| y.push(calculate_y(random::<f32>(), x)));
|
||||
|
||||
let mean_x = mean(&x);
|
||||
let mean_y = mean(&y);
|
||||
|
||||
println!("Середнє значення розподілу x: {mean_x}");
|
||||
println!("Середнє значення розподілу y: {mean_y}");
|
||||
|
||||
let x_deviation = standard_deviation(mean_x, &x);
|
||||
let y_deviation = standard_deviation(mean_y, &y);
|
||||
|
||||
println!("Середньоквадратичне відхилення розподілу x: {x_deviation}");
|
||||
println!("Середньоквадратичне відхилення розподілу y: {y_deviation}");
|
||||
|
||||
let covariance = covariance(&x, mean_x, &y, mean_y);
|
||||
let correlation = covariance / (x_deviation * y_deviation);
|
||||
|
||||
println!("Коефіцієнт кореляції: {correlation}");
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
use rand::{thread_rng, Rng};
|
||||
|
||||
fn exponential_distribution(random_number: f64, lambda: f64) -> f64 {
|
||||
-(1f64 / lambda) * random_number.ln()
|
||||
}
|
||||
|
||||
fn average(distribution: &[f64]) -> f64 {
|
||||
distribution.iter().sum::<f64>() / SAMPLE_SIZE as f64
|
||||
}
|
||||
|
||||
const SAMPLE_SIZE: usize = 100;
|
||||
const CHUNK_SIZE: usize = SAMPLE_SIZE / 4;
|
||||
|
||||
fn main() {
|
||||
let mut rng = thread_rng();
|
||||
|
||||
let lambda: f64 = 0.2;
|
||||
let mut x: Vec<f64> = (0..SAMPLE_SIZE)
|
||||
.map(|_| exponential_distribution(rng.gen(), lambda))
|
||||
.collect();
|
||||
x.sort_by(|a, b| a.partial_cmp(b).unwrap());
|
||||
|
||||
// let lambda_experimental: f64 = 1f64 / average(&x);
|
||||
dbg!(average(&x));
|
||||
dbg!(1f64 / lambda);
|
||||
|
||||
// dbg!(lambda_experimental);
|
||||
|
||||
let mut probabilities: Vec<f64> = x
|
||||
.chunks(CHUNK_SIZE)
|
||||
.map(|i| 1f64 - (-lambda * i.last().unwrap_or(&0f64)).exp())
|
||||
.collect(); // Os' jak ja šukaju jmovirnosti [P1, P', ...].
|
||||
|
||||
(1..probabilities.len())
|
||||
.rev()
|
||||
.for_each(|i| probabilities[i] -= probabilities[i - 1]); // I os' tut ja jih peretvorjuju na [P1, P2, ...]
|
||||
|
||||
/*let mut hi2_1: f64 = 0f64;
|
||||
|
||||
(0..probabilities.len())
|
||||
.for_each(|i| hi2_1 += CHUNK_SIZE.pow(2) as f64 / (SAMPLE_SIZE as f64 * probabilities[i]));
|
||||
|
||||
hi2_1 -= SAMPLE_SIZE as f64;
|
||||
|
||||
dbg!(hi2_1);*/
|
||||
|
||||
let mut hi2_2: f64 = 0f64;
|
||||
|
||||
(0..probabilities.len()).for_each(|i| {
|
||||
hi2_2 += (SAMPLE_SIZE as f64)
|
||||
.mul_add(-probabilities[i], CHUNK_SIZE as f64)
|
||||
.powi(2)
|
||||
/ (SAMPLE_SIZE as f64 * probabilities[i]);
|
||||
});
|
||||
|
||||
dbg!(hi2_2);
|
||||
|
||||
let something = "Žaba!";
|
||||
|
||||
println!("Hello {something}!")
|
||||
}
|
|
@ -0,0 +1,100 @@
|
|||
use rand::{thread_rng, Rng};
|
||||
|
||||
const SAMPLE_SIZE: usize = 1000;
|
||||
|
||||
#[derive(Debug)]
|
||||
enum State {
|
||||
One,
|
||||
Two,
|
||||
Three,
|
||||
Four,
|
||||
Five,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct TotalStats {
|
||||
state: State,
|
||||
state_counter: Vec<u32>,
|
||||
}
|
||||
|
||||
impl TotalStats {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
state: State::One,
|
||||
state_counter: vec![0; 5],
|
||||
}
|
||||
}
|
||||
fn change_state(&mut self, random_number: f32) {
|
||||
match &self.state {
|
||||
State::One => {
|
||||
if (0f32..0.2).contains(&random_number) {
|
||||
self.state = State::One;
|
||||
self.state_counter[0] += 1;
|
||||
} else if (0.2..0.7).contains(&random_number) {
|
||||
self.state = State::Two;
|
||||
self.state_counter[1] += 1;
|
||||
} else if (0.7..1f32).contains(&random_number) {
|
||||
self.state = State::Five;
|
||||
self.state_counter[4] += 1;
|
||||
}
|
||||
}
|
||||
State::Two => {
|
||||
if (0f32..0.35).contains(&random_number) {
|
||||
self.state = State::Three;
|
||||
self.state_counter[2] += 1;
|
||||
} else if (0.35..1f32).contains(&random_number) {
|
||||
self.state = State::Four;
|
||||
self.state_counter[3] += 1;
|
||||
}
|
||||
}
|
||||
State::Three => {
|
||||
if (0f32..0.2).contains(&random_number) {
|
||||
self.state = State::One;
|
||||
self.state_counter[0] += 1;
|
||||
} else if (0.2..1f32).contains(&random_number) {
|
||||
self.state = State::Four;
|
||||
self.state_counter[3] += 1;
|
||||
}
|
||||
}
|
||||
State::Four => {
|
||||
if (0f32..1f32).contains(&random_number) {
|
||||
self.state = State::Five;
|
||||
self.state_counter[4] += 1;
|
||||
}
|
||||
}
|
||||
State::Five => {
|
||||
if (0f32..0.45).contains(&random_number) {
|
||||
self.state = State::One;
|
||||
self.state_counter[0] += 1;
|
||||
} else if (0.45..1f32).contains(&random_number) {
|
||||
self.state = State::Four;
|
||||
self.state_counter[3] += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn probabilities(&self) -> Vec<f32> {
|
||||
let mut probabilities: Vec<f32> = Vec::new();
|
||||
|
||||
self.state_counter
|
||||
.iter()
|
||||
.for_each(|i| probabilities.push(*i as f32 / SAMPLE_SIZE as f32));
|
||||
|
||||
probabilities
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut rng = thread_rng();
|
||||
|
||||
let mut stats = TotalStats::new();
|
||||
|
||||
(0..SAMPLE_SIZE).for_each(|_| stats.change_state(rng.gen()));
|
||||
|
||||
stats
|
||||
.probabilities()
|
||||
.iter()
|
||||
.enumerate()
|
||||
.for_each(|(i, p)| println!("p{i} = {p}"));
|
||||
}
|
Loading…
Reference in New Issue