Octave Atelier

코드와 해설을 함께 읽는 학습 문서

Code Detail

main_02_cjw

Statistics + Visualization 중심의 Octave 학습 예제

ex-recv/02/02_cjw/main_02_cjw.m

목록으로

코드를 복사해 Octave에서 바로 실행할 수 있습니다.

카테고리

Submission Archive

submission

코드 길이

62

lines

작성자

-

날짜 정보 없음

패키지

none

pkg load

전체 코드

62 lines

% 주제(하나의 데이터 세트를 읽어서 대표값과 특징을 계산하고 그리는 연습을 합니다). 최정우. 20250328
clc;clear;

%%%%%%%%%% 1. 구글의 2024년도 주가 데이터를 준비하라
% 파일 이름 지정
filename = 'google_data_2024.csv';

% 파일에서 price 불러오기
price= read_csv_table(filename);

% price 중 close price 지정
price_end=price(:,1);

%%%%%%%%%% 2. 1년치 데이터의 종가에 대해 통계항목을 계산하라. 통계 항목: mean, variance, standard deviation, rms
%%%%%%%%%% 2_1. 정의식에 의해 직접 계산

avg_1=calculate_average(price_end); % 163.12

variance_1 = calculate_variance(price_end); % 236.8

standard_deviation_1 = calculate_standard_deviation(price_end); % 15.388

rms_1 = calculate_rms(price_end); % 163.84

%%%%%%%%%% 2_2. Octave 함수 사용하여 계산

avg_2 = mean(price_end); % 163.12

variance_2= var(price_end); % 236.8

standard_deviation_2 = std(price_end); % 15.388

rms_2 = rms(price_end); % 163.84
    
%%%%%%%%%% 3. 거래일: 1달 기준으로 통계항목 계산
month = 1:12; % 1~12월
days_in_month = [21, 20, 20, 22, 22, 19, 22, 22, 20, 23, 20, 20]; % 각 month에 day 수
start_day = 1;

for i = 1:length(month);
    end_day = start_day + days_in_month(i) - 1;  % 해당 월의 마지막 날
    monthly_avg(i) = calculate_average(price_end(start_day:end_day));  % 해당 월의 데이터 평균
    monthly_variance(i) = calculate_variance(price_end(start_day:end_day)); % 해당 월의 데이터 분산
    monthly_standard_deviation(i) = calculate_standard_deviation(price_end(start_day:end_day)); % 해당 월의 데이터 표준편차
    monthly_rms(i) = calculate_rms(price_end(start_day:end_day)); % 해당 월의 데이터 제곱평균제곱근
    start_day = end_day + 1;  % 다음 달의 첫 번째 날로 이동
end

figure(1)
plot(monthly_avg); hold on
plot(monthly_variance); hold on
plot(monthly_standard_deviation); hold on
plot(monthly_rms); hold off
grid on
xlim([1 12])
xticks(1:12)
xticklabels({'1월','2월','3월','4월','5월','6월','7월','8월','9월','10월','11월','12월'});
xlabel('Month')
title('2024 Google Monthy Price')
legend('Average','Variance','Standard_deviation','RMS')
set(gca,'fontsize',12)

코드 해설

목적

  • Statistics + Visualization 중심의 Octave 학습 예제

입력

  • 스크립트 상단에서 정의한 파라미터/입력 데이터를 사용합니다.

출력

  • 그래프/figure 출력

실행 흐름

  1. %%%%%%%%% 1. 구글의 2024년도 주가 데이터를 준비하라

핵심 함수

  • plot
  • price_end
  • calculate_average
  • calculate_rms
  • calculate_standard_deviation
  • calculate_variance
  • days_in_month
  • figure

실습 과제

  • 같은 연산을 내장 함수와 사용자 함수 두 방식으로 계산해 오차를 비교해보세요.
  • 축 범위와 라벨을 바꿔 그래프 해석성이 어떻게 달라지는지 확인해보세요.
  • 핵심 함수 plot의 인자를 한 가지 바꿔 결과 변화를 기록해보세요.

학습 팁

  • 그래프 비교 시 축 범위(XLim/YLim)와 단위를 먼저 고정하면 해석 오류를 줄일 수 있습니다.

같은 카테고리 코드

이전 코드 calculate_variance 다음 코드 read_csv_table