능선 회귀 이미지의 tikz 이미지를 그리는 방법은 무엇입니까?

능선 회귀 이미지의 tikz 이미지를 그리는 방법은 무엇입니까?

다음 이미지를 다시 만들려고 합니다.여기에 이미지 설명을 입력하세요

아쉽게도 Python으로 그린 ​​것이기 때문에 실제로 사용할 수는 없습니다. 그래서 틱즈로 다시 만들어야겠다고 생각했어요.

왼쪽 이미지를 만들려는 나의 시도는 다음과 같습니다.

\usetikzlibrary{arrows}
\usetikzlibrary{decorations.markings}
\begin{tikzpicture}
\coordinate (Origin) at (0,0);
\coordinate (OLSEstimates) at (2,8);
\coordinate (Intersect) at (2.55,4.3);
%0.5 scaled
\draw[rotate=45] (OLSEstimates) ellipse (1.275 and 3);
%0.75 scaled
\draw[rotate=45] (OLSEstimates) ellipse (1.9125 and 4.5);
%original boundary
\draw[rotate=45] (OLSEstimates) ellipse (2.56 and 6);
\draw [<->,thick] (0,10) node (yaxis) [above] {$y$}
        |- (10,0) node (xaxis) [right] {$x$};
\draw (0,0) circle (5);
%draw line to intersection
\draw[draw=black,-triangle 90] (Origin) -- (Intersect);
\end{tikzpicture}

이제 문제는 내 원이 타원에 닿는 것처럼 보이지만 수동으로 좌표를 조정하여 고통스럽게 작업을 수행했다는 것입니다(따라서 아마도 잘못된 것일 수 있습니다). 둘째, 타원과 원의 점교점도 같은 방법으로 처리했기 때문에 조금 어긋나 있을 수도 있습니다.

이제 저는 Tikz가 두 선의 교차점 좌표를 계산할 수 있다는 놀라운 사실을 보았습니다. 그래서 여기에 어떤 종류의 기술을 적용할 수 있는지 궁금했습니다.

작은 참고 사항: 타원의 크기를 자동으로 정확한 "크기"로 조정하여 정확히 한 지점에서 교차할 수 있다면 완벽할 것입니다. 그러나 이는 불가능하다고 생각합니다(누군가 그렇게 하는 것을 본 적이 없습니다).

감사해요!

답변1

동심 타원과 점을 배치하도록 코드를 수정했습니다. 타원을 정사각형 노드에 고정하는 것과 동일한 아이디어도 가능합니다.

대신 타원의 방향은 능선 방정식이 가리켜야 한다고 생각하는 원점 노드(그러나 그 부분을 이해하지 못했습니다)에 대해 고정하여 변경할 수 있습니다.

\documentclass[tikz]{standalone}
\usetikzlibrary{shapes.geometric,calc}
\begin{document}
\begin{tikzpicture}[>=latex]
\clip (-1,-1) rectangle (6,6);
\draw[->,thick] (-1,0) --++(6,0);
\draw[->,thick] (0,-1) --++(0,6);
% Given 
\def\myradius{2.5cm} % CHANGE THESE
\def\mypoint{(3,2)}
% computed 
\node[circle,draw,minimum height=2*\myradius] (o) at (0,0) {};
% here we take the point and compute the distance to the circle node 
% and also the angle of the point wrt to origin. Then we rotate ellipses and adjust the size
\path let \p1=\mypoint,\n1 = {veclen(\x1,\y1)-\myradius},\n2={atan2(\y1,\x1)} in 
\foreach \x in {1,0.75,0.5}{
node[ellipse,draw,
     minimum height=2*\n1*\x,
     minimum width=3.5*\n1*\x,
     rotate=\n2-90] (a) at \mypoint {}
};
\draw[->] (o.center) -- (a.center) 
node[above,inner sep=1pt,rounded corners,fill=white,draw] {$\theta_{\text{Normal Equation}}$};
\draw[->] (o.center) -- (o.80) 
node[above,inner sep=1pt,rounded corners,fill=white,draw] {$\theta_{\text{RidgeEquation}}$};
\end{tikzpicture}
\end{document}

여기에 이미지 설명을 입력하세요

답변2

다음과 같이 시도해 볼 수 있지만 더 효율적인 방법이 있어야 합니다.

\usetikzlibrary{intersections,calc}
\usetikzlibrary{arrows}
\usetikzlibrary{decorations.markings}
\begin{tikzpicture}
\draw [<->,thick] (0,10) node (yaxis) [above] {$y$}
    |- (10,0) node (xaxis) [right] {$x$};

\coordinate (Origin) at (0,0);
\coordinate (OLSEstimates) at (2,8);
\coordinate (Intersect) at (0,5.1); % Initial guess, must be inside the ellipse
%original boundary
\draw[rotate=45, name path=ellipse] (OLSEstimates) ellipse (2.56 and 6);
%scaled boundaries
\foreach \scale in {0.75,0.5} {
    \draw[rotate=45] (OLSEstimates) ellipse (\scale*2.56 and \scale*6);
}
%Compute intersection point iteratively
\foreach \j in {1,...,3} {
    \path[name path=circle-\j, overlay] let \p1 = ($(Origin)-(Intersect)$) in (Origin) circle ({veclen(\x1,\y1)});
    \draw[name intersections={of=ellipse and circle-\j,by={a,b}, total=\t}] let \p1 = ($0.5*(a)+0.5*(b)$) in (\x1,\y1) coordinate (Intersect);
}
\draw let \p1 = ($(Origin)-(Intersect)$) in (Origin) circle ({veclen(\x1,\y1)});
%draw line to intersection
\draw[draw=black,-triangle 90] (Origin) -- (Intersect);
\end{tikzpicture}

우리는 타원과의 교차점이 단 하나뿐인 원을 찾고 싶습니다. 이 코드에서는 원과 타원 사이의 두 교차점의 중심을 취하고 다음 원이 이 점을 교차하도록 하여 반복적으로 계산됩니다.

코드는 작동하지만 매우 느립니다!

줄임표를 그리는 경우 \draw명령을 루프에 넣었는데 foreach보시다시피 매우 간단합니다.

관련 정보