Let $I$ be an open interval of $\mathbb R$. Let $\varphi:I\to\mathbb R$ be a continuous function such that $\forall x\in I$, there is an open interval $V(x)\subset I$ containing $x$ such that $\varphi$ is convex on $V(x)$. Is it true that $\varphi$ is convex on $I$? What about if we remove the continuity hypothesis?
It's tempting to say that for any $x\in I$, $\exists y\in I$, $V(x)\cap V(y)\neq\emptyset$ and so $\varphi$ is convex on $V(x)\cup V(y)$ since in this case $V(x)\cup V(y)$ is convex. In that case, we get that $\varphi$ is convex on $\bigcup_{x\in I}V(x)=I$. The problem is I dont know if we can necessarily find such a $y$. Furthermore, we don't use the fact that $\varphi$ is continuous at all, which feels weird. But then on the other hand, I don't really know how to get from the local convexity to convexity without using a similar reasoning. Any help on this question would be greatly appreciated.
I think this is simple enough, and that the continuity of $\phi$ is not needed. Let $c\in(a,b)$. By the hypothesis, there are $c^{-},c^{+}$ with $a \lt c^{-} \lt c \lt c^{+} \lt b$ such that $\phi$ is convex on $[c^{-},c^{+}]$. Now, let
$$ \Omega=\bigg\lbrace d\in [c^{-},b) \ \bigg| \ \phi \ \textrm{is convex on } [c^{-},d] \bigg\rbrace $$
We have $[c^{-},c^{+}] \subseteq \Omega \subseteq [c^{-},b)$, so $\omega=\sup(\Omega)$ is $\leq b$, and further $\Omega=[c^{-},\omega)$.
In fact, we can show that $\omega=b$, by contradiction as follows. Suppose that $\omega \lt b$. By the hypothesis, there are $\omega^{-},\omega^{+}$ with $a \lt \omega^{-} \lt \omega \lt \omega^{+} \lt b$ such that $\phi$ is convex on $J=[\omega^{-},\omega^{+}]$. Then $\frac{\omega^{-}+\omega}{2}\in [c^{-},\omega)=\Omega$, so $\phi$ is convex on $K=[c^{-},\frac{\omega^{-}+\omega}{2}]$. Now the interior of $J\cap K$ is nonempty (it contains a neighborhood of $\omega^{-}$), so by gluing, $\phi$ must be convex on $J\cup K=[c^{-},\omega^{+}]$. But then $\omega^{+}\in \Omega$, contradicting $\omega=\sup(\Omega)$. So $\omega=b$, and $\phi$ is therefore convex on $[c^{-},b)$.
A symmetrical argument shows that phi is convex on $(a,c^{+}]$. By gluing, $\phi$ is convex on $(a,b)$ as wished.
Appendix: Since there is a userin the comments who is skeptical about the gluing property, I add a proof for it :
Theorem(gluing property). Suppose a function $\phi$ is convex on two intervals $J,K$ of $\mathbb R$, and that $J\cap K$ has nonempty interior. Then $\phi$ is convex on $J\cup K$.
Proof. For $x_1\lt x_2\lt x_3$ define
$$ \Delta_{f}(x_1,x_2,x_3)= (x_3-x_2)f(x_1)+(x_2-x_1)f(x_3)-(x_3-x_1)f(x_2) $$
Notice that $f$ is convex an interval $I$ iff $\Delta_{f}(x_1,x_2,x_3)\geq 0$ for any $x_1\lt x_2\lt x_3\in I$.
Now, we have the purely algebraic identity
$$ a\Delta_{f}(x_1,x_2,x_5)= b_1\Delta_{f}(x_1,x_2,x_3)+ b_2\Delta_{f}(x_2,x_3,x_4)+ b_3\Delta_{f}(x_3,x_4,x_5) \tag{1} $$
where $a,b_1,b_2,b_3$ are the nonnegative numbers defined by
$$ \begin{array}{lcl} a &=&(x_4-x_3)(x_3-x_2) \\ b_1&=&(x_4-x_3)(x_5-x_2) \\ b_2&=&(x_2-x_1)(x_5-x_3) \\ b_3&=&(x_2-x_1)(x_3-x_2) \end{array}\tag{2} $$
More generally, for an arbitrary number of points $x_1\lt x_2\lt \ldots \lt x_n$, it can be shown that any $\Delta_f(x_i,x_j,x_k)$ with $i\leq j\leq k$ can be written as a nonnegative linear combination of the $\Delta_f(x_t,x_{t+1},x_{t+2})$ for $i\leq t \leq j-2$ ; this can be shown by induction on $k-i$, starting with the trivial cases when some variables are equal. The formulas in (2) reflect this.
In particular, if the three deltas in the RHS of (1) are nonnegative, so is the $\Delta$ in the LHS.
Let us now show the theorem. Take $x_1 \lt x_2 \lt x_5$ in $J\cup K$ ; our goal is to show that $\Delta_{f}(x_1,x_2,x_5)\geq 0$.
If $x_1,x_2,x_5$ are all in $J$ or all in $K$, we are done by the hypothesis. So we can assume without loss that (say) $x_1,x_2\in J,x_5\in K$. Then we can find $x_3 \lt x_4\in J\cap K$ such that $x_2\lt x_3 \lt x_4 \lt x_5$. Applying (1) finishes the proof.