Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7720727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:44:41+00:00 2026-06-01T03:44:41+00:00

after optimization, I calculate the residuals with optimal parameters both in Python and C++.

  • 0

after optimization, I calculate the residuals with optimal parameters both in Python and C++. The gap in results is huge. Here is how I proceed more precisely:

I generate data according to a parametric model in Python. I store X and Y in Excel file. I load this file in my C++ program and run optimization. I come up with optimal parameters, that are pretty close to parameters used to generate the series. I then compute residuals (sum of squared difference between Y and model output with optimal parameters), in Python and with C++. Results are huge, with up to 10^3 difference for models that are very sensible to changes in parameters. Can these differences be imputable to different way to deal with precision in Python and C++, or can something else be wrong? Once optimization is finished, residuals computation is a simple calculation, and I wonder where the problem could lie if not in precision matter.

Thanks a lot for any advice or reference.

EDIT — I can easily show Python code for generating data and calculating sum of squared residuals, but not C++ code since calculation is performed via an interpreter. Thanks for any comments.

P1 =  5.21
P2 = 0.22

X_= list(range(0,100,1))
X=[float(x)/float(10) for x in X_]
Y = [P1*numpy.exp(-1*P2*x) for x in X]

##plt.plot(X,Y)
##plt.show()

##for j in range(len(Y)):
##    Y[j]+=rg.normal(0,0.01)

#build some input files
X1f = open('F:\WORK\SOLVEUR\ALGOCODE\PYTHON_\DataSets\exponential1X.txt', 'w')
for i in range(len(X)):
     X1f.write(str(X[i])+'\n')
X1f.close()

Yf = open('F:\WORK\SOLVEUR\ALGOCODE\PYTHON_\DataSets\exponential1Y.txt', 'w')
for i in range(len(Y)):
    Yf.write(str(Y[i])+'\n')
Yf.close()


def func_exp_1(param, x1, y):
   p1, p2 = param
   res = sum((y_i - p1*numpy.exp(-1*p2*x))**2 for x1_i, y_i in zip(x1, y))
   return res
print func_exp_1([5.2132,0.2202],x1,y)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T03:44:43+00:00Added an answer on June 1, 2026 at 3:44 am

    Both Python and C++ use the machine native format; Python’s float is
    the equivalent of C++’s double. Any differences
    would be due to differences in the way the algorithm is implemented,
    or, if the hardward has an extended format which is used for
    intermediate values (the case for Intel), when and where the language
    stores the values back to memory—values will probably be stored to
    memory more often in Python than in C++. Without seeing exact code,
    it’s impossible to say more (but the sum of a large number of elements
    can be off significantly, depending on the order and relative magnitudes
    of the elements).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have done an optimization with Pyevolve and after a look at the results
After some experience with functional languages, I'm starting to use recursion more in Java
After finishing coding for my app, I started to get into the optimization part.
My main goal with this question is optimization and faster run time. After doing
EDIT: Optimization results at end of this question! hi, i have a following code
I have a strange problem after turning on level 1 optimization in gcc. What
Looking for a code optimization in c# that allows me to both define an
I am looking at optimization options, and after checking SO questions, I don't quite
After highlighting text, I would like to obtain the paragraph in which the selected
After merging a branch back to the trunk what do most people do with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.