Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.05 KB | None | 0 0
  1. y =  degree_reg(V, b1f);
  2. k = build_plot_disp_loss(V, b1f, y);
  3.  
  4. function y=lin_reg(V, bf)
  5.     a = [sum(V) length(V);
  6.          sum(V.^2) sum(V)];
  7.     b = [sum(bf) sum(V .* bf)]';
  8.     x = a\b;
  9.     a = x(1);
  10.     b = x(2);
  11.     y = a * V + b;
  12. end
  13.  
  14. function y = square_reg(V, bf)
  15.     a = [sum(V.^2) sum(V) length(V);
  16.          sum(V.^3) sum(V.^2) sum(V);
  17.          sum(V.^4) sum(V.^3) sum(V.^2)];
  18.     b = [sum(bf) sum(V .* bf) sum(V.^2 .* bf)]';
  19.     x = a\b;
  20.     a = x(1);
  21.     b = x(2);
  22.     c = x(3);
  23.     y = a * V.^2 + b * V + c;
  24. end
  25.  
  26. function y = degree_reg(V, bf)
  27.     a = [length(V) sum(log(V));
  28.          sum(log(V)) sum(log(V).^2)];
  29.     b = [sum(log(bf)) sum(log(bf).*log(V))]';
  30.     x = a\b;
  31.     a = exp(x(1));
  32.     b = x(2);
  33.     y = a * V.^b;
  34. end
  35.  
  36. function y = loss_fn(bf, res)
  37.     y = 1/length(bf) * sum(abs((bf - res)./bf));
  38. end
  39.  
  40. function loss = build_plot_disp_loss(V, bf, res)
  41.     loss = loss_fn(bf, res);
  42.     disp("loss is: " + loss * 100 + "%");
  43.     figure('Color', 'w');
  44.     plot(V, bf, 'r');
  45.     hold on;
  46.     plot(V, res, 'b');
  47. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement