카테고리
Exercise 02exercise-02
코드와 해설을 함께 읽는 학습 문서
Code Detail
manipulation of price data set with user defined functions
ex-02/main-02.m
코드를 복사해 Octave에서 바로 실행할 수 있습니다.
81 lines
# filename: main-02.m
# writer: won sunggyu
# date: 2025-03-25
# language: octave
# description: manipulation of price data set with user defined functions
run("../startup.m");
addpath(genpath("../mylib"));
printf(fmt("{mfilename}\n", "#FF5733"));
filename = "google_data_2024.csv";
[date_, price] = read_close_price(filename); # 1 x N
# Statistics
m = mean(price);
v = var(price);
s = std(price);
r = rms(price);
printf(fmt("mean = {m}, var = {v}, std = {s}, rms = {r}\n"));
m = my_mean(price);
v = my_var(price);
s = my_std(price);
r = my_rms(price);
printf(fmt("mean = {m}, var = {v}, std = {s}, rms = {r}\n"));
# Moving average
n_wind = 30;
nn = length(price) - n_wind + 1;
i_vec = 1:nn;
d_vec = date_(n_wind:n_wind + nn - 1);
m_vec = zeros(1, nn);
v_vec = zeros(1, nn);
s_vec = zeros(1, nn);
r_vec = zeros(1, nn);
for i = 1:nn;
price_wind = price(i:i+n_wind-1);
m_vec(i) = my_mean(price_wind);
v_vec(i) = my_var(price_wind);
s_vec(i) = my_std(price_wind);
r_vec(i) = my_rms(price_wind);
end
# Visualization
param_f = {"Size", [1280, 640], "Name", "ECO-5-Ex-02"};
param_a = {
"Xlabel", "Days of 2024", "Ylabel", "Averaged Price [USD]", ...
"XLim", [min(i_vec), max(i_vec)], ...
"XTick", i_vec(1:40:end), ...
"XTickLabel", d_vec(1:40:end), ...
};
figured(param_f);
subplots(param_a);
plotd(i_vec, m_vec, ";mean;", "LineWidth", 4);
plotd(i_vec, v_vec, ";var;");
plotd(i_vec, s_vec, ";std;");
plotd(i_vec, r_vec, ";rms;");
# Animation
# figured(param_f);
# subplots(param_a);
# h1 = plotd(i_vec(1:2), m_vec(1:2), ";m;", "LineWidth", 4);
# h2 = plotd(i_vec(1:2), v_vec(1:2), ";v;");
# h3 = plotd(i_vec(1:2), s_vec(1:2), ";s;");
# h4 = plotd(i_vec(1:2), r_vec(1:2), ";r;");
# anim = Animator();
# for i = 1:nn
# set(h1, "Xdata", i_vec(1:i), "Ydata", m_vec(1:i));
# set(h2, "Xdata", i_vec(1:i), "Ydata", v_vec(1:i));
# set(h3, "Xdata", i_vec(1:i), "Ydata", s_vec(1:i));
# set(h4, "Xdata", i_vec(1:i), "Ydata", r_vec(1:i));
# drawnow;
# anim.capture();
# end
# anim.close();